Last post Jul 25, 2011 10:13 AM by Mastan Oli
Jul 19, 2011 08:49 PM|imfarrukhkhan|LINK
im new in a c#
i made a stored procedure on sql and its work good but the problem on passing the parameter on c#
i wanna know how to pass null Value from c# ............?
please hepl Me Quicj
Jul 19, 2011 08:56 PM|AceCorban|LINK
Try something like this:
cmd.Parameters.Add(new SqlParameter("@something", DBNull.Value));
Jul 19, 2011 09:02 PM|imfarrukhkhan|LINK
Thanx 4 quick rep ..........
but dear i did it but its not work properly and DBNull.Value is show the error its take a string type of data
i search it on diffrent web sites but i failed
Do u know another way ?
Jul 19, 2011 09:19 PM|perper|LINK
command.Parameters.Add("@YourParameter", SqlDbType.Nvarchar).Value = DBNull.Value ;
hope this helps
Jul 19, 2011 09:20 PM|AceCorban|LINK
Well, DBNull.Value is how you represent SQL's NULL value. It could be that either your database columns aren't set up to be nullable. Additionally, it depends on how you are adding the parameters. It's honestly been a while since I've done direct ADO,
so I don't think the exact code I sent you accepts anything but strings. I'm pretty sure you need to establish a type for the SQL Parameter being added. How are you currently adding parameters to the command?
Jul 19, 2011 09:27 PM|ajayg|LINK
Well, you can define the parameter used in your stored procedure to be defaulted to Null, and then just don't supply value from the business layer or UI.
create procedure test
@param1 as int = null
Jul 19, 2011 09:32 PM|imfarrukhkhan|LINK
i initilize the paramter as null in the stored Procedure but m faceing the problem in c# wen i passiing the value in the parameter
Jul 19, 2011 09:38 PM|imfarrukhkhan|LINK
no dear its not work
Jul 20, 2011 03:24 PM|ajayg|LINK
If you have initialized your parameter to null, you donot have to pass the value from UI. So the command object needs to execute the procedure without passing this null parameter.
Jul 25, 2011 06:51 AM|Damon Liu - MSFT|LINK
You can refer to this sample :http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_24016226.html
Jul 25, 2011 06:53 AM|Ved Prakash Nunia|LINK
Create Proc ABC
@Title varchar(500) = ' ',
@Sector varchar(500) = ' ',
@Location varchar(100) =' ',
Jul 25, 2011 09:48 AM|rakeshgali|LINK
<asp:TextBox ID="TextBox1" runat="server" Text=" "></asp:TextBox>
Jul 25, 2011 10:13 AM|Mastan Oli|LINK
for passing null parameter values to stored procedure, pls do following items...
stored procedure, should be accept nullable parameter like
CREATE PROCEDURE ABC(@ID Int = NULL)
use nullable object or create method which accept null parameter like
void ABC(int? myParameter)
using (SqlConnection myConn = new SqlConnection(strConnection))
SqlCommand myComm = new SqlCommand("ABC", myConn);
myComm.CommandType = CommandType.StoredProcedure;
SqlDataReader myReader = myComm.ExecuteReader();
call it from your code, like