VB Express and SQL Database

I have an SQL DB and I am creating a form to add data to the DB, how do I pass vars from textboxes to the fields in the DB

Answer this question

VB Express and SQL Database

  • SK_Rajdev

     

     Try it if you want.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     Dim myDataRow As DataRow
            Try
                myDataRow = CustomerDataSet11.Tables("customers").NewRow()
                myDataRow("FirstName") = txtFName.Text
                myDataRow("LastName") = txtLName.Text
                myDataRow("PhoneNumber") = txtPhone.Text
                CustomerDataSet11.Tables("customers").Rows.Add(myDataRow)
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString(), "Alert")
            End Try

    Me.Validate()

    Me.customersBindingSource.EndEdit()

    Me.customersTableAdapter.Update(Me.CustomerDataSet1.customers)

    End Sub

     


  • Raguvind

    using the sample above will not save anything to the database at all. The first example does. If you are using the second example then you need to say use a dataadapter to fill your dataset, perform updates to it or whatever then save it back using the dataAdapter's update method, giving it the dataset.

    What happens when you do the first code snippet I had supplied any errors



  • TheresaB

    It tells me one record was effected, but when I check the database, I dont see any changes.

  • Sundaraguru

    I have tried both of these samples and the projects builds but no data is saved to the database when I view the table in the database explorer.

    Here is the code that I am using:

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
           
            Dim myDataRow As DataRow
            Try
                myDataRow = CustomerDataSet11.Tables("customers").NewRow()
                myDataRow("FirstName") = txtFName.Text
                myDataRow("LastName") = txtLName.Text
                myDataRow("PhoneNumber") = txtPhone.Text
                CustomerDataSet11.Tables("customers").Rows.Add(myDataRow)
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString(), "Alert")
            End Try
           


           

        End Sub

  • Mardo

    I wouldnt understand why the code above (the insert statements etc...) doesnt work as everything looks correct. if you do this:

    Dim theRecordsReturned as Integer = theSqlCommand.ExecuteNonQuery()

    MessageBox.Show(theRecordsReturned.ToString())

    does the messagebox show you how many records were effected



  • Andy Britcliffe

    This is what I used from above and no errors and nothing gets added to the table.
    I needed to make a few changes to the code due to IDE messages

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

    Dim theSqlCommand As New SqlClient.SqlCommand("INSERT INTO [customers] (FirstName) VALUES (@param1)", New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Customer.mdf;Integrated Security=True;User Instance=True"))

    Dim theSqlParameter As New SqlClient.SqlParameter("@param1", SqlDbType.NVarChar, 50)

    theSqlParameter.Value = "Garling"

    theSqlCommand.Parameters.Add(theSqlParameter)


    theSqlCommand.Connection.Open()

    theSqlCommand.ExecuteNonQuery()

    theSqlCommand.Connection.Close()



    End Sub

  • chiyan

    well a typical example would be this:

    Dim theSqlCommand as new SqlCommand("INSERT INTO [TableName] (Field1) VALUES (@param1)", new SqlConnection(ConnectionString))

    Dim theSqlParameter as new SqlParameter("@param1", SqlDbType.NvarChar, 50)

    theSqlParameter.Value = "ValueHere"

    theSqlCommand.Parameters.Add(theSqlParameter)

    theSqlCommand.Connection.Open()

    theSqlCommand.ExecuteNonQuery()

    theSqlCommand.Connection.Open()

    this is a typical way. you create your statement, and include the parameter names, then you create the list of parameters and add them to the parameter collection of your sql command, open the connection, execute the non query and close the connection.

    Is this what you are after



  • Akram Hussein

    Maybe you need this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myDataRow As DataRow
    Try
    myDataRow = Database1DataSet.Tables("Table1").NewRow()
    myDataRow("Column1") = TextBox1.Text

    myDataRow("Column2") = TextBox2.Text
    Database1DataSet.Tables("Table1").Rows.Add(myDataRow)
    Catch ex As Exception
    MessageBox.Show(ex.Message.ToString(), "Alert")
    End Try

    End Sub



  • VB Express and SQL Database