1

In the collection of my BloodType combobox property. I have 3 values which is O, A and C. But after I choose the option of the value I want, and click submit I get this error.

The parameterized query '(@pFirstName nvarchar(5),@pLastName nvarchar(6),@pContact nvarch' expects the parameter '@pBloodType', which was not supplied.

It will worked if I use a textbox for my bloodtype instead.

For my textbox code which is before.

updateCmd.Parameters.AddWithValue("@pBloodType", txtpBloodType.Text);

For comboBox code which i am using now.

updateCmd.Parameters.AddWithValue("@pBloodType", cbpBloodType.SelectedValue);

enter image description here

Still Object reference not set to an instance of an object.

enter image description here

   public patient()
    {
        InitializeComponent();
        this.cbpBloodType = new System.Windows.Forms.ComboBox();
        this.cbpBloodType.Items.Add("O");
        this.cbpBloodType.Items.Add("A");
        this.cbpBloodType.Items.Add("C");
    }

    private int AddPatientRecord()
    {
        int result = 0;
        // TO DO: Codes to insert customer record
        //retrieve connection information info from App.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
        //STEP 1: Create connection
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        //STEP 2: Create command
        String strCommandText = "INSERT PATIENT(pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail) "
            + " VALUES (@pFirstName,@pLastName,@pContact,@pAddress,@pCity,@pZip,@pNationality, @pRace, @pIC, @pGender, @pDOB, @pBloodType, @pEmail)";

        SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);

        updateCmd.Parameters.AddWithValue("@pFirstName", txtpFirstName.Text);
        updateCmd.Parameters.AddWithValue("@pLastName", txtpLastName.Text);
        //updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output; 
        updateCmd.Parameters.AddWithValue("@pContact", txtpContact.Text);
        updateCmd.Parameters.AddWithValue("@pAddress", txtpAddress.Text);
        updateCmd.Parameters.AddWithValue("@pCity", txtpCity.Text);
        updateCmd.Parameters.AddWithValue("@pZip", txtpZip.Text);
        updateCmd.Parameters.AddWithValue("@pNationality", txtpNationality.Text);
        updateCmd.Parameters.AddWithValue("@pRace", txtpRace.Text);
        updateCmd.Parameters.AddWithValue("@pIC", txtpIC.Text);
        if (rbMale.Checked)
        {
            updateCmd.Parameters.AddWithValue("@pGender", "M");
        }
        else
        {
            updateCmd.Parameters.AddWithValue("@pGender", "F");
        }
        updateCmd.Parameters.AddWithValue("@pDOB", dtppDOB.Value);
        string value = cbpBloodType.SelectedItem == null ? "A" : cbpBloodType.SelectedItem.ToString();
        updateCmd.Parameters.AddWithValue("@pBloodType", value);
        updateCmd.Parameters.AddWithValue("@pEmail", txtpEmail.Text);
        // STEP 3 open connection and retrieve data by calling ExecuteReader
        myConnect.Open();
        // STEP 4: execute command
        // indicates number of record updated.
        result = updateCmd.ExecuteNonQuery();

        // STEP 5: Close
        myConnect.Close();
        return result;

    }
2
  • have you tried using updateCmd.Parameters.AddWithValue("@pBloodType", cbpBloodType.SelectedValue.ToString()); ? calling the ToString() method on the value? Commented Jan 27, 2014 at 14:33
  • Did you check in the debugger that cbpBloodType and cbpBloodType.SelectedValue exist when running your code? Commented Jan 27, 2014 at 14:37

3 Answers 3

1

Try to initialize your combobox in code behind :

public Form1()
{
    InitializeComponent();
    cb1.Items.Add("O");
    cb1.Items.Add("A");
    cb1.Items.Add("C");
}

Then access the data with cbpBloodType.SelectedItem.ToString();

string value = cbpBloodType.SelectedItem == null ? "A" : cbpBloodType.SelectedItem.ToString();
updateCmd.Parameters.AddWithValue("@pBloodType", value);
Sign up to request clarification or add additional context in comments.

