wendy
wendy

Reputation: 161

No value given to one or more parameters

So I input some data into textbox then I click this button

    private void button2_Click(object sender, EventArgs e)
    {
      command.Connection = connect;
      if (idkaryawantxt.Text != "")
       {
            string q = "UPDATE tableAbsensi SET Absen_keluar =('" + (DateTime.Now.ToString("hh:mm")) + "') WHERE ID ='" + idkaryawantxt.Text.ToString() + "' AND Tanggal ='" + (DateTime.Now.ToString("MM-dd-yyyy")) +"'";
            dosomething(q);
       }
       this.Close();
    }

Then it says

No value given to one or more parameters

The table looked like this : enter image description here

Upvotes: 0

Views: 62

Answers (1)

Paweł Bejger
Paweł Bejger

Reputation: 6366

Check not only if idkaryawantxt is empty but also if it is not null:

    if (string.IsNullOrEmpty(idkaryawantxt.Text))
    {
        var currentDateTime = DateTime.Now; 
        string q = "UPDATE tableAbsensi SET Absen_keluar ='" 
            + currentDateTime.ToString("hh:mm") + "' WHERE ID ='" 
            + idkaryawantxt.Text + "' AND Tanggal ='" 
            + currentDateTime.ToString("MM-dd-yyyy") +"'";

        dosomething(q);
    }

Secondly the brackets here (DateTime.Now.ToString("hh:mm"))and here (DateTime.Now.ToString("MM-dd-yyyy")) are not needed.

You do not need to convert idkaryawantxt.Text to string (idkaryawantxt.Text.ToString()), as it is already a string.

You do not need brackets here SET Absen_keluar =('"and here "') WHERE ID ='" .

What is more it might be useful to set the DateTime.Now to a variable instead of calling it twice, because in some exceptional cases it could give you two different values.

And finally: avoid creating your queries in the way you did in this case. It is not an elegant way of creating queries + it is not secured against SQL injections.

Upvotes: 1

Related Questions