Monday, April 1, 2013

how to fill menu dynamically in asp.net from database


  Query = "select * from tblCategory ";
            DataTable dtSubCat = GetDataTable(Query);

            foreach (DataRow drSubCat in dtSubCat.Rows)
            {
                string MText, MValue, MToolTip, MImageUrl, MNavigateUrl;

                MText = drSubCat["SubCatName"].ToString();
                MValue = drSubCat["Value"].ToString();
                MImageUrl = drSubCat["ImgUrl"].ToString();
                MNavigateUrl = drSubCat["NavigateUrl"].ToString();
                MToolTip = drSubCat["ToolTip"].ToString();

                MenuItem Mi = new MenuItem();
                Mi.Text = MText;
                Mi.Value = MValue;
                Mi.ToolTip = MToolTip;
                Mi.ImageUrl = MImageUrl;
                Mi.NavigateUrl = MNavigateUrl;
                Mi.SeparatorImageUrl = "";

                MenuControl.Items.Add(Mi);
            }

public DataTable GetDataTable(string Query)
    {

        SqlConnection objCon = new SqlConnection("your Connect String");
        SqlCommand objCmd = new SqlCommand(Query, objCon);

        SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

        objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
        DataTable dt = new DataTable("Table");
        objDA.Fill(dt);
        return dt;

    }

Edit Grid at runtime in ASP.net


First You Make one item Template To Show Data and One Edit Item Template enable editing in grid view  like this
< asp: TemplateField HeaderText="Sub Department"

This line will Show Textbox at the place of label
 GridViewPP.EditIndex = e.NewEditIndex
complete code :
Protected Sub GridViewPP_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridViewPP.RowEditing
 
GridViewPP.EditIndex = e.NewEditIndex
QRY = "SELECT SubDepartment.SubDepartment_ID,SubDepartment.Department_ID, Department.DepartmentDescription,SubDepartment.SubDepartment_Description, Employee.UserName AS CreatedBy,Employee_1.UserName AS ModifyBy, SubDepartment.CreatedOn,SubDepartment.ModifyOn FROM Department RIGHT OUTER JOIN Employee RIGHT OUTER JOIN SubDepartment LEFT OUTER JOIN Employee AS Employee_1 ON SubDepartment.ModifyBy = Employee_1.UserID ON Employee.UserID = SubDepartment.CreatedBy ON Department.DepartmentID = SubDepartment.Department_ID"

This Query show all data in gridview and textbox as there you want to update data
end sub

Now on Update Button in GridView
Protected Sub GridViewPP_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewPP.RowUpdating
 
Dim row As GridViewRow = CType(GridViewPP.Rows(e.RowIndex), GridViewRow)
Dim subDept As Label = CType(row.FindControl("lblsubDeptID"), Label)
Dim subDeptDesc As TextBox = CType(row.FindControl("txtSubDeptDesc"), TextBox)
pass Subdept and subDeptDesc in the update Query Like this
QRY = "Update SubDepartment Set SubDepartment_Description='" & subDeptDesc.Text & "',ModifyOn='" & datetime & "',ModifyBy='" & Session("UserID") & "' where SubDepartment_ID=" & subDept.Text


You can update data at runtime in grid view .