Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my Page I am fetching a value from the Database & filling the values in the DataTable and comparing that values with the String in the IF based upon the condition in the Query there will be no records fetched, so It is stuck in the IF condition and throws the “No row at Position 0” Exception not going to the Else part My code is,


C#
string mac = GetMac();
           string Qry = "Select VUserid,Password from passtable where VUserid='" + UserName.Text + "' and Flag='A'";
           string qry = "Select VUserid,Password from passtable where Flag='A'";
           string strq = "Select Mac_id from Sysinfo Where Appflag='A'";
           using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EvalCon"].ConnectionString))
           {
               try
               {

                   SqlCommand cmd = new SqlCommand(Qry, conn);
                   SqlCommand cmd1 = new SqlCommand(qry, conn);
                   SqlCommand cmd2 = new SqlCommand(strq, conn);
                   conn.Open();
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                   SqlDataAdapter dap = new SqlDataAdapter(cmd2);
                   DataTable dt = new DataTable();
                   DataTable dtt = new DataTable();
                   DataTable tab = new DataTable();
                   da.Fill(dt);
                   daa.Fill(dtt);
                   dap.Fill(tab);
                   for (int i = 0; i < tab.Rows.Count; i++)
                   {
                       for (int x = 0; x <= dtt.Rows.Count - 1; x++)
                       {
                           if (mac == tab.Rows[i]["Mac_id"].ToString() || tab.Rows.Count != 0)
                           {
                               if (UserName.Text == dtt.Rows[x]["VUserid"].ToString() && Password.Text == dtt.Rows[x]["Password"].ToString())
                               {
                                   Response.Redirect("~/Changepass.aspx");
                                   break;
                               }
                               else
                               {
                                   lblMessage.Visible = true;
                                   lblMessage.ForeColor = System.Drawing.Color.Red;
                                   lblMessage.Text = "Invalid Username or Password !!!";

                               }
                           }
                           else
                           {
                               lblMessage.Visible = true;
                               lblMessage.ForeColor = System.Drawing.Color.Red;
                               lblMessage.Text = "Invalid Access Point for Evaluation !!!";
                           }
                       }
                   }

               }
               finally
               {
                   conn.Close();
                   conn.Dispose();
               }
           }
Posted
Comments
NarasimhaMurthy 15-May-13 6:03am    
Before your if condition, check whether the rows count is > 0 or not.. then write else condition
Rajesh Kumar Sowntararajan 15-May-13 6:21am    
Thanks it Worked

1 solution

You can add a if condition at the beginning of the for loop
please try the following code


if(tab.Rows.Count>0)
{

for (int i = 0; i < tab.Rows.Count; i++)
{
for (int x = 0; x <= dtt.Rows.Count - 1; x++)
{
if (mac == tab.Rows[i]["Mac_id"].ToString() || tab.Rows.Count != 0)
{
if (UserName.Text == dtt.Rows[x]["VUserid"].ToString() && Password.Text == dtt.Rows[x]["Password"].ToString())
{
Response.Redirect("~/Changepass.aspx");
break;
}
else
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Invalid Username or Password !!!";

}
}
else
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Invalid Access Point for Evaluation !!!";
}
}
}

}
}
 
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