Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Codeproject Member ,

I am trying to populate data from sql in a C# windows form according to the selection made by the user in a combobox , though I have managed to successfully bind the combobox to the database , but I am not able to populate the data in the textboxes according to the selection of an item from combobox.
When I run program the error I am getting is--->
SqlException was unhandled by user code -"Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int."
My code is as following


//Database Table

1) id-int-notnull (primary key and autogenerate id)
2) name-varchar-notnull
3) mobile-numeric-notnull
4) address-varchar-notnull
private void Form2_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Database path.............");
        conn.Open();
        SqlCommand command = new SqlCommand("select * from customer", conn);
        SqlDataAdapter adp = new SqlDataAdapter(command);
        tbl = new DataTable();
        adp.Fill(tbl);
        comboBox1.DataSource = tbl;
     comboBox1.ValueMember = tbl.Columns[0].ColumnName;
 }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Database path...............");
        conn.Open();
       SqlCommand command = new SqlCommand("select name,mobile,address from customer where id=@id", conn);
    command.Parameters.AddWithValue("@id", comboBox1.SelectedValue.Tostring());
        SqlDataAdapter adp = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adp.Fill(ds, "customer");
        textBox1.Text = ds.Tables["customer"].Rows[1]["name"].ToString();
     textBox2.Text = ds.Tables["customer"].Rows[2]["mobile"].ToString();
        textBox3.Text = ds.Tables["customer"].Rows[3]["address"].ToString();
       }
Posted
Updated 1-Apr-14 9:05am
v2
Comments
Maciej Los 1-Apr-14 15:38pm    
--posted by mistake---

The problem is occurring because the parameter value is not getting set to what you are probably expecting. Your "id" column in the table is an int datatype and that is what you need to pass it. See code below:

int comboBoxChoice = Convert.ToInt32(comboBox1.SelectedValue.ToString());
command.Parameters.AddWithValue("@id", comboBoxChoice);


Now provided your combo box is getting a value, this should do the trick.
 
Share this answer
 
v2
Comments
manish7664 1-Apr-14 15:29pm    
Thanks for reply. I made changes as per your suggestion but it's showing error
Error -Input string was not in a correct format.
Richard C Bishop 1-Apr-14 15:31pm    
Ok, that means you are not getting the expected value upon your comboBox choice. What are the possible values available for it?
Maciej Los 1-Apr-14 15:36pm    
Sounds reasonable, +5!
Richard C Bishop 1-Apr-14 15:38pm    
I thought so, thank you Maciej!
manish7664 1-Apr-14 15:47pm    
Mr Richard guide me how can i remove Input string was not in a correct format error
There are known issues with ComboBox.Selected<Something> properties. Have a look here: ComboBox SelectedItem, SelectedValue, SelectedWhat???[^]
Quote:
SelectedValue - This property depends on the value of ValueMember.

If the property ValueMember is not Nothing the ComboBox will look for a member on SelectedItem with the name specified in ValueMember and return that. This is also the value displayed in the ComboBox.


I'd suggest you to use Int32.TryParse[^] method or better to deal with SelectedItem or SelectedIndex property.
 
Share this answer
 
Comments
Richard C Bishop 1-Apr-14 15:52pm    
Great addition. I suspected there was an issue with the value getting persisted from the combobox. +5
Maciej Los 1-Apr-14 15:57pm    
Thank you, Richard ;)
manish7664 2-Apr-14 2:51am    
Thanks Mr Maciej and Mr Richard. but int32.tryparse not taking here.
int comboxchoice = Int32.TryParse(comboBox1.SelectedItem.ToString());
command.Parameters.AddWithValue("@id", comboxchoice);
Maciej Los 2-Apr-14 3:00am    
Foolow the link and do proper convertion.
manish7664 2-Apr-14 7:44am    
now my code run successfully but i have one problem when i select id from combobox then "System.Data.DataRowView" showing in textbox instead of database value.
i request you that please tell me what mistake i am doing in code....
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
conn.Open();
SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
int comboxchoice;
Int32.TryParse(comboBox1.SelectedIndex.ToString(), out comboxchoice);
command.Parameters.AddWithValue("@id", comboxchoice);
SqlDataAdapter adp = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adp.Fill(ds, "customer");
textBox1.Text = comboBox1.SelectedValue.ToString();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900