Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Sub Tampildata()
Dim ds = New DataSet
Dim cn As New SqlConnection("Data Source=PRITHVI;Initial Catalog=Database2;Integrated Security=True")
Dim query As String
query = "select * from Dish"
Dim da = New SqlDataAdapter(query, cn)
da.Fill(ds)
End Sub



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Cmd = New SqlCommand
Dim cn As New SqlConnection("Data Source=PRITHVI;Initial Catalog=Database2;Integrated Security=True")
Dim query As String
query = "Update Dish set Nameofdish='" & Txtname.Text & "',Typeofdish='" & TxtType.Text & "',Price='" & Txtprice.Text & "' where Dishid='" & ComboBox1.Text & "' "
Cmd.CommandText = query
Cmd.ExecuteNonQuery()
MsgBox("Updated")
Tampildata()

End Sub
Posted
Comments
Member 12214252 17-Dec-15 14:09pm    
im trying to insert values in to my database . any corrections or alternative coding is welcome

1 solution

Three things:
1) Do not 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. Use Parametrized queries instead.
2) It's a damn good idea to store connections strings in a configuration file, so they only need to be changed in one place, and your app doesn't need to be recompiled...
3) Your Button1 Click handler doesn't use the connection object "cn" it cretaed - so you get the error. Try creating the CMD object after the Connection:
VB
Dim cn As New SqlConnection("Data Source=PRITHVI;Initial Catalog=Database2;Integrated Security=True")
cn.Open();
Dim Cmd = New SqlCommand(query, cn);
 
Share this answer
 
Comments
Member 12214252 20-Dec-15 9:31am    
that did it. thanks for the advice. just started working with vb so still have a lot to learn
OriginalGriff 20-Dec-15 9:34am    
You're welcome!

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