I am new to vb.net I need to insert  data in table by using vb.net please can any one help
I have tried this
Here I tried Sample Code
I got this exception Column name or number of supplied values does not match table definition.
thanks advance
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs)  Handles btnSave.Click
    Dim strName As String = txtName.Text
    Dim strId As String = txtID.Text
    Dim strPhone As String = txtPhone.Text
    Dim strBranch As String = cmboxBranch.SelectedItem.ToString()
    Dim  strCourse As String = cmbboxCourse.SelectedItem.ToString()
    Dim dblFee As Double = Double.Parse(txtFee.Text) 
    Dim strCommand As String = "insert into student values('" & strName & "','" & strId &    "','" & strPhone & "','" & strBranch & "','" & strCourse & "'," & dblFee & ")"
    Dim command As SqlCommand = New SqlCommand(strCommand, connection)
    command.CommandType = CommandType.Text
    '' MsgBox(strCommand) 
    connection.Open()
    If (command.ExecuteNonQuery().Equals(1)) Then
        MsgBox("Information stored in database")
    Else
        MsgBox("Not stored in database")
    End If
End Sub
                Easiest is to go to the table using SQL Server Management Studio (SSMS). Right click it, choose "Script Table -> As Create -> To Clipboard". Then edit your question and paste in the result. It will show us the data types for the table, any constraints, etc.
SQL Server and Visual Basic . NET must be installed and running on the same computer. In addition, the user must be able to use Windows Integrated Security to connect to SQL Server.
Right-click Data Connections, and then click Add connection. In the Data Link Properties dialog box, click localhost in the Select or enter a server name box. Click Windows NT Integrated Security to log on to the server. Click Select the database on the server, and then select Northwind database from the list.
It means that the number of values specified in your VALUES clause on the INSERT statement is not equal to the total number of columns in the table. You must specify the columnname if you only try to insert on selected columns.
Another one, since you are using ADO.Net , always parameterized your query to avoid SQL Injection. What you are doing right now is you are defeating the use of sqlCommand.
ex
Dim query as String = String.Empty
query &= "INSERT INTO student (colName, colID, colPhone, "
query &= "                     colBranch, colCourse, coldblFee)  "
query &= "VALUES (@colName,@colID, @colPhone, @colBranch,@colCourse, @coldblFee)"
Using conn as New SqlConnection("connectionStringHere")
    Using comm As New SqlCommand()
        With comm
            .Connection = conn
            .CommandType = CommandType.Text
            .CommandText = query
            .Parameters.AddWithValue("@colName", strName)
            .Parameters.AddWithValue("@colID", strId)
            .Parameters.AddWithValue("@colPhone", strPhone)
            .Parameters.AddWithValue("@colBranch", strBranch)
            .Parameters.AddWithValue("@colCourse", strCourse)
            .Parameters.AddWithValue("@coldblFee", dblFee)
        End With
        Try
            conn.open()
            comm.ExecuteNonQuery()
        Catch(ex as SqlException)
            MessageBox.Show(ex.Message.ToString(), "Error Message")
        End Try
    End Using
End USing 
PS: Please change the column names specified in the query to the original column found in your table.
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dr1 As SqlDataReader
Dim ra As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    myconnection = New SqlConnection("server=localhost;uid=root;pwd=;database=simple")
    'you need to provide password for sql server
    myconnection.Open()
    mycommand = New SqlCommand("insert into tbl_cus([name],[class],[phone],[address]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", myconnection)
    mycommand.ExecuteNonQuery()
    MessageBox.Show("New Row Inserted" & ra)
    myconnection.Close()
End Sub
End Class
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With