Question Inserting into SQL Server Database

ProgrammingN00b

New member
Joined
Dec 7, 2008
Messages
4
Programming Experience
Beginner
I've succesfully (i think) managed to create a connection to my database.

I want to be able to transfer data from a textbox on the form into collumns of a table on the database. I've tried using this code but it doesn't seem to work, other than making a connection. What else am I missing?

VB.NET:
Expand Collapse Copy
   Try
            myConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True")
            myConnection.Open()
            MsgBox("Connection Open ! ")
            myCommand = New SqlCommand("INSERT INTO Customer VALUES 'xxx', 'yyyy'")
        Catch ex As Exception
            MessageBox.Show("Failed to connect to data source")
        Finally
            myConnection.Close()
        End Try

thanks
 
Hi

You have not told you sql statement which columns you want to insert data into. In your code below I have assumed that the columns are called FirstName or LastName, but these may vary.

VB.NET:
Expand Collapse Copy
Try
   myConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True")
   myConnection.Open()
   MsgBox("Connection Open ! ")
   myCommand = New SqlCommand("INSERT INTO Customer (FirstName, LastName) VALUES 'xxx', 'yyyy'")
Catch ex As Exception
   MessageBox.Show("Failed to connect to data source")
Finally
    myConnection.Close()
 End Try
Hope this helps

Regards
ScottyB
 
You havent read the DW2 link in my signature, noting any sections that are .NET 3.5 specific (see the top right of the page), starting with the section called Creating a Simple Data App

Had you read and followed this tutorial, you would have embarked on a path of data access methodology that would have neatly avoided every problem youre currently facing becuse the computer would have created working code for you
 
Back
Top