By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,416 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,416 IT Pros & Developers. It's quick & easy.

Error in SqlCommand

P: n/a
Fox
Cannot accept null value in parameter list
My SQL Server can accept null value on field CustomerName
When I run this example

SqlConnection nwindConn = new SqlConnection("Data Source=(local);Integrated
Security=SSPI;Initial Catalog=northwind");

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CustomerName
FROM Customer", nwindConn);
nwindConn.Open();
SqlCommand insertCMD = new SqlCommand("insert into
Customer(CustomerID,CustomerName) values
(@CustomerID,@CustomerName)",nwindConn);
insertCMD.Parameters.Add("@CustomerID",SqlDbType.I nt);
insertCMD.Parameters.Add("@CustomerName",SqlDbType .Char,30);
insertCMD.Parameters["@CustomerName"].Value =null;
insertCMD.Parameters["@CustomerID"].Value =15;

try
{
insertCMD.ExecuteNonQuery();
}
catch (Exception em)
{
MessageBox.Show(em.ToString());
}

The error message : expects parameter @CustomerName which was not supplied
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
oj
You need to use DBNull.

e.g.
insertCMD.Parameters["@CustomerName"].Value =DBNull.Value;

--
-oj
"Fox" <sdasd> wrote in message news:un**************@TK2MSFTNGP15.phx.gbl...
Cannot accept null value in parameter list
My SQL Server can accept null value on field CustomerName
When I run this example

SqlConnection nwindConn = new SqlConnection("Data
Source=(local);Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CustomerName
FROM Customer", nwindConn);
nwindConn.Open();
SqlCommand insertCMD = new SqlCommand("insert into
Customer(CustomerID,CustomerName) values
(@CustomerID,@CustomerName)",nwindConn);
insertCMD.Parameters.Add("@CustomerID",SqlDbType.I nt);
insertCMD.Parameters.Add("@CustomerName",SqlDbType .Char,30);
insertCMD.Parameters["@CustomerName"].Value =null;
insertCMD.Parameters["@CustomerID"].Value =15;

try
{
insertCMD.ExecuteNonQuery();
}
catch (Exception em)
{
MessageBox.Show(em.ToString());
}

The error message : expects parameter @CustomerName which was not supplied

Nov 16 '05 #2

P: n/a
Fox
Thank you so much
"oj" <no**********@home.com> glsD:eC****************@TK2MSFTNGP12.phx .gbl...
You need to use DBNull.

e.g.
insertCMD.Parameters["@CustomerName"].Value =DBNull.Value;

--
-oj
"Fox" <sdasd> wrote in message
news:un**************@TK2MSFTNGP15.phx.gbl...
Cannot accept null value in parameter list
My SQL Server can accept null value on field CustomerName
When I run this example

SqlConnection nwindConn = new SqlConnection("Data
Source=(local);Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CustomerName
FROM Customer", nwindConn);
nwindConn.Open();
SqlCommand insertCMD = new SqlCommand("insert into
Customer(CustomerID,CustomerName) values
(@CustomerID,@CustomerName)",nwindConn);
insertCMD.Parameters.Add("@CustomerID",SqlDbType.I nt);
insertCMD.Parameters.Add("@CustomerName",SqlDbType .Char,30);
insertCMD.Parameters["@CustomerName"].Value =null;
insertCMD.Parameters["@CustomerID"].Value =15;

try
{
insertCMD.ExecuteNonQuery();
}
catch (Exception em)
{
MessageBox.Show(em.ToString());
}

The error message : expects parameter @CustomerName which was not
supplied


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.