0

This is the code I got, but using this I cannot update custname. I need to use id in this which is autoincremented in database.

con2.Open()

Dim query As String
query = "update CustomerMaster set CustName='" + TextBox1.Text + "',Address='" + TextBox2.Text + "',Phone='" + TextBox3.Text + "',Mobile='" + TextBox4.Text + "',email='" + TextBox5.Text + "'where  CustName='" + TextBox1.Text + "'"

cmd = New SqlCommand(query, con2)

Dim reader2 As SqlDataReader
reader2 = cmd.ExecuteReader

MessageBox.Show("Data updated")
con2.Close()

snap

This is a snap from a Youtube video in this they use datagrid view but in my form it is listbox so can anyone convert this code to use in listbox

Actually I want to find the id in vb.net which is invisible and only can see in sql server.

  • Please [edit] the question to add the code from the image as text so that those of us who can't see the picture clearly can read it as text. – Andrew Morton Oct 28 '22 at 20:30
  • Oh, so whatever YT video you're following, it shows a wrong way (an "anti-pattern") of interacting with a database for .NET. Note the video's author and don't follow them any more - they are 30 years out of date. – Andrew Morton Oct 28 '22 at 20:37
  • The following may be helpful: https://stackoverflow.com/a/68355687/10024425 and https://stackoverflow.com/questions/70483410/how-do-i-correct-a-syntax-error-in-command-builder/70484316#70484316 (this one can be adapted for SQL Server). – Tu deschizi eu inchid Oct 28 '22 at 22:46
  • The id should have been retrieved when you retrieved the data from the database. However, it's not necessary to display the id to the user. – Tu deschizi eu inchid Oct 28 '22 at 22:49
  • You should be using a data adapter and a `DataTable`. Call `Fill` to populate the `DataTable` with data including the PK values, make as many edits as you require and then call `Update` to save the changes. The control(s) that you bind your `DataTable` to are irrelevant to that. – jmcilhinney Oct 29 '22 at 01:19
  • The screenshot you have posted shows a `SELECT` statement being executed by calling `ExecuteNonQuery` and your code executes am `UPDATE` statement by calling `ExecuteReader`, both of which are backwards. You're also being shown how to build SQL code using string concatenation, which is bad. I agree that you should never watch anything from that person again and find a proper ADO.NET tutorial. – jmcilhinney Oct 29 '22 at 01:22
  • actually i want to find id which is not visible in the form ,but only in sql – Fathima Nifru Oct 29 '22 at 06:10
  • You can't use something that you don't have. If you want to use the id in your program, ensure that you've retrieved the id from the database when you retrieve the data from your database. You may consider using a [DataTable](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netframework-4.8) as others have suggested. – Tu deschizi eu inchid Oct 29 '22 at 12:44
  • The following may also be helpful: [DataSets, DataTables, and DataViews](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/) and [SqlDataAdapter](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldataadapter?view=netframework-4.8). – Tu deschizi eu inchid Oct 29 '22 at 12:46

0 Answers0