插入错误参数化查询'(@username varchar(50),@ Fullname varchar(50),@ Address varchar(50')需要未提供的参数'@username'. [英] Insert ErrorThe parameterized query '(@username varchar(50),@Fullname varchar(50),@Address varchar(50' expects the parameter '@username', which was not supplied.

查看:132
本文介绍了插入错误参数化查询'(@username varchar(50),@ Fullname varchar(50),@ Address varchar(50')需要未提供的参数'@username'.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个表格,当我单击下一步按钮时,出现错误:Insert Error参数化查询(@username varchar(50),@ Fullname varchar(50),@ Address varchar(50'')参数"@username"(未提供).

代码是:-

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

        }

推荐答案

我建​​议您更改if语句:单个竖线是逻辑OR:我怀疑您希望使用条件AND :双&"号.<&&".
目前,您尝试插入的是文本框中的任何一个都不为空-我怀疑如果所有这些文本框都不为空,您打算这样做.

这可能会解决您的问题.

[edit]"is"更改为"if"-为准确输入还为时过早-OriginalGriff [/edit]


我这样做了,但是它仍然给出了相同的错误:("


现在,它已格式化,更易读,也很明显:您正在创建参数,但没有将值传递给它.
与其创建参数数组并在以后的循环中添加它们,不如使用Parameters.AddWithValue方法:

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:

con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@Fullname", fullname);
...


u也应在类似下面的sql permetar中写入源列..
u should write source column also in sql permetars like below..
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); .......


您创建了很多参数,但是没有设置它们的值(或者代码中未显示).请参阅 MSDN文档 [
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


这篇关于插入错误参数化查询'(@username varchar(50),@ Fullname varchar(50),@ Address varchar(50')需要未提供的参数'@username'.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