Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
protected void btnView_Click(object sender, EventArgs e)
       {

               BA.DeptNo = int.Parse(txtno.Text);
              BA.option = "select";
               DataTable dt = BA.viewRecords();

                 txtname.Text = Convert.ToString( dt.Rows[0]["dname"]);
                 
                txtloc.Text = Convert.ToString(dt.Rows[0]["loc"]);

                if (dt != null)
                {
                    Response.Write("The Records are:");
            }
           else
           {
               Response.Write("The deptno is invalid");
           }

       }
Posted
Updated 28-May-14 3:18am
v3

I suspect your code doesn't look like that - Visual studio would have tidied up the indentation for you - so it will really depend on what your real, executing code looks like: I don't think you have the
C#
dt.Rows.Count>0
test in the "live" code, as the only way it should get to the row access is if there is a row at position 0.

So start by checking you code, and if you think it's all correct, use VS to auto tidy the indentation using CTRL+K CTRL+D - if it compiles ok then it will work - and show us that code,
or use the debugger to examine the DataTable immediately prior to the if test.
 
Share this answer
 
Comments
Member 10532069 24-May-14 5:44am    
Above I changed the code even though its giving me the error.
Add validations as below
C#
protected void btnView_Click(object sender, EventArgs e)
{

    BA.DeptNo = int.Parse(txtno.Text);
    BA.option = "select";
    DataTable dt = BA.viewRecords();
    if (dt!=null && dt.Rows.Count >0)
    {
        txtname.Text = Convert.ToString(dt.Rows[0]["dname"]);

        txtloc.Text = Convert.ToString(dt.Rows[0]["loc"]);

    }
    else
    {
        Response.Write("The deptno is invalid");
    }

}
 
Share this answer
 
use below validation before your line which is throw error :
if (dt!=null && dt.Rows.Count >0)
 
Share this answer
 
C#
protected void btnView_Click(object sender, EventArgs e)
{
 
    BA.DeptNo = int.Parse(txtno.Text);
    BA.option = "select";
    DataTable dt = BA.viewRecords();
    // Before using table object, should check of null reference
    if (dt != null && dt.Rows.Count >0)
    {
        Response.Write("The Records are:");
   //While converting object, there should be check of null reference.
        txtname.Text = (!string.IsNullOrWhiteSpace(dt.Rows[0]["dname"]))?     Convert.ToString(dt.Rows[0]["dname"]) : String.Empty;
 
        txtloc.Text = (!string.IsNullOrWhiteSpace(dt.Rows[0]["loc"]))?Convert.ToString(dt.Rows[0]["loc"]): String.Empty;
 
    }
    else
    {
        Response.Write("The deptno is invalid");
    }
 
}
 
Share this answer
 
v2

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