locked
Assign different values to an array of struct RRS feed

  • Question

  • 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();
        }

    Friday, April 25, 2014 11:26 AM

Answers

  • User2103319870 posted

    Please post the details of Exception which you are getting.

    Also you can try with this suggestion to use Int32 instead of Int16

    myMenu[noMyMenu].id = (Int32)reader.GetInt32(0);      //0 = id column
    myMenu[noMyMenu].parent = (Int32)reader.GetInt32(2);  //2 = parent column

    Please try with the below code

    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 = (Int32)reader.GetInt32(0);      //0 = id column
                    myMenu[noMyMenu].name = (string)reader.GetString(1);  //1 = name column
                    myMenu[noMyMenu].parent = (Int32)reader.GetInt32(2);  //2 = parent column
                    myMenu[noMyMenu].path = (string)reader.GetString(3);  //3 = path column
                }
    
                reader.Close();
                cmd.Connection.Close();
                cmd.Dispose();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 25, 2014 12:18 PM