Friday, March 9, 2012

expects parameter......parameterized query

Hi all,

I am using the below parameterized query and get an error while executing it...can anyone please spot the error. Any help will be appreciated. I have gone cross-eyed now looking at it all day. The error I get it is

Parameterized Query '(@.Re_UK_Eligible nvarchar(4000),@.Re_Aus_Eligible nvarchar(33),@.R' expects parameter @.Re_JobType_Temp, which was not supplied.

 sqlStmt ="UPDATE Re_Users SET Re_UK_Eligible=@.Re_UK_Eligible,Re_Aus_Eligible=@.Re_Aus_Eligible,Re_Can_Eligible=@.Re_Can_Eligible,Re_USA_Eligible=@.Re_USA_Eligible,Re_Address1=@.Re_Address1,Re_Address2=@.Re_Address2,Re_Address3=@.Re_Address3,Re_City=@.Re_City,Re_Postcode=@.Re_Postcode,Re_Country=@.Re_Country,Re_Homephone=@.Re_Homephone,Re_Mobile=@.Re_Mobile,Re_JobType_Per=@.Re_JobType_Per,Re_JobType_Temp=@.Re_JobType_Temp,Re_JobType_Con=@.Re_JobType_Con,Re_Hours_Full=@.Re_Hours_Full,Re_Hours_Part=@.Re_Hours_Part,Re_Sector=@.Re_Sector,Re_StepTwoDone=1 WHERE Re_UserCount=" + Session["ReUserIdentity"]; cn =new SqlConnection(ConfigurationManager.ConnectionStrings["ReConnectionString"].ConnectionString); cmd =new SqlCommand(sqlStmt, cn); cmd.CommandType = CommandType.Text;//Insert UKif (chkUK.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_UK_Eligible", DBNull.Value)); }if ((chkUK.Checked ==true) && (UKRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_UK_Eligible", UKRadioButtonList.SelectedItem.Text)); }//Insert AUSif (chkAUS.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Aus_Eligible", DBNull.Value)); }if ((chkAUS.Checked ==true) && (AUSRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_Aus_Eligible", AUSRadioButtonList.SelectedItem.Text)); }//Insert CANif ((chkCAN.Checked ==false)) { cmd.Parameters.Add(new SqlParameter("@.Re_Can_Eligible", DBNull.Value)); }if ((chkCAN.Checked ==true) && (CANRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_Can_Eligible", CANRadioButtonList.SelectedItem.Text)); }//Insert USAif (chkUSA.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_USA_Eligible", DBNull.Value)); }if ((chkUSA.Checked ==true) && (USARadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_USA_Eligible", USARadioButtonList.SelectedItem.Text)); }//Contact Details cmd.Parameters.Add(new SqlParameter("@.Re_Address1", Address1TextBox.Text));if (Address2TextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Address2", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Address2", Address2TextBox.Text)); }if (Address3TextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Address3", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Address3", Address3TextBox.Text)); } cmd.Parameters.Add(new SqlParameter("@.Re_City", CityTextBox.Text)); cmd.Parameters.Add(new SqlParameter("@.Re_Postcode", PostcodeTextBox.Text)); cmd.Parameters.Add(new SqlParameter("@.Re_Country", CountryDropDownList.SelectedItem.Text));if (HomeTelephoneTextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Homephone", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Homephone", HomeTelephoneTextBox.Text)); }if (MobileTelephoneTextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Mobile", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Mobile", MobileTelephoneTextBox.Text)); }//Job Preferencesfor (int i = 0; i < JobTypeCheckBoxList.Items.Count; i++) {if (JobTypeCheckBoxList.Items[i].Text =="Permanent" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Per", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Permanent" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Per", 0)); }if (JobTypeCheckBoxList.Items[i].Text =="Temporary" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Temp", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Temporary" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Temp", 0)); }if (JobTypeCheckBoxList.Items[i].Text =="Contract" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Con", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Contract" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Con", 0)); } }//Hours and Sectorfor (int i = 0; i < HoursCheckBoxList.Items.Count; i++) {if (HoursCheckBoxList.Items[i].Text =="FullTime" && HoursCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Full", 1)); }else if (HoursCheckBoxList.Items[i].Text =="FullTime" && HoursCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Full", 0)); }if (HoursCheckBoxList.Items[i].Text =="PartTime" && HoursCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Part", 1)); }else if (HoursCheckBoxList.Items[i].Text =="PartTime" && HoursCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Part", 0)); } } cmd.Parameters.Add(new SqlParameter("@.Re_Sector", SectorDropDownList.SelectedItem.Text)); cn.Open(); cmd.ExecuteNonQuery();

thanks,

vijay

I would add the parameters to the collection first and set the value in the IF loops. Sometimes there are other conditions that you might not have accounted for.

Example:

myCommand.Parameters.Add(New SqlParameter("@.userid",SqlDbType.int));if txtbox1.text ==""{myCommand.Parameters("@.userid").Value = 0}else{myCommand.Parameters("@.userid").Value = userid}

Also, I would do a response.write of JobTypeCheckBoxList.Items.Count to see what the count is.

|||

Gave my sore eyes some rest and changed the code as recommended and guess what it did do the trick!!!

thanks

vijay

No comments:

Post a Comment