I have problem updating specific record(s) selected to edit from a DataGridView into textboxes ,where the data can be edited and then update to the database.
Now my update statement must have a where clause ,but it's not working,right now the code I'm about to post does updated record(s),but not the specified record in the textboxes to be edited but the whole set of recordes in the table.
Please help me with the where clause in my sql update querry.
here is the code
Dim con = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Danial\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Pay.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
con.Open()
Dim _String As String = "UPDATE Employee SET Firstname = @Firstname, Lastname = @Lastname, Fathername = @Fathername, Nominename = @Nominename, Gender = @Gender, Address = @Address, Pincode = @Pincode, Contactnumber = @Contactnumber, DOB = @DOB, City = @City, Bankdetails = @Bankdetails, Companyname = @Companyname, Designation = @Designation, ESINO = @ESINO, PFNO = @PFNO, Basicsalary = @Basicsalary, DOJ =@DOJ, DOL = @DOL, HRA = @HRA, BasicofPF = @BasicofPF, Conv = @Conv, Loanamount = @Loanamount, Bonus = @Bonus, Otherded = @Otherded, Imagedata = @Imagedata, Imagename = @Imagename, Grosssalary = @Grosssalary, Remark = @Remark "
'Using update As New SqlCommand("UPDATE Employee SET Firstname,Lastname,Fathername,Nominename,Gender,Address,Pincode,Contactnumber,DOB,City,Bankdetails,Companyname,Designation,ESINO,PFNO,Basicsalary,DOJ,DOL,HRA,BasicofPF,Conv,Loanamount,Bonus,Otherded,Imagedata,Imagename,Imagepath,Grosssalary,Remark) values (@Firstname,@Lastname,@Fathername,@Nominename,@Gender,@Address,@Pincode,@Contactnumber,@DOB,@City,@Bankdetails,@Companyname,@Designation,@ESINO,@PFNO,@Basicsalary,@DOJ,@DOL,@HRA,@BasicofPF,@Conv,@Loanamount,@Bonus,@Otherded,@Imagedata,@Imagename,@Imagepath,@Grosssalary,@Remark where Firstname = " & DGV2.CurrentRow.Cells(1).Value() & "")
cmd = New SqlCommand(_String, con)
cmd.Parameters.Add(New SqlParameter("Firstname", SqlDbType.NVarChar, 50))
cmd.Parameters("Firstname").Value = TextBox1.Text
cmd.Parameters.Add(New SqlParameter("Lastname", SqlDbType.NVarChar, 50))
cmd.Parameters("Lastname").Value = TextBox2.Text
cmd.Parameters.Add(New SqlParameter("Fathername", SqlDbType.NVarChar, 50))
cmd.Parameters("Fathername").Value = TextBox3.Text
cmd.Parameters.Add(New SqlParameter("Nominename", SqlDbType.NVarChar, 50))
cmd.Parameters("Nominename").Value = TextBox4.Text
cmd.Parameters.Add(New SqlParameter("Gender", SqlDbType.NVarChar, 5))
cmd.Parameters("Gender").Value = TextBox5.Text
cmd.Parameters.Add(New SqlParameter("Address", SqlDbType.NVarChar, 50))
cmd.Parameters("Address").Value = TextBox6.Text
cmd.Parameters.Add(New SqlParameter("Pincode", SqlDbType.NChar, 10))
cmd.Parameters("Pincode").Value = TextBox7.Text
cmd.Parameters.Add(New SqlParameter("Contactnumber", SqlDbType.NChar, 15))
cmd.Parameters("Contactnumber").Value = TextBox8.Text
cmd.Parameters.Add(New SqlParameter("DOB", SqlDbType.Date))
cmd.Parameters("DOB").Value = DateTimePicker1.Text
cmd.Parameters.Add(New SqlParameter("City", SqlDbType.NChar, 10))
cmd.Parameters("City").Value = TextBox9.Text
cmd.Parameters.Add(New SqlParameter("Bankdetails", SqlDbType.NVarChar, 50))
cmd.Parameters("Bankdetails").Value = TextBox10.Text
cmd.Parameters.Add(New SqlParameter("Companyname", SqlDbType.NVarChar, 50))
cmd.Parameters("Companyname").Value = TextBox11.Text
cmd.Parameters.Add(New SqlParameter("Designation", SqlDbType.NVarChar, 50))
cmd.Parameters("Designation").Value = TextBox12.Text
cmd.Parameters.Add(New SqlParameter("ESINO", SqlDbType.NChar, 20))
cmd.Parameters("ESINO").Value = TextBox13.Text
cmd.Parameters.Add(New SqlParameter("PFNO", SqlDbType.NChar, 20))
cmd.Parameters("PFNO").Value = TextBox14.Text
cmd.Parameters.Add(New SqlParameter("Basicsalary", SqlDbType.NChar, 10))
cmd.Parameters("Basicsalary").Value = TextBox15.Text
cmd.Parameters.Add(New SqlParameter("DOJ", SqlDbType.Date))
cmd.Parameters("DOJ").Value = DateTimePicker2.Text
cmd.Parameters.Add(New SqlParameter("DOL", SqlDbType.Date))
cmd.Parameters("DOL").Value = DateTimePicker3.Text
cmd.Parameters.Add(New SqlParameter("HRA", SqlDbType.NChar, 10))
cmd.Parameters("HRA").Value = TextBox16.Text
cmd.Parameters.Add(New SqlParameter("BasicofPF", SqlDbType.NChar, 10))
cmd.Parameters("BasicofPF").Value = TextBox17.Text
cmd.Parameters.Add(New SqlParameter("Conv", SqlDbType.NChar, 10))
cmd.Parameters("Conv").Value = TextBox18.Text
cmd.Parameters.Add(New SqlParameter("Loanamount", SqlDbType.NVarChar, 20))
cmd.Parameters("Loanamount").Value = TextBox19.Text
cmd.Parameters.Add(New SqlParameter("Bonus", SqlDbType.NChar, 10))
cmd.Parameters("Bonus").Value = Txtbonus.Text
cmd.Parameters.Add(New SqlParameter("Otherded", SqlDbType.NChar, 10))
cmd.Parameters("Otherded").Value = TextBox21.Text
cmd.Parameters.Add(New SqlParameter("Imagedata", SqlDbType.Image))
cmd.Parameters("Imagedata").Value = imgdata
cmd.Parameters.Add(New SqlParameter("Imagename", SqlDbType.NChar, 10))
cmd.Parameters("Imagename").Value = Txtfilename.Text
cmd.Parameters.Add(New SqlParameter("Imagepath", SqlDbType.NVarChar, 200))
cmd.Parameters("Imagepath").Value = Txtfilepath1.Text
cmd.Parameters.Add(New SqlParameter("Grosssalary", SqlDbType.NChar, 10))
cmd.Parameters("Grosssalary").Value = TxtGross.Text
cmd.Parameters.Add(New SqlParameter("Remark", SqlDbType.NVarChar, 500))
cmd.Parameters("Remark").Value = RTB1.Text
cmd.ExecuteNonQuery()
MsgBox("Data + Image Updated Into DataBase..", MsgBoxStyle.Information)
con.Close()
I have tried with the where clause
Using update As New SqlCommand("UPDATE Employee SET Firstname,Lastname,Fathername,Nominename,Gender,Address,Pincode,Contactnumber,DOB,City,Bankdetails,Companyname,Designation,ESINO,PFNO,Basicsalary,DOJ,DOL,HRA,BasicofPF,Conv,Loanamount,Bonus,Otherded,Imagedata,Imagename,Imagepath,Grosssalary,Remark) values (@Firstname,@Lastname,@Fathername,@Nominename,@Gender,@Address,@Pincode,@Contactnumber,@DOB,@City,@Bankdetails,@Companyname,@Designation,@ESINO,@PFNO,@Basicsalary,@DOJ,@DOL,@HRA,@BasicofPF,@Conv,@Loanamount,@Bonus,@Otherded,@Imagedata,@Imagename,@Imagepath,@Grosssalary,@Remark where Firstname = " & DGV2.CurrentRow.Cells(1).Value() & "")
But it keeps on updated the first row only.