Here I got my connection to my database with this code
Code:
Public Sub connection()
connection.ConnectionString = "Data Source=localhost;Initial Catalog=base;Integrated Security=true"
connection.Open()
If connection.State = ConnectionState.Open Then
MsgBox("Connection was successful", MsgBoxStyle.Exclamation, "Status")
Else
MsgBox("Connection failed", MsgBoxStyle.Critical, "Status")
End If
connection.Close()
Now I try to add data in my database, data taken from the TextBox, but I get this warning when I click "save"
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Here is my code
Code:
Try
connection()
Dim strRequest As String = "SELECT * FROM Coaches"
Dim dtt As DataTable
Dim oSqlDataAdapter As New SqlDataAdapter(strRequest, connection)
Dim oDataSet As New DataSet("Coaches")
oSqlDataAdapter.Fill(oDataSet, "Coaches")
dtt = oDataSet.Tables("Coaches")
oSqlDataAdapter.InsertCommand = New SqlCommand("INSERT INTO Coaches(Name,Surname,Address,Telephone) Values(@TextBoxName_E,@TextBoxSurname_E,@TextBoxAd_E,@TextBoxTel_E)", connection)
oSqlDataAdapter.InsertCommand.Parameters.Add("@TextBoxName_E", SqlDbType.Int, 30, "TextBoxName_E")
oSqlDataAdapter.InsertCommand.Parameters.Add("@TextBoxSurname_E", SqlDbType.NChar, 15, "TextBoxSurname_E")
oSqlDataAdapter.InsertCommand.Parameters.Add("@TextBoxAd_E", SqlDbType.Int, 100, "TextBoxAd_E")
oSqlDataAdapter.InsertCommand.Parameters.Add("@TextBoxTel_E", SqlDbType.Int, 100, "TextBoxTel_E")
Dim oDataRow As DataRow
Dim byteArray As Byte() = {}
oDataRow = oDataSet.Tables("Coaches").NewRow
oDataRow("TextBoxName_E") = TextBoxName_E.Text
oDataRow("TextBoxSurname_E") = TextBoxSurname_E.Text
oDataRow("TextBoxAd_E") = TextBoxAd_E.Text
oDataRow("TextBoxTel_E") = TextBoxTel_E.Text
oDataSet.Tables("Coaches").Rows.Add(oDataRow)
oSqlDataAdapter.Update(oDataSet, "Coaches")
oDataSet.Clear()
oSqlDataAdapter.Fill(oDataSet, "Coaches")
dtt = oDataSet.Tables("Coaches")
MsgBox("Article successfully registered with", MsgBoxStyle.Information, "Status")
connection.Close()
Catch
MsgBox("Failed registration")
End Try
Bookmarks