Reputation: 401
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);
Still Object reference not set to an instance of an object.
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;
}
Upvotes: 1
Views: 2566
Reputation: 1790
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);
Upvotes: 1
Reputation: 135
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))
Upvotes: 0
Reputation: 2659
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;
}
Upvotes: 0