Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
  cmd = new SqlCommand("select productid from ProductDetails where productname='" + ddlproductid.SelectedItem.ToString() + "'", con);
                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    proid = dr[0].ToString();
                }
               
                dr.Close();
                cmd.Dispose();

I had a coloumn to insert
 SqlParameter ProductID = new SqlParameter("ProductID", int.Parse(proid.ToString())); 

The Question is:
how to set the int.Parse(proid.ToString()) value as a NULL while insertion. the db datatype is integer
if i pass the value to this parameter proid.ToString() its working 
in some cases i dosen't pass any values it throws me the conversionerror instead of that must be set as a NULL in DB
Posted
Updated 12-Jul-13 1:44am
v4
Comments
Sushil Mate 12-Jul-13 7:13am    
what does it mean by this? null when? simply null will work I guess.
kesav prakash 12-Jul-13 7:41am    
if i pass the value to this parameter proid.ToString() its working
in some cases i dosen't pass any values it throws me the conversionerror
ArunRajendra 12-Jul-13 9:28am    
Yes, thats why I told you dont convert just use SqlParameter ProductID = new SqlParameter("ProductID",proid);

Try this way and dont parse as it will throw exception. ? will make a varible nullable.

C#
int? proid=null;
SqlParameter ProductID = new SqlParameter("ProductID",proid);
 
Share this answer
 
Comments
kesav prakash 12-Jul-13 7:39am    
if dosent pass value its k we can do like this in some cases
if i pass value na i take it as string i need conversion there how to solve it
Use Convert.ToInt32 Method (String)[^]

In your case it would be like

C#
SqlParameter ProductID = new SqlParameter("ProductID", Convert.ToInt32(proid)); 
 
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