User-1887133141 posted
Hi pals,
I have a table ("Menus") with the follwoing attributes:
Id(int), name(nvarchar), parent(int), path(nvarchar)
and a struct named ("menusData"). I am going to assign the data in the table to an array of the struct. The following is the code. But unfortunately, I don't know how to assing Int types and nvarchar types. The following code throws an exception! Please
help me with the problem:
protected struct menusData
{
public int id, parent;
public string name, path;
}
protected menusData [] myMenu = new menusData[100];
protected int noMyMenu = 0;
protected void fillData()
{
string connect = "SomeConnectionString";
SqlConnection con = new SqlConnection(connect);
var cmd = new SqlCommand("SELECT * FROM Menus", con);
cmd.Connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
myMenu[noMyMenu].id = (Int16)reader.GetInt16(0); //0 = id column
myMenu[noMyMenu].name = (string)reader.GetString(1); //1 = name column
myMenu[noMyMenu].parent = (Int16)reader.GetInt16(2); //2 = parent column
myMenu[noMyMenu].path = (string)reader.GetString(3); //3 = path column
}
reader.Close();
cmd.Connection.Close();
cmd.Dispose();
}