Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
at line drr = dt.Rows[i]
if this happening i want to move on next row of table and if row is there that row's value should display


//code is
public partial class WebForm9 : System.Web.UI.Page
    {
       
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        DataRow drr;
       public string ans_tea;
        public string ans_stu;
        SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False");
         static int i = 0;
        public void Page_Load(object sender, EventArgs e)
        {
           con.Open();
            string tea_id = TextBox2.Text;
            string s1 = "select student_id from Student where user_name='" + Session["sid"] + "'";
            SqlCommand cmd = new SqlCommand(s1, con);
        SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Label4.Text = dr[0].ToString();
                dr.Close();
            }
    }
      public void Button2_Click(object sender, System.EventArgs e)
        {
            string tea_id = TextBox2.Text;
            cmd = new SqlCommand("SELECT * from Question WHERE Teacher_id=@tea_id", con);
            
            cmd.Parameters.AddWithValue("@tea_id", tea_id);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
         da.Fill(dt);//at this line getting Exception so how can i avoid getting this Exception
            drr = dt.Rows[i];
             if (i == (dt.Rows.Count - 1))
            {
                Response.Write("Last record !");
            }
            else
            {
               i++;
             
            }
              Label3.Text = Convert.ToString(drr[0]);//for displaying question_id
                Label2.Text = Convert.ToString(drr[2]);//for displaying question
            con.Close();
       }
Posted
Comments
CoderPanda 23-Mar-14 6:09am    
Are you sure you are getting any records from the DB?
CHill60 23-Mar-14 6:36am    
And you still have the bug from yesterday in Button2_Click
I guess the query is not returning any row.

You should modify your line of code code like this:
C#
if (i >= dt.Rows.Count))
{
Response.Write("There are no record !");
con.Close();
return;
}         
drr = dt.Rows[i]; 
//... your old code!        
 
Share this answer
 
v2
static int i = 0;
Why do you do that? The cause of the problem is likely based here: you assume some value for i which is no more valid due to another instance of your class being processed at the same time or i being changed in a different function etc. In addition to Paul's answer you'd better repair that too.
 
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