“变量名在查询批处理或存储过程中必须是唯一的。”错误c#asp.net

时间:2021-10-31 10:12:37

this is my code,when the textbox content changes the datas required have to retrieved from the databse and displayed in the labels specified.

这是我的代码,当文本框内容发生更改时,必须从数据库中检索所需的数据并显示在指定的标签中。

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
     Match match = Regex.Match(TextBox1.Text, @"^\d{4}[A-Z]{5}\d{3}$");
     if (match.Success)
     {
         try
         {
             DropDownList1.Focus();
             string dpt = (string)Session["deptmnt"];
             idd = TextBox1.Text;
             Label33.Text = idd;
             string val = idd;
             string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
             SqlConnection con1 = new SqlConnection(con);
             con1.Open();
            // string val1 = dpt;
             try
             {

             String str = "SELECT * from student where sid=@val";
             SqlCommand cmd = new SqlCommand(str, con1);
             cmd.CommandType = CommandType.Text;
             SqlParameter sql;
             cmd.Parameters.Clear();
             sql = cmd.Parameters.Add("@val", SqlDbType.VarChar, 20);
             sql.Value = val;
             SqlDataReader reader = cmd.ExecuteReader();
             if (reader.HasRows == false)
             {
                 Label35.Visible = true;
                 TextBox1.Text = "";
             }
             else
             {
                 {
                     Panel3.Visible = true;
                     DropDownList1.Focus();
                     while (reader.Read()) // if can read row from database
                     {
                         Panel3.Visible = true;
                         Label3.Text = reader["sname"].ToString();
                         Label5.Text = reader["dept"].ToString();
                         Label25.Text = reader["yr"].ToString();
                     }
                     cmd.Parameters.Clear();


                     {

                         string val1 = idd;
                         string str2 = "SELECT bid from studentissuebook where sid=@val1 AND status='" + "lost" + "'";
                         SqlCommand cmd2 = new SqlCommand(str2, con1);
                         cmd2.CommandType = CommandType.Text;
                         cmd2.Parameters.Clear();
                         SqlParameter sql2;
                         sql2 = cmd2.Parameters.Add("@val1", SqlDbType.VarChar, 20);
                         sql2.Value = val1;
                         SqlDataReader reader1 = cmd2.ExecuteReader();
                         if (reader1.HasRows == false)
                         {
                             TextBox1.Text = "";
                             Label39.Visible = true;
                             Panel3.Visible = false;
                         }
                         else
                         {
                             DropDownList1.Focus();
                             while (reader1.Read()) // if can read row from database
                             {

                                 DropDownList1.Items.Add(reader1[0].ToString());
                             }

                             DropDownList1.Focus();
                         }
                     }
                 }
             }
             con1.Close();
             }
             catch(Exception ex)
             {
             TextBox1.Text=ex.ToString();
             }
         }
         catch (Exception ex)
         {
             TextBox1.Text = ex.ToString();
         }

         } else

     {
         formatlabel.Visible = true;
     }
}

but,when i run the code,i get an error "The variable name '@sid' has already been declared. Variable names must be unique within a query batch or stored procedure.",I googled, generally this error occurs when there is a for loop or any loops,but i do not have any loops in my code.so im unable to find the cause

但是,当我运行代码时,我得到一个错误“变量名称'@sid'已经被声明。变量名在查询批处理或存储过程中必须是唯一的。”,我用Google搜索,通常这个错误发生在有for循环或任何循环,但我的代码中没有任何循环。所以我无法找到原因

1 个解决方案

#1


1  

Try using two separate connection and command objects, like this:

