Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:-

C#
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!!!
           

        }
Posted
Updated 17-Oct-11 21:49pm
v3

I suggest that you change the if statement: a single vertical bar is a Logical OR : I suspect you want a Conditional AND instead: a double Ampersand "&&".
At the moment, you with try to do the insert is any one of the Text boxes is not empty - I suspect you meant to do it if all of them are not empty...

This will probably cure your problem.

[edit]"is" changed to "if" - it's too early for accurate typing - OriginalGriff[/edit]


"I did that, but its still giving the same error :("


Now it's formatted it's more readable and pretty obvious: you are creating parameters, but you don't pass the value through.
Instead of creating a parameter array and adding them in a loop later, use the Parameters.AddWithValue method instead:

C#
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@Fullname", fullname);
...
 
Share this answer
 
v3
Comments
angel 2 18-Oct-11 3:54am    
I did that, but its still giving the same error :(
angel 2 18-Oct-11 4:42am    
um so silly!!!
Thanks for ur help...Its working now :)
u should write source column also in sql permetars like below..
SQL
param[0] = new SqlParameter("@username", SqlDbType.VarChar, 50,username);
            param[1] = new SqlParameter("@Fullname", SqlDbType.VarChar, 50,fullname);
            param[2] = new SqlParameter("@Address", SqlDbType.VarChar, 50,address);
            param[3] = new SqlParameter("@City", SqlDbType.VarChar, 50,city);
            param[4] = new SqlParameter("@State", SqlDbType.VarChar, 50,state);
            param[5] = new SqlParameter("@Country", SqlDbType.VarChar, 50,country); .......
 
Share this answer
 
Comments
angel 2 18-Oct-11 4:43am    
thanks a lot for your help :)
You create a lot of parameters but don't set their values (or it's not shown in your code). See MSDN documentation[^] for an example
 
Share this answer
 
Comments
angel 2 18-Oct-11 4:43am    
thnks for your help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900