For Loop Invalid attempt to read when no data is present

  • Thread starter Thread starter Mary21student
  • Start date Start date
M

Mary21student

Guest
Database

john smith 9234839
ken williams 3293432
mary davis 9234323

jan will 5832934

kc bing 3892343

//////////////////////////////

This is a multiline Text Box wanting to input more then one item at a time

input Box 9234839,
3293432 ,

5832934,

3892343

Button
//////////////////////////////////

private void submitbutton_Click(object sender, EventArgs e)
{

string connString = ConfigurationManager.ConnectionStrings["data"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))


{
for (int i = 1; i <= Contact.Lines.Length; i++)
{


con.Open();

SqlCommand cmd = new SqlCommand("NameAddress", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;


cmd.Parameters.AddWithValue("@NameAddress", SqlDbType.VarChar).Value = Contact.Text.Trim();


SqlDataReader dr = cmd.ExecuteReader();
--------------------- dr.Read(); //'Invalid attempt to read when no data is present.'

textBox1.Text = dr[1].ToString() + " " + dr[2].ToString() + " " + dr[3].ToString() + " " + i.ToString();

con.Close();
}
}
}

This will work if I put one value in at a time but if I put 2 values or more I will get 'Invalid attempt to read when no data is present.

what I want it to do is print out like this below, I am just not sure what I am doing wrong?

john smith 9234839 1
ken williams 3293432 2
mary davis 9234323 3

jan will 5832934 4

kc bing 3892343 5

Continue reading...
 
Back
Top