尝试使用两个单独的连接和命令对象,如下所示:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Match match = Regex.Match(TextBox1.Text, @"^\d{4}[A-Z]{5}\d{3}$");
    if (match.Success)
    {
        DropDownList1.Focus();
        string dpt = (string) Session["deptmnt"];
        idd = TextBox1.Text;
        Label33.Text = idd;
        string val = idd;
        string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        using (SqlConnection con1 = new SqlConnection(con))
        {
            String str = "SELECT * from student where sid=@val";
            con1.Open();
            using (SqlCommand cmd = new SqlCommand(str, con1))
            {
                cmd.CommandType = CommandType.Text;
                SqlParameter sql;
                cmd.Parameters.Clear();
                sql = cmd.Parameters.Add("@val", SqlDbType.VarChar, 20);
                sql.Value = val;
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows == false)
                {
                    Label35.Visible = true;
                    TextBox1.Text = "";
                }
                else
                {
                    Panel3.Visible = true;
                    DropDownList1.Focus();
                    while (reader.Read()) // if can read row from database
                    {
                        Panel3.Visible = true;
                        Label3.Text = reader["sname"].ToString();
                        Label5.Text = reader["dept"].ToString();
                        Label25.Text = reader["yr"].ToString();
                    }
                    cmd.Parameters.Clear();
                }
            }
        }

        using (SqlConnection con2 = new SqlConnection(con))
        {
            string val1 = idd;
            string str2 = "SELECT bid from studentissuebook where sid=@val1 AND status='" + "lost" + "'";
            con2.Open();
            using (SqlCommand cmd2 = new SqlCommand(str2, con2))
            {
                cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.Clear();
                SqlParameter sql2;
                sql2 = cmd2.Parameters.Add("@val1", SqlDbType.VarChar, 20);
                sql2.Value = val1;
                SqlDataReader reader1 = cmd2.ExecuteReader();
                if (reader1.HasRows == false)
                {
                    TextBox1.Text = "";
                    Label39.Visible = true;
                    Panel3.Visible = false;
                }
                else
                {
                    DropDownList1.Focus();
                    while (reader1.Read()) // if can read row from database
                    {
                        DropDownList1.Items.Add(reader1[0].ToString());
                    }
                    DropDownList1.Focus();
                }
            }
        }
    }
    else
    {
        formatlabel.Visible = true;
    }
}

Note: I have removed your try-catch blocks to make the code simpler to interpret, once it is working, then please re-apply your try-catch logic where you feel appropriate. Also, I added using blocks for the SqlConnection and SqlCommand objects, this will clean up the connection even if an exception happens.

注意:我已经删除了try-catch块以使代码更易于解释,一旦它工作,那么请在您认为合适的地方重新应用try-catch逻辑。此外,我添加了使用块的SqlConnection和SqlCommand对象,这将清除连接,即使发生异常。

#1


1  

Try using two separate connection and command objects, like this:

尝试使用两个单独的连接和命令对象,如下所示:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Match match = Regex.Match(TextBox1.Text, @"^\d{4}[A-Z]{5}\d{3}$");
    if (match.Success)
    {
        DropDownList1.Focus();
        string dpt = (string) Session["deptmnt"];
        idd = TextBox1.Text;
        Label33.Text = idd;
        string val = idd;
        string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        using (SqlConnection con1 = new SqlConnection(con))
        {
            String str = "SELECT * from student where sid=@val";
            con1.Open();
            using (SqlCommand cmd = new SqlCommand(str, con1))
            {
                cmd.CommandType = CommandType.Text;
                SqlParameter sql;
                cmd.Parameters.Clear();
                sql = cmd.Parameters.Add("@val", SqlDbType.VarChar, 20);
                sql.Value = val;
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows == false)
                {
                    Label35.Visible = true;
                    TextBox1.Text = "";
                }
                else
                {
                    Panel3.Visible = true;
                    DropDownList1.Focus();
                    while (reader.Read()) // if can read row from database
                    {
                        Panel3.Visible = true;
                        Label3.Text = reader["sname"].ToString();
                        Label5.Text = reader["dept"].ToString();
                        Label25.Text = reader["yr"].ToString();
                    }
                    cmd.Parameters.Clear();
                }
            }
        }

        using (SqlConnection con2 = new SqlConnection(con))
        {
            string val1 = idd;
            string str2 = "SELECT bid from studentissuebook where sid=@val1 AND status='" + "lost" + "'";
            con2.Open();
            using (SqlCommand cmd2 = new SqlCommand(str2, con2))
            {
                cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.Clear();
                SqlParameter sql2;
                sql2 = cmd2.Parameters.Add("@val1", SqlDbType.VarChar, 20);
                sql2.Value = val1;
                SqlDataReader reader1 = cmd2.ExecuteReader();
                if (reader1.HasRows == false)
                {
                    TextBox1.Text = "";
                    Label39.Visible = true;
                    Panel3.Visible = false;
                }
                else
                {
                    DropDownList1.Focus();
                    while (reader1.Read()) // if can read row from database
                    {
                        DropDownList1.Items.Add(reader1[0].ToString());
                    }
                    DropDownList1.Focus();
                }
            }
        }
    }
    else
    {
        formatlabel.Visible = true;
    }
}

Note: I have removed your try-catch blocks to make the code simpler to interpret, once it is working, then please re-apply your try-catch logic where you feel appropriate. Also, I added using blocks for the SqlConnection and SqlCommand objects, this will clean up the connection even if an exception happens.

注意:我已经删除了try-catch块以使代码更易于解释,一旦它工作,那么请在您认为合适的地方重新应用try-catch逻辑。此外,我添加了使用块的SqlConnection和SqlCommand对象,这将清除连接,即使发生异常。