Results 1 to 18 of 18

Thread: (resolved)***Adding data to sql table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73

    (resolved)***Adding data to sql table

    Hello everyone,

    I have a test box (txtAdd) and a button (btnAdd) on a form. I want to type in a number in the text box, click the add button, and have it save to my sql database. I have the sql statements but im not sure what the other code looks like that I need. Could some show me an example?

    Thanks
    Last edited by pea33nut; Jun 28th, 2004 at 01:37 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Anyone have any ideas???

  3. #3
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20

    Re: Adding data to sql table

    Originally posted by pea33nut
    Hello everyone,

    I have a test box (txtAdd) and a button (btnAdd) on a form. I want to type in a number in the text box, click the add button, and have it save to my sql database. I have the sql statements but im not sure what the other code looks like that I need. Could some show me an example?

    Thanks
    this from mssql 2000
    VB Code:
    1. @txtbox varchar (50)
    2. as
    3. insert into nameoftable
    4. values(@txtbox)
    5. go

    hope it helps

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    I think I miss wrote what I wanted. I have that part of the code but I need to know what vb code is needed to send it to the data base. Do I need the Dim cmdtext As String? Or anything in that nature. This is some code I use to retrieve data from the database.
    da.Fill(myDS, "Auto")
    With cboItemNumber
    .DataSource = myDS.Tables("Auto")
    .DisplayMember = "ItemNumber"

    So is thier any vb code to add my sql statement in?

    thanks

  5. #5
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20
    Originally posted by pea33nut
    I think I miss wrote what I wanted. I have that part of the code but I need to know what vb code is needed to send it to the data base. Do I need the Dim cmdtext As String? Or anything in that nature. This is some code I use to retrieve data from the database.
    da.Fill(myDS, "Auto")
    With cboItemNumber
    .DataSource = myDS.Tables("Auto")
    .DisplayMember = "ItemNumber"

    So is thier any vb code to add my sql statement in?

    thanks
    VB Code:
    1. Function add()
    2.         Try
    3.             With cm
    4.                 .Parameters.Clear()
    5.                 .CommandText = "addtable"
    6.                 .Parameters.Add("@name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
    7.                 .Parameters.Add("@address", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
    8.                 .Parameters(0).Value = txtbox1.text
    9.                 .Parameters(1).Value = txtbox2.text
    10.                 .ExecuteNonQuery()
    11.             End With
    12.         Catch ex As Exception
    13.             MsgBox(ex.Message)
    14.         End Try
    15.     End Function

    this what i do to save in my database (ms sql 200)
    addtable is a storedprocedure
    hope it helps..

  6. #6
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    one other way.
    VB Code:
    1. Dim cn As New SqlConnection()
    2.       cn.ConnectionString = "integrated security=true;initial catalog=tempdb"
    3.       cn.Open()
    4.  
    5.       Dim cmdtext As String = "insert into table1 values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
    6.       Dim cm As New SqlCommand(cmdtext, cn)
    7.       cm.ExecuteNonQuery()
    8.       cn.Close()

  7. #7
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20
    brown monkey code is quiet good than mine..

    im just a stupid beginner....

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    What does the cm.ExecuteNonQuery() do? I got and error when I ran my program that said

    (An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll)

    Additional information: System error.

  9. #9
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    that's [sql | oledb | odbc | etc...]command.executequery executes a non query. sample, update, delete. i don't know exactly about your error but i think you have no database named tempdb and table in tempdb named table1. you should make this. hope this helps.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Here is my code.
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    ' Connect to the database
    Dim cn As SqlConnection = New SqlConnection
    cn.ConnectionString = "Data Source=(local);" & _
    "Initial Catalog=Justin;" & _
    "Integrated Security=SSPI"
    cn.Open()

    ' Add Data to the database
    Dim cmdtext As String = "insert into Auto values('" & txtAddItemNumber.Text & "',')"
    Dim cm As New SqlCommand(cmdtext, cn)
    cm.ExecuteNonQuery()
    cn.Close()

    The cm.executenonquery() is highlighted green with this error.

    ****An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.

  11. #11
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    perhaps "insert into [auto]..." not "auto" alone. auto is a reserved word...

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Hmmm, good thought but still no luck.

  13. #13
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Originally posted by pea33nut

    Dim cmdtext As String = "insert into Auto values('" & txtAddItemNumber.Text & "',')"
    what's with the comma?

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    I took out the comma but I still get the same error.

    ' Connect to the database
    Dim cn As SqlConnection = New SqlConnection
    cn.ConnectionString = "Data Source=(local);" & _
    "Initial Catalog=Justin;" & _
    "Integrated Security=SSPI"
    cn.Open()

    ' Add Data to the database
    Dim cmdtext As String = "insert into [Auto] values('" & txtAddItemNumber.Text & "')"
    Dim cm As New SqlCommand(cmdtext, cn)
    cm.ExecuteNonQuery()
    cn.Close()

    thier seems to be something wrong with the cm.executenonquery()

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Auto is the table but ItemNumber is the field that I want the txtAddItemNumber value to go. Shouldn't I have the ItemNumber somewhere in my code to let it know what field to go to? Anyone
    Last edited by pea33nut; Jun 28th, 2004 at 09:27 AM.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Any suggestions!!!
    Last edited by pea33nut; Jun 28th, 2004 at 12:17 PM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    I have stumped the forum.

  18. #18
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Ok, your Insert is messed up. It should be:

    INSERT INTO AUTO (FieldName) VALUES ('" & Text & "'")

    You aren't telling it what field to fill with your value....
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width