Click here to Skip to main content
13,291,084 members (58,073 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello all..

[Conversion failed when converting the varchar value 'System.Data.DataRow' to data type int]

i'm stuck with that error..
this is my code..

adapter = new SqlDataAdapter("SELECT result_date,student_ID,test_ID,correct_answer,false_answer,mark FROM RESULTS WHERE student_ID='" + selectedrow + "'", connect);
           dt = new DataTable();
               resultDG.ItemsSource = dt.DefaultView;

selectedrow mean this>>
private void stdlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
           DataRowView selectedDRow = (DataRowView)stdlist.SelectedItem;
           selectedrow = selectedDRow.Row;

why this error hppend.?
how can i fixi't
Posted 8-May-12 20:38pm
Updated 8-May-12 22:03pm
00210021 15-Oct-12 13:48pm
private void cmbemployee_SelectedIndexChanged(object sender, EventArgs e)
object varindex = cmbCustomer.SelectedValue;
//textBox1.Text = varindex.ToString();
if (true )
SqlConnection cnn = new SqlConnection(clsmain.cnnstr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "select c_id from tbl_customer where c_id = '" + varindex + "'";

if (cnn.State != ConnectionState.Open)
object respon = cmd.ExecuteScalar();
//if (respon.Read())
// {
lblfamilyCustomer1.Text = respon.ToString();

MessageBox.Show("ffff", "ff");
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

selectedrow is a System.Data.DataRow[^] type, which means that you can't directly concatenate it to a string. Try to check the right index of the value that you want from selectedrow (selectedrow["student_ID"] probably).
TageTage 9-May-12 5:38am
ok,, i checked it and it has a value..should i convert it to string
walterhevedeich 9-May-12 5:44am
You mean selectedrow["student_ID"] has a value? In that case, yes, you should convert it first to string, since its data type is initially object.
TageTage 10-May-12 2:33am
it's still show up..this's what i wrote..
adapter = new SqlDataAdapter("SELECT result_date,student_ID,test_ID,mark FROM RESULTS WHERE student_ID='" + selectedrow.ToString()+ "'", connect);
dt = new DataTable();
resultDG.ItemsSource = dt.DefaultView;
walterhevedeich 10-May-12 3:09am
Your code is still not correct. selectedrow.ToString() will still return "System.Data.DataRow". DataRow objects can be compared to rows on the table. They also have columns/fields, and can be accessed either by index, or column name (ex: selectedrow["student_ID"] or selectedrow[0]). What I mean earlier was, you need to convert the field of the row to string, not convert the whole row itself.
TageTage 10-May-12 6:28am
ok i did..and there's no exception show up.It's work fine with my datagrid i mean when i selected student id the datagrid is populated with data but my comboBox is populated with [System.Data.DataRow].
walterhevedeich 10-May-12 21:45pm
That's a different issue, but the fix can be the same. Since you are now familiar with how values on a DataRow are accessed, you can check how your combobox is being populated. You might be populating it again with the the whole DataRow instead of just a specific column on it.
TageTage 10-May-12 22:28pm
Thank you so much walterhevedeich,,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

'" & val (selectedrow.text) & "'"

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100