tags:

views:

42

answers:

4

Hello,

I am writing a program and part of it is to store the keyed entry in a database on an sql server when the enter key is hit. The database is small and simple with only three entry spots in the table. one for the tracking number, one for the date entered, and one for the time entered. The only thing the user will see is the tracking number text box and an enter button. the date and time will be auto entered when the enter key is hit. i am relatively new to databases so i cannot figure out how to send the data to the database. The database is already configured, just need to get the program and the database talking to eachother. Thanks.

A: 

Check out the API for SqlClient: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.aspx

Specifically focus in on SqlCommand. There is example code in the documentation.

decompiled
A: 

Ok i understand how to open the connection now but not how to add an entry into the database when the enter key is hit.

TID
Don't reply to your own question like this. These spaces are reserved for answers. Use the "add comment" links instead.
Joel Coehoorn
A: 

one for the date entered, and one for the time entered

This is wrong, from a database design standpoint. Put them in a single column with a datetime type, preferably set to use getdate() or current_timestamp as the default value. Assuming you fix that:

Using cn As New SqlConnection("connection string here"), _
      cmd As New SqlCommand("INSERT INTO [tablename] (TrackingNumber) VALUES (@Number)", cn)
    cmd.Parameters.Add("@Number", SqlDbType.VarChar, 50).Value = TextBox1.Text

    cn.Open()
    cmd.ExecuteNonQuery()
End Using

The SqlConnection and SqlCommand classes are in the System.Data.SqlClient namespace.
For help with your connection string, see Connectionstrings.com

Joel Coehoorn
A: 

Is this close?

Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click

    'Add entry to database
    Using cn As New SqlClient.SqlConnection("Data Source=APP02;Initial Catalog=ScannedDB;Integrated Security=True;Pooling=False;Encrypt=False"), _
    cmd As New SqlClient.SqlCommand("INSERT INTO [ScannedDBTable] (Tracking_Number) VALUES (@Number)", cn)
        cmd.Parameters.Add("@Tracking_Number", SqlDbType.VarChar, 50).Value = ScannedBarcodeText.Text()

        cn.Open()
        cmd.ExecuteNonQuery()
    End Using

End Sub

TID