We help IT Professionals succeed at work.
Get Started

how to store data from a textbox to sql DB

tehmole
tehmole asked
on
1,364 Views
Last Modified: 2013-12-20
hi, messing around with VB 2005 for a project...

i'm trying to figure out how to populate my DB by users entering data in a textbox.  By clicking a button will update the DB with the new data entered.

please look at my code, i dont know what i'm doing wrong or right...

thank you in advance....
Imports System.Data.SqlClient
 
 
Public Class Form1
    Dim mycon As SqlConnection
    Dim myad As SqlDataAdapter
    Dim dt As DataTable
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
 
        'establish connection to datatable
        mycon = New SqlConnection
        mycon.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=c:\something.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"
        mycon.Open()
 
        'create adapter to the connection
        myad = New SqlDataAdapter
        myad.SelectCommand = New SqlCommand
        myad.SelectCommand.CommandText = "select * from concerts"
        myad.SelectCommand.Connection = mycon
 
        dt = New DataTable
        myad.Fill(dt)
 
        DataGridView1.DataSource = dt
 
 
        ListBox1.DataSource = dt
        ListBox1.DisplayMember = "CONCERTS"
 
 
'update commands
        myad.UpdateCommand = New SqlCommand
        myad.UpdateCommand.CommandText = "update members set CONCERTS=@CN, SEATS=@ST, ID=@ID"
        myad.UpdateCommand.Parameters.Add("@CN", SqlDbType.VarChar, 50, "CONCERTS")
        myad.UpdateCommand.Parameters.Add("@ST", SqlDbType.Int, 50, "SEATS")
        myad.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 50, "ID")
 
        myad.UpdateCommand.Connection = mycon
 
        'insert commands
 
        myad.InsertCommand = New SqlCommand
        myad.InsertCommand.Connection = mycon
        myad.InsertCommand.CommandText = "insert into concerts (CONCERTS) VALUES ('" & TextBox1.Text & "')"
 
        myad.InsertCommand.CommandText = "insert into Concerts (ID, CONCERTS, SEATS) VALUES (@ID, @CN, @ST)"
        myad.InsertCommand.Parameters.Add("@CN", SqlDbType.VarChar, 50, "CONCERTS")
        myad.InsertCommand.Parameters.Add("@ST", SqlDbType.Int, 50, "SEATS")
        myad.InsertCommand.Parameters.Add("@ID", SqlDbType.Int, 50, "ID")
 
 
 
End Sub
End Class

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE