Reputation: 25
Here is my code
public void gridshow() {
sc.Open();
//int custtid ;
//custtid = (int)cidshow.SelectedItem;
SqlDataAdapter da = new SqlDataAdapter("select Cust_id , Cust_name from Cust_master where Cust_id = '" + cidshow.SelectedItem + "'", sc);
DataSet ds = new DataSet();
da.Fill(ds,"Cust_master");
custshow.DataSource = ds.Tables[0];
sc.Close();
}
Error raised is
(Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int.)
Upvotes: 0
Views: 38
Reputation: 1806
If your cidshow
is a databound combobox, SelectedItem
returns a DataRowView. You will have to extract your ID field from that object
....
int custtid ;
DateGridView custdgv = cidshow.SelectedItem;
int custtid = Convert.ToInt(custdgv["customerId"]);
....
Upvotes: 0
Reputation: 66439
If cidshow.SelectedItem
is a DataRowView
, you can't use it the way you are.
Try accessing just the value you need, using something like:
... Convert.ToString(((DataRowView)cidshow.SelectedItem)["customerId"]) ...
Also, look into parameterizing your queries. It's safer, and it makes your query easier to read and maintain in the future.
var da = new SqlDataAdapter(
"select Cust_id , Cust_name from Cust_master where Cust_id = @custId", sc);
da.SelectCommand.Parameters.AddWithValue("@custId", yourCustomerId);
Upvotes: 2