Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I update a sql database with data from a text box on a form?

Posted on 2008-10-12
7
Medium Priority
?
410 Views
Last Modified: 2012-05-05
I'm new to VB.Net and I need to know how to insert a new record in a sql table from data in two text boxes on a form called txtFirstName and txtLastName. Also the sql table has a CustID field which is the index, it's not auto generated so I need to increment it by one when a record is inserted. Thank you for your help.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 
        Dim dbConnection As New SqlConnection("Data Source=192.168.0.4,1433;Network Library=DBMSSOCN;Initial Catalog=TestData;User ID=sa;Password=password;")
 
        Dim queryString As String = "SELECT * FROM dbo.Patient"
 
        Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter( _
          queryString, dbConnection)
 
        dbConnection.Open()
 
        Dim tblCustomer As DataSet = New DataSet
        dataAdapter.Fill(tblCustomer, "Customer")
 
        MISSING CODE GOES HERE
 
 
        dbConnection.Close()
 
 
 
 
    End Sub

Open in new window

0
Comment
Question by:rfgraham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:sijishJohn
ID: 22700292
use this to increment the CustID field

select Max(CustID)+1 from <tablename>

Refer this link

http://www.java2s.com/Code/ASP/ADO.net-Database/GenerateSQLinsertcommandVBnet.htm
0
 

Author Comment

by:rfgraham
ID: 22700389
sijishJohn,

I made a change in my sql db table to use Identity, which is for incrementing my ID field when a record is added.

I'm connecting to a sql db on another computer so I'm using:
"Imports System.Data.SqlClient" in the General Declarations of my code window.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22700882
>>I made a change in my sql db table to use Identity, which is for incrementing my ID field when a record is added.

That way you only need to insert the fields ... don't use the CustID in the Transact SQL command.
The example that sijishJohn has showed you is for Access and for SQL is a little (no to much) different.
Check this example that I have posted:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23349839.html 
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 4

Expert Comment

by:sijishJohn
ID: 22700904
Ok then this will be enough...
Dim conn As New SqlConnection("Data Source=192.168.0.4,1433;Network Library=DBMSSOCN;Initial Catalog=TestData;User ID=sa;Password=password;")
        conn.Open()
        Dim strSQL As String = "INSERT INTO <table name> (FirsName, LastName) VALUES (?, ?)"
 
        Dim dbComm As New SqlCommand(strSQL, conn)
        dbComm.Parameters.Add("FirstName", SqlDbType.VarChar)
        dbComm.Parameters.Add("LastName", SqlDbType.VarChar)
 
        dbComm.Parameters("FirstName").Value = txtFirstName.Text
        dbComm.Parameters("LastName").Value = txtLastName.Text
 
        dbComm.ExecuteNonQuery()
        conn.Close()

Open in new window

0
 
LVL 4

Accepted Solution

by:
sijishJohn earned 2000 total points
ID: 22701051
Oops...
what jpaulino said was right..that was not the sql code..sorry to mislead you..

Dim conn As New SqlConnection("Data Source=192.168.0.4,1433;Network Library=DBMSSOCN;Initial Catalog=TestData;User ID=sa;Password=password;")
        conn.Open()
        Dim strSQL As String = "INSERT INTO <Your Table Name> (<Field1>, <field2>) VALUES (@First, @Last)"
 
        Dim dbComm As New SqlCommand(strSQL, conn)
        dbComm.Parameters.Add("@First", SqlDbType.VarChar)
        dbComm.Parameters.Add("@Last", SqlDbType.VarChar)
 
        dbComm.Parameters("@First").Value = txtFirstName.Text
        dbComm.Parameters("@Last").Value = txtLastName.Text
 
        dbComm.ExecuteNonQuery()
        conn.Close()

Open in new window

0
 

Author Comment

by:rfgraham
ID: 22749975
Thanks for you help but I have a related question, maybe you'll know the answer to this one.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23826936.html
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22749989
Did you looked to my example ? Maybe not!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question