Ive made a form and when I click the next button, I get an error: Insert Error The parameterized query '(@username varchar(50),@Fullname varchar(50),@Address varchar(50' expects the parameter '@username', which was not supplied.
The code is:-
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Online job\\App_Data\\db.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select Username from db";
cmd.Connection = con;
username.Text = Session["Username"].ToString();
}
private void ExecuteInsert(string username,string fullname, string address, string city, string state, string country, string postalcode, string phoneno, string gender, string dob, string workyrs, string workmonths, string keyskills, string basicEduc, string mastersEduc, string EmployIndus, string funcArea, string CurrLoc, string Hobbies, string Achievements, string Awards )
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Online job\\App_Data\\db.mdf;Integrated Security=True;User Instance=True");
string sql = "INSERT into db2(username,Fullname,Address,City,State,Country,Postalcode,Phoneno,gender,dob,Workyrs,workmonths,keyskills,BasicEducation,MastersEducation,EmployInd,functionalArea,CurrentLoc,Hobbies,Achievements,Awards) VALUES (@username,@Fullname,@Address,@City,@State,@Country,@Postalcode,@Phoneno,@gender,@dob,@Workyrs,@workmonths,@keyskills,@BasicEducation,@MastersEducation,@EmployInd,@functionalArea,@CurrentLoc,@Hobbies,@Achievements,@Awards)";
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter[] param = new SqlParameter[21];
param[0] = new SqlParameter("@username", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@Fullname", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@Address", SqlDbType.VarChar, 50);
param[3] = new SqlParameter("@City", SqlDbType.VarChar, 50);
param[4] = new SqlParameter("@State", SqlDbType.VarChar, 50);
param[5] = new SqlParameter("@Country", SqlDbType.VarChar, 50);
param[6] = new SqlParameter("@Postalcode", SqlDbType.VarChar, 9999999);
param[7] = new SqlParameter("@Phoneno", SqlDbType.VarChar, 999999999);
param[8] = new SqlParameter("@gender", SqlDbType.VarChar, 50);
param[9] = new SqlParameter("@dob", SqlDbType.VarChar, 50);
param[10] = new SqlParameter("@Workyrs", SqlDbType.VarChar, 50);
param[11] = new SqlParameter("@workmonths", SqlDbType.VarChar, 50);
param[12] = new SqlParameter("@keyskills", SqlDbType.VarChar, 1000);
param[13] = new SqlParameter("@BasicEducation", SqlDbType.VarChar, 50);
param[14] = new SqlParameter("@MastersEducation", SqlDbType.VarChar, 50);
param[15] = new SqlParameter("@EmployInd", SqlDbType.VarChar, 50);
param[16] = new SqlParameter("@functionalArea", SqlDbType.VarChar, 50);
param[17] = new SqlParameter("@CurrentLoc", SqlDbType.VarChar, 50);
param[18] = new SqlParameter("@Hobbies", SqlDbType.VarChar, 1000);
param[19] = new SqlParameter("@Achievements", SqlDbType.VarChar, 1000);
param[20] = new SqlParameter("@Awards", SqlDbType.VarChar, 1000);
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = "Insert Error";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (fullname.Text != "" | address.Text != "" | city.Text != "" | state.Text != "" | country.Text != "" | postcode.Text != "" | PhoneNo.Text != "" | dob.Text != "" | Keyskills.Text != "" | hobbies.Text != "")
{
ExecuteInsert(username.Text, fullname.Text, address.Text, city.Text, state.Text, country.Text, postcode.Text, PhoneNo.Text, Gender.SelectedItem.Text, dob.Text, workyr.SelectedItem.Text, workmonth.SelectedItem.Text, Keyskills.Text, basicEducation.SelectedItem.Text, mastersEdu.SelectedItem.Text, employmentInd.SelectedItem.Text, FunctionalArea.SelectedItem.Text, CurrentLoc.SelectedItem.Text, hobbies.Text, achievements.Text, awards.Text);
Response.Redirect(Home.aspx);
}
else
{
lblfield.Text = Fill the desired fields!!!
}