Reputation: 54
I need the DropDownList ddlReceipt to display the value in the database. my current code is below.
I have a feeling that my code is wrong on ddlReceipt.SelectedValue = "toa_receipt"; but I am not for sure.
foreach (GridViewRow row in gvCreditCards.Rows)
{
DropDownList ddlReceipt = row.FindControl("ddlReceipt") as DropDownList;
string id = row.Cells[0].Text;
SqlConnection con2 = new SqlConnection(conStringTOA);
SqlDataAdapter sda2 = new SqlDataAdapter();
SqlCommand cmd2 = new SqlCommand("SELECT toa_receipt FROM toa_CreditCardStatement WHERE id = '" + id + "'");
cmd2.Connection = con2;
con2.Open();
ddlReceipt.SelectedValue = "toa_receipt";
con2.Close();
}
Upvotes: 1
Views: 59
Reputation: 9470
You missed a good part of code. You have to read data from DB (this is why you create SqlCommand
).
//your code
//SqlDataAdapter sda2 = new SqlDataAdapter();//no need
SqlCommand cmd2 = new SqlCommand("SELECT toa_receipt FROM toa_CreditCardStatement WHERE id = @id", con2);
//It is very insecure to concatenate value into SQL string
//cmd2.Connection = con2;
//Use parameter instead
cmd2.Parameters.Add("@id",DbType.VarChar).Value = id;
con2.Open();
using(SqlDataReader r = cmd2.ExecuteReader()){
if(r.Read()){
ddlReceipt.SelectedValue = (string)r["toa_receipt"];
}
r.Close();
}
con2.Close();
Upvotes: 1