1

Using SQL Server 2008, WinForms C# .NET 4.5, Visual Studio 2012.

I have a query that currently updates a table with some information from a GridView.

Below is the code that calls the stored procedure:

public void UpdateMain(string part, int? pareto)
{
  try
  {
    using (SqlConnection AutoConn = new SqlConnection(conn32))
    {
      AutoConn.Open();
      using (SqlCommand InfoCommand = new SqlCommand())
      {
        using (SqlDataAdapter infoAdapter = new SqlDataAdapter(InfoCommand))
        {
          InfoCommand.Connection = AutoConn;
          InfoCommand.CommandType = CommandType.StoredProcedure;
          InfoCommand.CommandText = "dbo.updateMain";
          InfoCommand.Parameters.AddWithValue("@part", part);
          InfoCommand.Parameters.AddWithValue("@pareto", pareto);
          InfoCommand.CommandTimeout = 180;

          InfoCommand.ExecuteNonQuery();
        }
      }
    }
  }
  catch (Exception e)
  {
    //MessageBox.Show("Error in connection :: " + e);
  }
}

And here's the SQL:

ALTER PROCEDURE [dbo].[updateMain]
    @part varchar(255),
    @Pareto int
as
    UPDATE dbo.ParetoMain 
    SET NewPareto = @Pareto 
    WHERE Part = @part

Nothing fancy as you can see. The problem I have is the Newpareto doesn't have to have a value, so I need it to allow nulls. I made sure the table allows nulls. And in my C# code I made sure to use nullable int, but when I run the code I get the error:

Exception:Thrown: "Procedure or function 'updateMain' expects parameter '@Pareto', which was not supplied." (System.Data.SqlClient.SqlException)
A System.Data.SqlClient.SqlException was thrown: "Procedure or function 'updateMain' expects parameter '@Pareto', which was not supplied."

So how do I stop this error and get the null into the table?

4
  • Also ignore the "using sqlAdapter", i dont use it anymore for this query Commented Apr 15, 2013 at 12:25
  • eh? i thought it didnt matter about case sens in sql? normally I try to stick to case sensitivity in my c# code as good practice, but sql i always thought wasnt? Commented Apr 15, 2013 at 12:44
  • although i cant see any issues there, i can understand where your coming from, although it isn't case sensative and this case isnt an issue, it is always good practice to keep case sensitivity. Ill Update my query and code to better represent this. Commented Apr 15, 2013 at 13:00
  • Sorry for the confusion - you're right, the SQL parameter names of a stored procedure are NOT case sensitive, even when being called from a C# application; just tested and verified. Commented Apr 15, 2013 at 16:07

3 Answers 3

6

The problem is that the parameter is expected, but not added if the nullable value is null. You need to address this by either:

  1. Manually setting it do DBNull.Value as in: InfoCommand.Parameters.AddWithValue("@Pareto", (object)pareto ?? DbNull.Value);
  2. Or by making the parameter optional as in: @Pareto int = null

Your stored procedure could look like this if you want to make the parameter optional:

ALTER PROCEDURE [dbo].[updateMain]
@part varchar(255),
@Pareto int = null
as
UPDATE dbo.ParetoMain SET NewPareto =@Pareto WHERE Part = @part

EDIT
I take it from the accepted answer that you need to cast to object due to type mismatch problems. I'm fixing my answer for the sake of completeness.

Sign up to request clarification or add additional context in comments.

1 Comment

exactly yes really good answer wish i could accept more than one but that would defy the meaning of answer lol, +1 anyway for great advice!
4

use

InfoCommand.Parameters.AddWithValue("@Pareto", (Object)pareto ?? DBNull.Value);

5 Comments

problem here is conversion error of type nullable int to dbnull its says
@StevenSmith Ah, you need to do (Object)pareto ?? DBNull.Value (I edited answer)
perfect so problem was trying to add the nullable value as the paramter, but because its null it doesnt get used? thus using dbnull as the parameter?
@StevenSmith Exactly, plus sql server doesn't have a concept of c# null, only its own kind of null, which DBNull.Value represents.
ahhh so thats why, i always thought null was generic to all languages and dbnull was a special type of null that DBOs distinctly recognise....wow, the more you know! nice one David!
1

Try this:

if (pareto != null)
{
  InfoCommand.Parameters.AddWithValue("@pareto", pareto);
}
else
{
  InfoCommand.Parameters.AddWithValue("@pareto", DBNull);
}

1 Comment

yeh didnt know why i didnt think of a condition hehe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.