Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when I edit the record with the following code
VB.NET
Try
    If EditClick = True Then
        SqlQuery = "update Ledgers set Name = '" & TxtName.Text & "',Grp = '" & CmbGrp.Text & "',Add1= '" & TxtAdd1.Text & "',Add2= '" & TxtAdd2.Text & "',City = '" & CmbCity.Text & "',Dist ='" & TxtDist.Text & "',Pincode ='" & TxtPIN.Text & "',State ='" & DBState.Text & "',Email ='" & TxtEmail.Text & "',Landline ='" & TxtPhone.Text & "',Mobile ='" & TxtMob.Text & "',VATNO ='" & TxtVAT.Text & "',CSTNO ='" & TxtCST.Text & "',GSTNO = '" & TxtGST.Text & "',PAN ='" & TxtPAN.Text & "' where cust_id = '" & TxtCustID.Text & "'"
        Sqlcommand = New MySqlCommand(SqlQuery, Conn)
        Reader = Sqlcommand.ExecuteReader
        Reader.Close()

    Else
        SqlQuery = "insert into Ledgers (Name,Grp,Add1,Add2,City,Dist,Pincode,State,Email,Landline,Mobile,VATNO,CSTNO,GSTNO,PAN) values ('" & TxtName.Text & "' , '" & CmbGrp.Text & "' , '" & TxtAdd1.Text & "' , '" & TxtAdd2.Text & "' , '" & CmbCity.Text & "' , '" & TxtDist.Text & "' , '" & TxtPIN.Text & "' , '" & DBState.Text & "' , '" & TxtEmail.Text & "' , '" & TxtPhone.Text & "' , '" & TxtMob.Text & "' , '" & TxtVAT.Text & "' , '" & TxtCST.Text & "' , '" & TxtGST.Text & "' , '" & TxtPAN.Text & "')"
        Sqlcommand = New MySqlCommand(SqlQuery, Conn)
        Reader = Sqlcommand.ExecuteReader
        Reader.Close()
    End If
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
DisableTxt(Me)
EnableBtn(Me)
BtnSave.Enabled = False
BtnAdd.Enabled = True
BtnAdd.Focus()
BtnExit.Enabled = True
Reader.Close()
Ledgers()
EditClick = False

I see both records(edited and old) on form.
If my table has 10 records , I see 20 records(extra entry of all entries)
Suppose if i edit my record from "JAMEER" to "JAMEER MULLA" i see both entries
when I exit and again start my application everything id Ok.

What I have tried:

I tried to refresh the dataset, but could not find the command to refres it.
Posted
Updated 14-Oct-19 8:59am
v2
Comments
Member 13550326 20-Oct-19 7:12am    
Try
If EditClick = True Then
Sqlcommand.CommandText = "update Ledgers set Name = @name, Grp = @Grp, Add1= @add1, Add2= @add2, City = @city, Dist = @dist, Pincode = @pin, State = @state, Email = @email, Landline = @phone, Mobile = @mob, VATNO = @vat, CSTNO = @cst, GSTNO = @gst, PAN = @pan where cust_id = '" & TxtCustID.Text & "'"
AddParam()
Dim Rer As Integer = Sqlcommand.ExecuteNonQuery
Else
Sqlcommand.CommandText = "insert into Ledgers (Name,Grp,Add1,Add2,City,Dist,Pincode,State,Email,Landline,Mobile,VATNO,CSTNO,GSTNO,PAN) values (@Name, @Grp, @add1, @add2, @city, @dist, @pin, @state, @email, @phone, @mob, @vat, @cst, @gst, @pan)"
AddParam()
Dim Rer As Integer = Sqlcommand.ExecuteNonQuery
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
DisableTxt(Me)
EnableBtn(Me)
BtnSave.Enabled = False
BtnAdd.Enabled = True
BtnAdd.Focus()
BtnExit.Enabled = True
EditClick = False

above I have tried to use parameterized query as suggested, no problem in that. now whenever i add or edit a record number of records dont double up, but still i dont see a newly added record or a change in existing record immediately. I have to close and open my application to see the changes in the table. Please Help

1 solution

Two things:

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Don't use ExecuteReader with INSERT or UPDATE commands - use ExecuteNonQuery instead.

Fix both of those, and it'll work.
 
Share this answer
 
Comments
Member 13550326 26-Oct-19 9:37am    
First of all Thanks for your advice of using Parameterized Query.
I have chaged all my queries in parameterized queries, but the above mentioned problem is still not resolved.

After adding a new record , form does not shows the newly added record .
after editing a record, I see two entries of a same record one with changes and other without changes.

After I restart my app everything is ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900