how to increment index value of integer array

I want to store value from datatbase table to array.but when I do,exception"object eferance not set instance of object " through please review my code.

.cod Behinde

   public DataSet showoption1()
    {
        SqlCommand cmd = new SqlCommand("select * from assessmenttest",con);

        SqlDataAdapter adptr = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adptr.Fill(ds,"test");

        int [] arr=new int[10];
        DataTable table=ds.Tables[0];
        for(int i=0;i<table.Rows.Count;i++  )
        {
test.Agree[i] =Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);

    }

Business logic layer class code:

public class test{

public static int[] agree;

}

Answers


test.agree is null.

You need to put a new array in the field.


Where exactly are you getting the error, on which array? By the looks of it, it has to be on the agree[] one, which you never instantiate.

try

public static int[] agree =new int[10];

but I think you might want to make agree a list since you don't know how many you need

public static List<int> agree = new List<int>;

then usage

agree.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);

and you can access it the same as an array

MessageBox.Show(agree[1].ToString());

Instantiate before adding values :

 SqlCommand cmd = new SqlCommand("select * from assessmenttest", con);

        SqlDataAdapter adptr = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adptr.Fill(ds, "test");

        int[] arr = new int[10];
        DataTable table = ds.Tables[0];
        test.agree = new int[table.Rows.Count];
        for (int i = 0; i < table.Rows.Count; i++)
        {

            test.agree[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["option1"]);
        }

Need Your Help

Lazy Loading Items in Entity Framework

c# .net entity-framework lazy-loading

I have two objects that have a many to many relationship with each other.

CCS\HTML - Spry menu sub-menu change text on hover

html css menu

I have a spry menu and it has a drop menu I want to have the text change when you hover over the menu button. So what that would look like is when you have the drop down menu open and highlight ove...