简体   繁体   中英

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 : 在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM