Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL2000 C# ASP.NET SQL-Server , +
  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 12-Jul-13 2:04am
Edited 12-Jul-13 2:44am
v4
Comments
Sushil Mate at 12-Jul-13 7:13am
   
what does it mean by this? null when? simply null will work I guess.
kesav prakash at 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 at 12-Jul-13 9:28am
   
Yes, thats why I told you dont convert just use SqlParameter ProductID = new SqlParameter("ProductID",proid);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this way and dont parse as it will throw exception. ? will make a varible nullable.
 
int? proid=null;
SqlParameter ProductID = new SqlParameter("ProductID",proid);
  Permalink  
Comments
kesav prakash at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use Convert.ToInt32 Method (String)[^]
 
In your case it would be like
 
SqlParameter ProductID = new SqlParameter("ProductID", Convert.ToInt32(proid)); 
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 12 Jul 2013
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