this is my code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=DayanaP;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand test = new SqlCommand("select * from maindata where userid='"+textBox1.Text+ "' and password='" + textBox2.Text + "' ", con);
SqlDataReader dr = test.ExecuteReader();
int count = 0;
while (dr.Read())
{
count++;
}
if (count == 1)
MessageBox.Show(dr.GetString(0));
else
{
MessageBox.Show("123");
}
con.Close();
}
}
}
and that is my database maintable which I used in code
when I enter name and password I hope to see a message box which show the user first name, but I get this error:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Data.dll
Additional information: Invalid attempt to read when no data is present.
How can I fix this?
You have already consumed the reader
this is what you need
or shorter
or