Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to retrive multiple rows from datareader so my code is following bt there is some error:-like:-Index was outside the bounds of the array.
my code:-
C#
SqlConnection sqlCon = new SqlConnection();
       sqlCon.ConnectionString = "Data Source=PC-13\\SQLEXPRESS;Initial Catalog=DbPlacementCoordinator;Integrated Security=True";
       sqlCon.Open();
       string str = lbcategory.SelectedItem.Value;
       string query = "Select Industry_ID from Industry_Category where Industry_Name=@str;";

       SqlCommand cmd3 = new SqlCommand(query, sqlCon);
       cmd3.Parameters.AddWithValue("str", str);
       int b = Convert.ToInt16(cmd3.ExecuteScalar());

       string jobcat = "Select Job_Name from Job_Category where Industry_ID=1;";

       SqlCommand cmd4 = new SqlCommand(jobcat,sqlCon);
       SqlDataReader reader = cmd4.ExecuteReader();

       while (reader.Read())
       {
           Label6.Text = Convert.ToString(reader[0]);
           Label8.Text = Convert.ToString(reader[1]);//here above error occure.
           Label10.Text = Convert.ToString(reader[2]);
           Label12.Text = Convert.ToString(reader[3]);
           Label14.Text = Convert.ToString(reader[4]);
           Label16.Text = Convert.ToString(reader[5]);
           Label18.Text = Convert.ToString(reader[6]);
           Label20.Text = Convert.ToString(reader[7]);
           Label22.Text = Convert.ToString(reader[8]);
           Label24.Text = Convert.ToString(reader[9]);
           Label26.Text = Convert.ToString(reader[10]);
           Label28.Text = Convert.ToString(reader[11]);
           Label30.Text = Convert.ToString(reader[12]);
           Label7.Text = Convert.ToString(reader[13]);
       }
       Panelconstruction.Visible = true;
       reader.Close();

so plz try to help me where is mistake.....
Posted
Updated 12-Jun-12 20:53pm
v2
Comments
Upniwesh 13-Jun-12 2:50am    
How many Column in your table?
Member 9027483 13-Jun-12 3:07am    
three columns...that is Industry_ID,Job_ID,Job_Name.

hi Look at your query
C#
string jobcat = "Select Job_Name from Job_Category where Industry_ID=1;";

you are fetching only Job_Name from the table and then in the loop you had try to read 14 cloumns.

I suggest you if your table Job_Category has 14 column then use query like
C#
string jobcat = "Select * from Job_Category where Industry_ID=1;";

hope you will solve your issue.
 
Share this answer
 
v3
Comments
Vani Kulkarni 13-Jun-12 4:51am    
My 5!
hi,

sorry for my english. i am not so fluent but i will try.

first thing , you have query

string jobcat = "Select Job_Name from Job_Category where Industry_ID=1;";
right?

so, in this, you have selected only one column, so the DataReader contains the multiple rows but each row having the one columns, it means that you cannot select more then one column from the reader. index of the datareader starts from 0(zero).
so you are gettiing the error at Label8.Text = Convert.ToString(reader[1]);//here above error occure..

Hope you get the solution.


Nilesh Chauhan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900