Error in SqlCommand

F

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.Int);
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
 
O

oj

You need to use DBNull.

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

Fox

Thank you so much
oj said:
You need to use DBNull.

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

--
-oj


Fox said:
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.Int);
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top