Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 24-Mar-13 20:12pm
Comments
KM Perumal at 25-Mar-13 3:10am
   
Please show ur whole event coding

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

u have to check dr.HasRows in if, before update query
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();
}
  Permalink  
Comments
suma2212 at 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 at 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 at 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 at 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 at 25-Mar-13 3:11am
   
Try sql Adapter
suma2212 at 25-Mar-13 3:16am
   
i want to read 2 values userid and emailid. so i used sql reader.... please check above code...
suma2212 at 25-Mar-13 3:48am
   
sorry for the trouble i am using this
 
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 check it once......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 195
3 DamithSL 195
4 Richard MacCutchan 178
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100