Click here to Skip to main content
12,401,049 members (61,753 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-server-2005 C# ASP.NET , +
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 19:12pm
Comments
KM Perumal 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 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
suma2212 25-Mar-13 3:16am
   
i want to read 2 values userid and emailid. so i used sql reader.... please check above code...
suma2212 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2016
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