I’m an asp.net / c# newbie and i’m trying to create a very simple menu that is generated from my database.
so far i have managed to extract the table from database and into a data table and from there to an array.
My problem is that i haven’t figured out how to use the data in a loop in my page (the menu changes according to user types).
I tried using <% =Array %> but i can’t seem to use that inside a while/for loop or even assignment line.
Maybe i’m attacking this the wrong way , i guess my question is basically this:
How can i use the data i gather from the database (names , urls) in my dynamic page?
here is my current code behind:
public string[] keep = new string[100];
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Response.Write("response in postback");
getMenu();
}
}
void getMenu()
{
Response.Write("response in getmenue");
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "Select * from Materials";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
dt = ds.Tables[0];
DataRow[] drowpar = dt.Select();
int i = 0;
foreach (DataRow dr in drowpar)
{
keep[i] = dr["Material Name"] as string;
i = i + 1;
}
con.Close();
}
Thank you.
You just need to use a data contol like gridview or repeater.You can then bind dataset to those control’s datasource property. From my point of view repeater will be best datacontro for dynamic menu..
here is mark up for repeater
and here is code to bind dataset