Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..


SQL
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();
               adapter.Fill(dt);
               resultDG.ItemsSource = dt.DefaultView;


selectedrow mean this>>
SQL
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
Updated 8-May-12 21:03pm
v3
Comments
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 + "'";

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

}
finally
{
MessageBox.Show("ffff", "ff");
cnn.Close();
}
}

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).
 
Share this answer
 
Comments
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();
adapter.Fill(dt);
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].
'" & val (selectedrow.text) & "'"
 
Share this answer
 

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