The parameterized query expects the parameter which was not supplied

后端 未结 6 1081
甜味超标
甜味超标 2020-11-27 05:47

I\'m having a problem with my code:

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged         


        
相关标签:
6条回答
  • 2020-11-27 06:26

    Your website is in serious danger of being hacked.

    Read up on SQL Injection and how to prevent it in .NET

    Your query problem is the least of your concerns right now.

    But.....

    @Misnomer's solution is close but not quite there:

    Change your query to this:

    cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%@DepartmentText%')"
    

    and add parameters this way (or the way that @Misnomer does):

    cmd.Parameters.AddWithValue("@DepartmentText",TextBox2.Text)
    

    The important difference is that you need to change your CommandText.

    0 讨论(0)
  • 2020-11-27 06:27

    If you are writing from a DataGridView control to your database, make sure there is no empty row. Set 'Allow User to add Rows' to false; it truncates the unnecessary last empty row.

    0 讨论(0)
  • 2020-11-27 06:30

    If you pass null value to parameter,you will get this error even after you add the parameter so try to check the value and if it null then use DBNull.Value

    This will work

    cmd.Parameters.Add("@Department", SqlDbType.VarChar)
    
    If (TextBox2.Text = Nothing) Then
        cmd.Parameters("@Department").Value = DBNull.Value
    Else
        cmd.Parameters("@Department").Value = TextBox2.Text
    End If
    

    This will convert the null values from the object layer to DBNull values that are acceptable to the database.

    0 讨论(0)
  • 2020-11-27 06:33

    Try adding parameters like this -

    cmd.Parameters.Add("@Department", SqlDbType.VarChar)
    cmd.Parameters("@Department").Value = TextBox2.Text
    

    and change your command text to what @Abe Miessler does he is right i just thought you will figure it out.

    0 讨论(0)
  • 2020-11-27 06:33
    SqlConnection conn = new SqlConnection(connectionString);
    
    conn.Open();
    //SelectCustomerById(int x);
    comboBoxEx1.Items.Clear();
    
    SqlCommand comm = new SqlCommand("spSelectCustomerByID", conn);
    //comm.Parameters.Add(new SqlParameter("cust_name", cust_name));
    //comm.CommandText = "spSelectCustomerByID";
    comm.Parameters.Add(new SqlParameter("cust_id", SqlDbType.Int));
    comm.CommandType = CommandType.StoredProcedure;
    comm.ExecuteNonQuery();
    
    SqlDataAdapter sdap = new SqlDataAdapter(comm);
    DataSet dset = new DataSet();
    sdap.Fill(dset, "cust_registrations");
    
    if (dset.Tables["cust_registrations"].Rows.Count > 0)
    {
        comboBoxEx1.Items.Add("cust_registrations").ToString();
    }
    comboBoxEx1.DataSource = dset;
    comboBoxEx1.DisplayMember = "cust_name";
    
    0 讨论(0)
  • 2020-11-27 06:41

    Building on and simplifying ravidev's answer:

    The VB.NET shorthand is:

    cmd.Parameters.AddWithValue("@Department", IF(TextBox2.Text, DBNull.Value))
    

    The C# shorthand is:

    cmd.Parameters.AddWithValue("@Department", (object)TextBox2.Text ?? DBNull.Value)
    
    0 讨论(0)
提交回复
热议问题