943,095 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3698
  • VB.NET RSS
Dec 31st, 2009
0

how to get last insert id (vb + mysql)

Expand Post »
Hi guys,

I'm a newbie in Visual Basic and I need some assistance.

I have 2 tables in mysql:

1. stats (stats_id,statsno,statsName)
2.stats_det (stats_det_ID,stats_id,stat_DateAdd,stat_UserAdd,stat_DateModif,stat_UserModif)

I have a form named status and from here I want to add a new record in the first table (stats).
I've created the insert statement in the first table which is running well, but I don't know how to get the last_insert_id from stats table and insert it in the second table stats_det.

My code is below :
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub checkDuplicates()
  2. 'the field must contain some values
  3. If txtStatNo.Text = "" Then
  4. 'field StatNo is empty
  5. tsResult.Text = "Field StatNo must have a value"
  6. tsResult.ForeColor = Color.Red
  7. ErProvider.SetError(txtStatNo, "Please fill the Status No field")
  8. ErProvider.SetIconPadding(txtStatNo, 5)
  9. Else
  10. 'field statNo is not empty
  11. SQL = "select stats_ID,statsNo,statsName from stats where " _
  12. & " statsNo= '" & txtStatNo.Text & "'"
  13.  
  14. Try
  15. conn.Open()
  16. Try
  17. myComm.Connection = conn
  18. myComm.CommandText = SQL
  19.  
  20. myReader = myComm.ExecuteReader
  21. If myReader.HasRows = 0 Then
  22. 'is not duplicate
  23. 'we have to insert the values
  24. insertValues_stats()
  25. Else
  26. 'is duplicate
  27. 'we have to post back he information
  28. tsResult.Text = "This record exists in our database. Duplicate."
  29. tsResult.ForeColor = Color.Red
  30. ErProvider.SetError(txtStatNo, "Duplicate Record")
  31. ErProvider.SetIconPadding(txtStatNo, 5)
  32. txtStatNo.Focus()
  33. End If
  34. Catch myerror As MySqlException
  35. MsgBox("Error loading datas " & myerror.Message)
  36. End Try
  37. Catch myerror As MySqlException
  38. MsgBox("Error connecting to the server" & myerror.Message)
  39. Finally
  40. If conn.State <> ConnectionState.Closed Then conn.Close()
  41. End Try
  42. End If
  43. End Sub
  44.  
  45. Private Sub insertValues_stats()
  46.  
  47. SQL = "insert into stats(statsNo,statsName) " _
  48. & " values(?statsNo,?statsName); " _
  49. & "select last_insert_id() from stats"
  50.  
  51. With myComm
  52. .Connection = conn
  53. .CommandText = SQL
  54. .Parameters.AddWithValue("?StatsNo", txtStatNo.Text)
  55. .Parameters.AddWithValue("?StatsName", txtStatName.Text)
  56. End With
  57.  
  58. Try
  59. conn.Close()
  60. conn.Open()
  61. myComm.ExecuteNonQuery()
  62. insertValues_statsDet()
  63. Catch myerror As MySqlException
  64. MsgBox("Error updating the database :" & myerror.Message)
  65. Finally
  66. If conn.State <> ConnectionState.Closed Then conn.Close()
  67. End Try
  68. End Sub
  69.  
  70. Private Sub insertValues_statsDet()
  71.  
  72. SQL = "insert into stats_det(stats_ID,stats_dateadd, " _
  73. & "stats_useradd,stats_datemodif,stats_usermodif) " _
  74. & " values(last_insert_id(),now(),?userID,now(),?userID)"
  75.  
  76. With myComm
  77. .Connection = conn
  78. .CommandText = SQL
  79. .Parameters.AddWithValue("?UserID", UserID)
  80.  
  81. End With
  82. Try
  83.  
  84. conn.Close()
  85. conn.Open()
  86. myComm.ExecuteNonQuery()
  87.  
  88. Catch myerror As MySqlException
  89. MsgBox("Error updating the stats_def table :" & myerror.Message)
  90. Finally
  91. If conn.State <> ConnectionState.Closed Then conn.Close()
  92. End Try
  93.  
  94. End Sub
  95.  
  96. On the btnSave I placed the code :
  97. Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
  98. checkDuplicates()
  99. End Sub

Please tell me where I've made the mistake.

Thank you.
Last edited by peter_budo; Dec 31st, 2009 at 10:59 am. Reason: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
venom80 is offline Offline
2 posts
since Dec 2009
Jan 2nd, 2010
0
Re: how to get last insert id (vb + mysql)
I've just modified the Sub and now it's working :
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub insertValues_statsDet()
  2. Dim LastID As New MySqlCommand
  3. Dim ID As Integer
  4.  
  5. LastID.CommandText = "select last_insert_id()"
  6. LastID.Connection = conn
  7. Lid = LastID.ExecuteScalar()
  8. txtStatsID.Text = Lid
  9. ID = Lid.ToString
  10.  
  11. SQL = "insert into stats_det(stats_ID,stats_dateadd, " _
  12. & "stats_useradd,stats_datemodif,stats_usermodif) " _
  13. & " values(?ID,now(),?userID,now(),?userID)"
  14.  
  15. With myComm
  16. .Connection = conn
  17. .CommandText = SQL
  18. .Parameters.Add("?ID", Lid)
  19. .Parameters.Add("?UserID", My.Settings.UserID)
  20. End With
  21. Try
  22.  
  23. conn.Close()
  24. conn.Open()
  25. myComm.ExecuteNonQuery()
  26. clearText()
  27. Catch myerror As MySqlException
  28. MsgBox("Error updating the stats_def table :" & myerror.Message)
  29. Finally
  30. If conn.State <> ConnectionState.Closed Then conn.Close()
  31. End Try
  32.  
  33. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
venom80 is offline Offline
2 posts
since Dec 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: urgent need-webbrowser
Next Thread in VB.NET Forum Timeline: Escaping a Loop While It's Running





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC