Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
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();
               adapter.Fill(dt);
               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 19:38pm
Edited 8-May-12 21:03pm
v3
Comments
00210021 at 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();
}
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

'" & val (selectedrow.text) & "'"
  Permalink  
Rate this: bad
good
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).
  Permalink  
Comments
TageTage at 9-May-12 5:38am
   
ok,, i checked it and it has a value..should i convert it to string
walterhevedeich at 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 at 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 at 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 at 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 at 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 at 10-May-12 22:28pm
   
Thank you so much walterhevedeich,,

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

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 330
2 _Amy 115
3 Andreas Gieriet 100
4 Rob Philpott 90
0 OriginalGriff 7,107
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
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