MS Access

kindads

New member
Joined
Jul 10, 2005
Messages
1
Programming Experience
Beginner
Hello,
what's wrong with this code ,
Nothing added to database

VB.NET:
[left]

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim TRS As DataRow = DataSet11.Bayynat_db.NewRow

		Try
			TRS("Revenue") = TextBox1.Text
			TRS("Expenses") = TextBox2.Text
			TRS("Note") = TextBox3.Text
			TRS("Date") = DateTimePicker1.Value.Date
			DataSet11.Tables("Bayynat_db").Rows.Add(TRS)
			OleDbDataAdapter1.Update(DataSet11)

			MsgBox("Data Was SuccessFully Added To The DataBase", MsgBoxStyle.Information, Me.Text)
			TextBox1.Focus()
		Catch
			MsgBox("2 Records were added with the same primary key. Changes canceled.", MsgBoxStyle.Critical, Me.Text)
			TRS.Delete()
			DataSet11.Clear()
		End Try

		MsgBox("Data Was SuccessFully Added To The DataBase")

		OleDbConnection1.Close()
		Me.Close()
	End Sub[/left]
 
Where do you open the connection to the database? Also, in your catch block, what if there is another error other than the one that is coded?

Chester
 
You don't need to open and close a connection as Update does that implicitly. You would only open and close the connection yourself if you are calling Update multiple times on the same connection. cpopham is correct that if you want to catch a specific exception that you should code for it. You're catching any exception and assuming that it is caused by a duplicate primary key, which is likely but by no means certain.

I would guess that the issue is with your data adapter and/or insert command. I think you need to examine it and perhaps get some more information about the exception that you are catching, if there is one. It may be a syntax error in your SQL or something like that.
 
Back
Top