Question

Hello I am trying to use OleDb to connect database but when i want to read data from there and use Read() command after that

cmd.Parameters.Add("@name", TextBox1.Text);
cmd.Parameters.Add("@password", TextBox2.Text);
cmd.ExecuteNonQuery();
System.Data.OleDb.OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) 
{                      
    string istifadeciAd = (string)rdr.GetString(1);
    string istifadeciParol = (string)rdr.GetString(2);
}

in String istifadeciAd and istifadeciParol i got error for GetString because of IndexoutofRange. But don't we need to call GetString with column index?

Was it helpful?

Solution

you need to have more than 2 select columns in your select statement to get rdr.GetString(2)

something like below

select id, istifadeciAd, istifadeciParol from Table1 where name =? and password =?

note that index is zero-based

So if you only select istifadeciAd, istifadeciParol columns you need to read it as

string istifadeciAd = rdr.GetString(0);
string istifadeciParol = rdr.GetString(1);

And you don't need to cast the result to string, because it is returning string

I think you need to change the parameters adding code as well,

cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top