1

I am totally stumped on this one, and I'm brand new to C# so maybe one of you fine folks can help me out. I'm trying to update a database from form fields using ADO.NET.

Basically, the error I'm getting is that I am trying to insert null values into the database. The exact text of the error is:

System.ArgumentNullException: The SqlParameterCollection only accepts non-null SqlParameter type objects.

Here is the code behind for the page I am trying to use:

private void ExecuteInsert(string firstName, string lastName, string emailAddress, string city, string phone)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());
    string sql = "INSERT INTO contactInfo (firstName, lastName, emailAddress, city, phone) VALUES "
                + " (@firstName, @lastName, @emailAddress, @city, @phone)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[6];
        param[0] = new SqlParameter("@firstName", SqlDbType.NVarChar, 25);
        param[1] = new SqlParameter("@lastName", SqlDbType.NVarChar, 25);
        param[2] = new SqlParameter("@emailAddress", SqlDbType.NVarChar, 30);
        param[3] = new SqlParameter("@city", SqlDbType.NVarChar, 50);
        param[4] = new SqlParameter("@phone", SqlDbType.Int, 10);

        param[0].Value = firstName;
        param[1].Value = lastName;
        param[2].Value = emailAddress;
        param[3].Value = city;
        param[4].Value = phone;

        for (int i = 0; i < param.Length; i++)
        {
                cmd.Parameters.Add(param[i]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        conn.Close();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (firstName.Text != null)
    {
        //call the method to execute insert to the database
        ExecuteInsert(firstName.Text,
                      lastName.Text,
                      emailAddress.Text,
                      city.Text,
                      phone.Text);
        Response.Write("Record was successfully added!");
        ClearControls(Page);
    }
    else
    {
        Response.Write("Please enter your name");
    }
}

public static void ClearControls(Control Parent)
{

    if (Parent is TextBox)
    { (Parent as TextBox).Text = string.Empty; }
    else
    {
        foreach (Control c in Parent.Controls)
            ClearControls(c);
    }
}

If there is another way that I should be doing this, I would love to hear it. I just can't seem to find good information online.

1
  • Why are you using an array in the first place? Commented May 3, 2012 at 20:24

2 Answers 2

2

This line:

SqlParameter[] param = new SqlParameter[6];

Should be (array is too big and you don't set last parameter):

SqlParameter[] param = new SqlParameter[5];
Sign up to request clarification or add additional context in comments.

2 Comments

+1, Basically, you're assigning param[5] into the collection, when param[5] is NULL.
Yes! Thank you so much, that's exactly the type of problem I've been running into over and over with this project. I can't see the forest for the trees because I'm so used to PHP. The code works perfectly now.
0

Change

SqlParameter[] param = new SqlParameter[6];

to

SqlParameter[] param = new SqlParameter[5];

You can add your parameters directly to cmd.Parameters without creating temp array.

Comments

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.