Click here to Skip to main content
15,885,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Anyone Please help

i have 2 form . in first form have two textbox id and name and one save button and second form have one combo and one textbox. i want, when form-2 loaded then database table id's fill in form -2 combobox and if i select any id in combobox then releted id name display in form 2 textbox.

i tried this...but one error is coming
ERROR->Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type
 // FORM-1 CODE
 private void cmdsave_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
            conn.Open();
            SqlCommand command = new SqlCommand();
            command.CommandText = "insert into table1(name)values('" + textBox2.Text + "')";
            command.Connection = conn;
            command.ExecuteNonQuery();
            MessageBox.Show("successfully Saved");
         }
 
//FORM-2 CODE

 private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the '_test2_mdfDataSet.Table1' table. You can move, or remove it, as needed.
            this.table1TableAdapter.Fill(this._test2_mdfDataSet.Table1);
             }
 
        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='"+comboBox1.SelectedItem+"'", conn);
            SqlDataAdapter adp = new SqlDataAdapter(command);
            tbl = new DataTable();
            adp.Fill(tbl);
            textBox1.Text = tbl.Rows[1].ToString();
}
Posted
Updated 31-Mar-14 9:16am
v2
Comments
Richard C Bishop 31-Mar-14 15:18pm    
Where does this fail at? Your insert statement will definitely fail, does it fail anywhere else?
guru14 31-Mar-14 15:26pm    
my insert statement correct..actually id is auto generated
(identity increment=1)
ZurdoDev 31-Mar-14 16:53pm    
1. Your insert statement is open to SQL injections which means someone could do whatever they wanted to with your database through this form. That may be what Richard was referring to.
2. Where is the error? And what is the complete error?
guru14 31-Mar-14 23:35pm    
error coming here -adp.Fill(tbl); and it's a complete error -
Error type -> SqlException was Unhandled
Error -> Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type
ZurdoDev 1-Apr-14 6:29am    
That isn't the complete error. You can even see that it isn't a complete sentence. No matter, Solution 1 has the answer.

1 solution

That is because you are assigning comboBox1.SelectedItem to id in query. That is actually a object, but you should be providing the value.

Use SelectedValue Property[^] to get the value.
C#
SqlCommand command = new SqlCommand("select name from table1 where id='" + comboBox1.SelectedValue +"'", conn);

But you should always use Parameterized query to avoid SQL Injection issues.
C#
SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
command.Parameters.AddWithValue("@id", comboBox1.SelectedValue);
 
Share this answer
 
Comments
guru14 1-Apr-14 4:22am    
Thanks for reply. I made changes in my code as per your suggestion and run program but one error is coming in adp.fill(tbl);
Error-No mapping exists from object type System.Data.DataRowView to a known managed provider native type.
Okay, seems like the ComboBox is bound from a DataTable, so its type is System.Data.DataRowView. If you've neglected to set the ValueMember for one or more of those ComboBoxes the the SelectedValue will return the same as the SelectedItem, i.e. a DataRowView.

So now try like below...

SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
command.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());

While debugging check what is the value of comboBox1.SelectedValue.ToString().
guru14 1-Apr-14 8:35am    
if i used this command.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
then Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int error coming if i not use Tostring(); inside of query then No mapping exists from object type System.Data.DataRowView to a known managed provider native type. coming
now what should i do sir ..
As I said, did you check what is the value of comboBox1.SelectedValue.ToString() while debugging? Is the value correct?
guru14 1-Apr-14 8:56am    
Value of combobox.selectedvalue.tostring is System.Data.DataView.

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