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;

    }

No comments:

Post a Comment