Results 1 to 20 of 20

Thread: Simple database adding question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Simple database adding question

    Hey there,
    (using a windows applicaiton) What I have is a database with engineers on it in a table tited 'Engineer', what I want to do is be able to add a new engineer to the database. Via text boxes and a submit button instead of a data grid.

    I have the data connections n such and have fiddled around a bit and tried this code.

    VB Code:
    1. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    2.  
    3. Dim Name As String
    4.  
    5. Dim qual As String
    6.  
    7. Dim mob As String
    8.  
    9. Dim phone As String
    10.  
    11. Dim NOK As String
    12.  
    13. Dim specId As String
    14.  
    15. Dim newEngineer As DataRow
    16.  
    17. Name = txtName.Text
    18.  
    19. qual = txtQual.Text
    20.  
    21. mob = txtMobile.Text
    22.  
    23. phone = txtPhone.Text
    24.  
    25. NOK = txtNOK.Text
    26.  
    27. specId = txtSpecID.Text
    28.  
    29. newEngineer = Update1.Engineer.NewRow
    30.  
    31. newEngineer("Eng-name") = Name
    32.  
    33. newEngineer("Eng-Qual") = qual
    34.  
    35. newEngineer("Eng-Mobile") = mob
    36.  
    37. newEngineer("Eng-Phone") = phone
    38.  
    39. newEngineer("ENG-NOK") = NOK
    40.  
    41. newEngineer("Spec-ID") = CInt(specId)
    42.  
    43. Update1.Engineer.Rows.Add(newEngineer)
    44.  
    45. OleDbDataAdapter1.Update(Update1)


    but to no prevail, anyone know what im doing wrong? If you need more information please ask

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    What I have understand in your question is that you want to add a data to your engineer table using textbox and a submit button. Is that what you mean?

    If so..create an sqlcommand and provide an insert statement as parameter to your sqlcommand or better use stored procedures.


    I recommend reading some toturials about ADO.NET would help you a lot.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    yeah, pretty much, but id like to add an entirely new row. so a new Engineer in other words.

    I have some trouble with that SQL stuff. what do you mean exactly?

    I've been looing through tutorials, and I am finding them difficult to understand to some extent.

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    Here a sample on how to insert a new row to your table.
    VB Code:
    1. dim cmd as sqlcommand()
    2. strSQL = "INSERT INTO Engineer(Eng-name,Eng-Qual) VALUES('" & txtName.Text & "','" & txtQual.Text "')"
    3. cmd = New SqlCommand(strSQL, con) 'con is your connection
    4. cmd.executenonquery()

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    do I have to import SQL for that? if so whats the command for that?

    cheers

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    Haven't you tried using sqlcommands? Sqlcommand is under namespace System.Data.Sqlclient

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    humm, im totally lost, if I use that name space nothing works at all now.

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    ok. what database are you using?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    just a normal access database, connected by an oleDbDataAdapter

  10. #10
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    oh. so just change the sql to oledb.
    VB Code:
    1. dim cmd as Oledbcommand()
    2. strSQL = "INSERT INTO Engineer(Eng-name,Eng-Qual) VALUES('" & txtName.Text & "','" & txtQual.Text "')"
    3. cmd = New OledbCommand(strSQL, con) 'con is your connection
    4. cmd.executenonquery()

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    I dont know what im doing wrong here

    oledbcommand is not defined

  12. #12
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    Imports System.Data.Oledb

  13. #13

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    ok, half way three I suppose

    "con" as my connection is that as in my oledbconnection ("OleDbConnection1")?
    i tired that and it didnt work.

    and it says

    cmd.executenonquery()

    is not a memeber System.Array

    but that may have something to do with my connection not being set yet

    Thanx for your help so far btw.

  14. #14
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    Could you post the code you're using? and yeah con is your oledbconnection.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple database adding question

    mar_zim has mistakenly put parentheses after the type. This:
    VB Code:
    1. dim cmd as Oledbcommand()
    is declaring an OleDbCommand array variable. Drop the parentheses and it should be alright.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    VB Code:
    1. Dim strSQL As String
    2.         Dim cmd As OleDbCommand()
    3.         strSQL = "INSERT INTO Engineer(Eng-name,Eng-Qual,Eng-Mobile,Eng-Phone,Eng-NOK,Spec-ID) VALUES('" & txtName.Text & "','" & txtQual.Text "','" & txtMobile.Text "','" & txtPhone.Text "','" & Eng-NOK "','" & Spec-ID.Text "')"
    4.         cmd = New OleDbCommand(strSQL, OleDbConnection1)
    5.         cmd.executenonquery()

    also at the end of the insert statment, it has an error also, probably just wrong syntax

  17. #17

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    ok yeah jmc that works, thanx for that

    but now I have a problem at the end of
    strSQL = "INSERT INTO Engineer(Eng-name,Eng-Qual) VALUES('" & txtName.Text & "','" & txtQual.Text "')"

    the "') " is underlined 'end of statment expected' hopefully ill be able to udnerstand all this stuff once i get in to it more

  18. #18

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    ok I think I fixed that problem, I now have
    VB Code:
    1. Dim cmd As OleDbCommand
    2.         strSQL = "INSERT INTO Engineer(Eng-name,Eng-Qual,Eng-Mobile,Eng-Phone,Eng-NOK,Spec-ID) VALUES([txtName.Text] ,[txtQual.Text], [txtMobile.Text], [txtPhone.Text], [txtNOK.text] ,[Spec-ID.Text] )"
    3.         cmd = New OleDbCommand(strSQL, OleDbConnection1)
    4.         cmd.ExecuteNonQuery()

    but now when I run it, I can an error on
    cmd.ExecuteNonQuery()

    saying

    An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

    Additional information: ExecuteNonQuery requires an open and available Connection. The connection's current state is Closed

  19. #19
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple database adding question

    Yeah sorry for the lacking of () and for your last error you must open a connection before making transaction to the database.

    oledbconnection1.open()

  20. #20

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    121

    Re: Simple database adding question

    ok, now I get

    An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

    on cmd.ExecuteNonQuery()

    Nothing ever goes right with me, does it have something to do with me having a dataset?

    Im assuming that a dataset a dataset is use with a closed connection, is that correct?

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