Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i am getting this error

System.InvalidOperationException: Invalid attempt to read when no data is present.


i used this code:


SqlDataReader dr = command.ExecuteReader();

while (dr.Read())
{
if (dr["emailId"].ToString() !=txtenrEmail.Text)
{
Label1.Text = "Emailid already exists. Use another EmailId";
}
else
{
Label1.Text = "User details Inserted successfully";
}
}

String strQuery = "";
SqlConnection mySqlConnection = new SqlConnection(str);

strQuery += "update PersonalDetails set EnrolmentNo= ('CD' + ' " + dr["userid"].ToString() + " ') where userid = ' " + Convert.ToInt32(dr["userid"]) + " ' ";
SqlCommand com = new SqlCommand(strQuery, mySqlConnection);
mySqlConnection.Open();
com.ExecuteNonQuery();
mySqlConnection.Close();

con.Close();

please help me..............
Posted
Comments
KM Perumal 25-Mar-13 3:10am    
Please show ur whole event coding

1 solution

u have to check dr.HasRows in if, before update query
C#
if(dr.HasRows)
{
String strQuery = "";
SqlConnection mySqlConnection = new SqlConnection(str);
 
strQuery += "update PersonalDetails set EnrolmentNo= ('CD' + ' " + dr["userid"].ToString() + " ') where userid = ' " + Convert.ToInt32(dr["userid"]) + " ' ";
SqlCommand com = new SqlCommand(strQuery, mySqlConnection);
mySqlConnection.Open();
com.ExecuteNonQuery();
mySqlConnection.Close();
 
con.Close();
}
 
Share this answer
 
Comments
suma2212 25-Mar-13 2:38am    
Hi i used if(dr.HasRows)
{
}
but i got this error

Object cannot be cast from DBNull to other types.
please help me.......
Pallavi Waikar 25-Mar-13 2:46am    
i think u r facing this problem becouse u already get all values of dr using while and then ur trying to update...best way store this values in two variable in while and use them for update..i.e string userid =dr["userid"].ToString() ; when get match in while
suma2212 25-Mar-13 3:03am    
ya i got it.thanx for ur help......

is this code correct?

while (dr.Read())
{
if (dr["emailId"].ToString() !=txtenrEmail.Text)
{
Label1.Text = "User details Inserted successfully";
}
else
{
Label1.Text = "Emailid already exists. Use another EmailId";

}
}

if that emailid is already exists i got this message
Label1.Text = "Emailid already exists. Use another EmailId";
and emailid not exists also i am getting same message..


please help me..
Pallavi Waikar 25-Mar-13 3:33am    
best way search in select query cmd= "select * from urTablename where emailId= '"+txtenrEmail.Text+"'"
and use if instead of while
dr=cmd..ExecuteReader();
if (dr.Read())
{
if (dr["emailId"].ToString() !=txtenrEmail.Text)
{
string uid=dr["userid"].ToString();
string emailid=dr["emailId"].ToString();
Label1.Text = "User details Inserted successfully";
}
else
{
Label1.Text = "Emailid already exists. Use another EmailId";
}
}
KM Perumal 25-Mar-13 3:11am    
Try sql Adapter

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