Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlCommand cmd2 = new SqlCommand("insertProducts_TBL", con2);
                        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd2.Parameters.AddWithValue("@Product_name", txtProduct_name.Text);
                        cmd2.Parameters.AddWithValue("@FK_catId", comboBoxFK_catid.SelectedValue);
Posted
Updated 5-Apr-15 22:45pm
v2
Comments
Sascha Lefèvre 6-Apr-15 4:36am    
Where's the problem?
Member 11280947 6-Apr-15 4:39am    
The code not running with SelectedValue
Sascha Lefèvre 6-Apr-15 4:44am    
What do you mean by "not running"? Does it not insert the correct value or does it result in an error message (which?) ?
Member 11280947 6-Apr-15 4:56am    
The mesage is:

no mapping exists from object type System.Data.DataRowViwe to a Known managed provider native type


HI, I think your field 'FK_catId' is int that why error could arise ,so you have to type cast the string value of combobox into integer like this below,

Convert.Toint32(comboBoxFK_catid.SelectedValue)


Full code below:

SqlCommand cmd2 = new SqlCommand("insertProducts_TBL", con2);
                        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd2.Parameters.AddWithValue("@Product_name", txtProduct_name.Text);
                        cmd2.Parameters.AddWithValue("@FK_catId", Convert.Toint32(comboBoxFK_catid.SelectedValue));
 
Share this answer
 
Comments
Member 11280947 6-Apr-15 5:10am    
cmd2.Parameters.AddWithValue("@FK_catId",Convert.ToInt32( comboBoxFK_catid.SelectedValue));


The Error Message :

Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'.
deepankarbhatnagar 6-Apr-15 5:13am    
Could you please display your full code
use
C#
SelectedItem
instead of
C#
selectedValue
 
Share this answer
 
Comments
Member 11280947 6-Apr-15 5:11am    
The mesage is:

no mapping exists from object type System.Data.DataRowViwe to a Known managed provider native type
Brahmmam 14-Apr-15 2:11am    
follow below link
http://stackoverflow.com/questions/18929481/error-no-mapping-exists-from-object-type-system-data-datarowview-to-a-known-ma
Your error message indicates that comboBoxFK_catid.SelectedValue contains a DataRowView. You have to know which column of that row you want to insert into your database - I indicate it here with "columnName", replace that with the actual column name:
C#
cmd2.Parameters.AddWithValue(
    "@FK_catId",
    ((DataRowView)comboBoxFK_catid.SelectedValue)["columnName"]);
 
Share this answer
 
Comments
Member 11280947 6-Apr-15 5:18am    
Error Message:

catId is neither a DataColumn nor a DataRelation for table .




cmd2.Parameters.AddWithValue("@FK_catId", ((DataRowView)comboBoxFK_catid.SelectedValue)["catId"]);

catId : From another Table
Sascha Lefèvre 6-Apr-15 5:25am    
Looks like there is no column with that name "catId". Please check your column names.
Try text property of Combobox

Full code here:-
C#
SqlCommand cmd2 = new SqlCommand("insertProducts_TBL", con2);
                        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd2.Parameters.AddWithValue("@Product_name", txtProduct_name.Text);
                        cmd2.Parameters.AddWithValue("@FK_catId", Convert.Toint32(comboBoxFK_catid.text));
 
Share this answer
 
v3

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