Solved

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

Posted on 2008-10-12
7
401 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:sijishJohn
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:sijishJohn
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Did you looked to my example ? Maybe not!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now