Reputation: 217
for (int i = 0; i < final_query.Length; i++)
{
con.Open();
SqlCommand cmd=new SqlCommand("SELECT * fROM TableFFF WHERE Data="+final_query[i]);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string PatientName = (string)rdr[" Data "];
}
}
I got that error
ExecuteReader: Connection property has not been initialized.
on this line
SqlDataReader rdr = cmd.ExecuteReader();
How can i fix it?
Upvotes: 0
Views: 149
Reputation: 216293
You need to pass the Connection to the SqlCommand
SqlCommand cmd=new SqlCommand("SELECT * fROM TableFFF WHERE Data="+final_query[i], con);
But there are other errors:
Use and reuse Parameters, not string concatenation
con.Open();
SqlCommand cmd=new SqlCommand("SELECT * fROM TableFFF WHERE Data=@data", con);
cmd.Parameters.AddWithValue("@data", 0); // Supposing final_query[i] is an Int32
for (int i = 0; i < final_query.Length; i++)
{
cmd.Parameters["@data"].Value = final_query[i];
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string PatientName = (string)rdr[" Data "];
}
}
And it is still not clear what you are supposing to do with the PatientName after the first round
Upvotes: 4
Reputation: 1747
You arnt telling the sqlcommand to use the connection con try
SqlCommand("SELECT * fROM TableFFF WHERE Data="+final_query[i], con);
Though it seems this isnt very good coding, and could use some redesign....
Upvotes: 0