Results 1 to 6 of 6

Thread: Warning message when adding data to database

  1. #1
    Join Date
    Jan 2010
    Posts
    43

    Warning message when adding data to database

    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

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Warning message when adding data to database

    Your Sub Connection closes the connection immediately after opening, so it will not work.

    On the other hand, if the Open method is successful, the connection status will inevitably open. And so the Open method fails, you have an exception, so anyway you will never pass in the else.

    You should not use the same connection object each time it does that bring you problems. Instead use a local variable that you initialized with the result of a function GetConnection:
    Code:
        Public Function GetConnection() As SqlConnection
            Dim connection As New SqlConnection()
            connection.ConnectionString = "Data Source=localhost;Initial Catalog=base;Integrated Security=true"
            connection.Open()
            Return connection
        End Function
     
    ...
    
    Try
            Using (connection As GetConnection())
                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")
     
            End Using ' The connection is automatically closed here
     
            Catch ex As Exception
                MsgBox("Failed registration : " & ex.ToString())
            End Try

  3. #3
    Join Date
    Jan 2010
    Posts
    43

    Re: Warning message when adding data to database

    I found an error in this line of code
    Code:
    Using (connection As GetConnection())
    ')' Expected .... I can not find where

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Warning message when adding data to database

    Ah yes sorry ... I am not very use to write VB codes
    Code:
    Using (connection As SqlConnection = GetConnection())

  5. #5
    Join Date
    Jan 2010
    Posts
    43

    Re: Warning message when adding data to database

    Sorry but I am still getting the same error, missing ')' from VB
    Code:
    Using (connection As SqlConnection = GetConnection())

  6. #6
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Warning message when adding data to database

    I am really struggling with the VB syntax... I think you should try "Using" without parentheses:
    Code:
    Using connection As SqlConnection = GetConnection()

Similar Threads

  1. Warning C4244: possible loss of data
    By supernoob in forum Software Development
    Replies: 2
    Last Post: 03-09-2009, 09:41 PM
  2. USB shows warning message
    By JamesB in forum Hardware Peripherals
    Replies: 4
    Last Post: 26-02-2009, 06:32 PM
  3. How to refresh database after adding a new record?
    By ArunJ in forum Software Development
    Replies: 5
    Last Post: 16-02-2009, 07:27 PM
  4. Adding Image in SQL database through vb.net application?
    By Sanket07 in forum Software Development
    Replies: 4
    Last Post: 16-02-2009, 06:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,556,505.00676 seconds with 17 queries