Click here to Skip to main content
15,924,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in coding of displaying panel error occure:-
my code is:-

C#
protected void lbcategory_SelectedIndexChanged(object sender, EventArgs e)
{

    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 jobcategory = "Select Job_Name from Job_Category where Industry_ID=1;";

    SqlCommand cmd4 = new SqlCommand(jobcategory,sqlCon);
    SqlDataReader reader = cmd4.ExecuteReader();
    if (reader.HasRows)
    {
        Label6.Text = Convert.ToString(reader[0]);
        Label8.Text = Convert.ToString(reader[1]);
        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;
    }


my error is:-Invalid attempt to read when no data is present....
so i cant understand where is error????plz try to solve it...


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 12-Jun-12 18:32pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-12 0:39am    
Just a note: I cannot imagine that a person who would think it's OK to write a code line like "Label6.Text = Convert.ToString(reader[0]);" 14 times (!) could possibly put something right. First, think about what you are writing and how you are doing it, have a clear idea and a plan of what you want to do and how, right it (first step would be throwing out the code you show and never remember it), and then, after you have clear understanding what each like does and why, test it and fix possible bugs. It would be much easier...
--SA

1 solution

It's pretty simple:
SQL
SELECT Job_Name FROM Job_Category WHERE Industry_ID=1
Is an SQL query which returns an unknown number of rows (this depends on how many rows in your DB are in the appropriate industry), each of which has a single column (the Job Name).
C#
Label6.Text = Convert.ToString(reader[0]);
Label8.Text = Convert.ToString(reader[1]);
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]);
This attempts to read 14 columns from a single row which contains only one column. This will throw an error when it gets the the second line.
But it isn't getting that far, because you are not reading any rows into the SqlReader from the DB.
You need to call the SqlReader.Read method before you use each row:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }
 
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