Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table 'clientinfo'. In this 'CartTxNo' is a column with int datatype.
I want to assign its value value to a variable 'v_CartTxno1'. I get the error: 'Invalid attempt to read when no data is present' at line ' int v_CartTxNo1 = reader.GetInt32(0);'
How to rectify my code?

SqlCommand cmd = new SqlCommand("select CartTxNo from clientinfo where ClientPhone= '" + v_MobileNo + "'", con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@v_mem_id1";
param.Value = TextBox1.Text;
cmd.Parameters.Add(param);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
int v_CartTxNo1 = reader.GetInt32(0);
Posted

1 solution

You need to start reading the reader first.

For e.g.
if (reader.HasRows)
{
  while (reader.Read())
  {
    int v_CartTxNo1 = reader.GetInt32(0);
    break; 
  }
}
 
Share this answer
 
v3

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