10 Comments

omg this worked, but whatever i clicked, it always gives me A in the datagrid @Plue
must be this line of code. // Pass default value, for example A updateCmd.Parameters.AddWithValue("@pBloodType", "A"); . how to rewrite it? @Plue
Change string bloodtype = cbpBloodType.Text; to string bloodtype = cbpBloodType.SelectedValue;
I got error when changed to that . Error 2 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) @Plue
After changed to this. string bloodtype = cbpBloodType.SelectedValue.ToString(); this error again. Object reference not set to an instance of an object. @Plue
|
0

Mybe cbpBloodType.SelectedValue return null. How are you binding your drop downlist ?

Try this

private int AddPatientRecord()
{
    int result = 0;
    // TO DO: Codes to insert customer record
    //retrieve connection information info from App.config
    string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
    //STEP 1: Create connection
    SqlConnection myConnect = new SqlConnection(strConnectionString);
    //STEP 2: Create command
    String strCommandText = "INSERT PATIENT(pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail) "
        + " VALUES (@pFirstName,@pLastName,@pContact,@pAddress,@pCity,@pZip,@pNationality, @pRace, @pIC, @pGender, @pDOB, @pBloodType, @pEmail)";

    SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);

    updateCmd.Parameters.AddWithValue("@pFirstName", txtpFirstName.Text);
    updateCmd.Parameters.AddWithValue("@pLastName", txtpLastName.Text);
    //updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output; 
    updateCmd.Parameters.AddWithValue("@pContact", txtpContact.Text);
    updateCmd.Parameters.AddWithValue("@pAddress", txtpAddress.Text);
    updateCmd.Parameters.AddWithValue("@pCity", txtpCity.Text);
    updateCmd.Parameters.AddWithValue("@pZip", txtpZip.Text);
    updateCmd.Parameters.AddWithValue("@pNationality", txtpNationality.Text);
    updateCmd.Parameters.AddWithValue("@pRace", txtpRace.Text);
    updateCmd.Parameters.AddWithValue("@pIC", txtpIC.Text);
    if (rbMale.Checked)
    {
        updateCmd.Parameters.AddWithValue("@pGender", "M");
    }
    else
    {
        updateCmd.Parameters.AddWithValue("@pGender", "F");
    }
    updateCmd.Parameters.AddWithValue("@pDOB", dtppDOB.Value);
    if(!String.IsNullOrEmpty(cbpBloodType.SelectedValue.ToString()))
    {
      updateCmd.Parameters.AddWithValue("@pBloodType", cbpBloodType.SelectedValue.ToString());
    }
    else
    {
       // Pass default value, for example A
       updateCmd.Parameters.AddWithValue("@pBloodType","A");
    } 
    updateCmd.Parameters.AddWithValue("@pEmail", txtpEmail.Text);
    // STEP 3 open connection and retrieve data by calling ExecuteReader
    myConnect.Open();
    // STEP 4: execute command
    // indicates number of record updated.
    result = updateCmd.ExecuteNonQuery();

    // STEP 5: Close
    myConnect.Close();
    return result;

}

9 Comments

I got this Object reference not set to an instance of an object by following your code @Mohammad jouhari
So now instead of writing it in the collection in the property. I write the values in the code is it? @Mohammad jouhari
Where you got exactly the Object reference not set to an instance of an object ? Can you Debug It ?
I thing you have issue with binding the drop down list. Can you share with us that code ?
At least put your connection to the database in a try catch so you definitely close the connection if the database doesn't like what you send it. Your code is begging for a database connection to be left open.
|
0

Not sure why the null exception but in any case I bet you are getting the wrong value from "Selected Value". Assign the variable first then step through it. I bet you might find you are not getting back "A" or "B" but rather something like "System.Windows.Controls..."

try this and see if you get the same error.

string bloodtype = cbpBloodType.Text;

if(!String.IsNullOrEmpty(bloodtype))

1 Comment

And when you step through it, is the variable getting assigned properly or is the error happening when you call the AddWithValue?

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.