How can I Insert data into SQL Server using VBNet

how to insert data into sql table using visual studio
how to insert data into sql table using visual basic insert into access database insert into sql with parameters
save button code in vb net using sql server
how to insert data in database in using c#
vbnet insert
insert update delete in vb net using sql server

I am new to I need to insert data in table by using 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) 

    If (command.ExecuteNonQuery().Equals(1)) Then
        MsgBox("Information stored in database")
        MsgBox("Not stored in database")
    End If

End Sub

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.


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
        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.

VB.NET And SQL Server, Learn how to perform the VB NET SQL query to connect to the SQL Server database and insert data using the INSERT INTO statement. 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.

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


    mycommand = New SqlCommand("insert into tbl_cus([name],[class],[phone],[address]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", myconnection)


    MessageBox.Show("New Row Inserted" & ra)


End Sub

End Class

VB.NET Tutorial, Make sure that is the table name customer details contains a space. If this, rename your table name and remove the space, then write the actual  Importing Excel data to SQL Server Database. On the click of the button the Excel file is first uploaded and then saved inside a folder named Files. Then based on the extension of the Excel file i.e. xls or xlsx, the appropriate connection sting is fetched from the Web.Config file.

Function ExtSql(ByVal sql As String) As Boolean
    Dim cnn As SqlConnection
    Dim cmd As SqlCommand
    cnn = New SqlConnection(My.Settings.mySqlConnectionString)
        cmd = New SqlCommand
        cmd.Connection = cnn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sql
    Catch ex As Exception
        Return False
    End Try
    Return True
End Function

VB NET INSERT INTO SQL Server - Querychat, Have you done any research on data access in VB.NET? If not then that should be step 1. You might start with the Database FAQ link in my  If you always use parameterized queries then you will avoid problems with representing dates as strings. You can use SQL parameters (I had to guess at the database column data types) for your query like this:

Insert data to sql server database using, cmd = New SqlCommand("insert into records ([emp_id], [emp_name], SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. No error and data not inserted to sql server table? I have a problem inserting data into a table on a MySQL database using a VB.NET application. I have a simple form where when I set some data to the textboxes and I press a GO button, the code should, insert data into sql server in by user input: i want to insert data of windows application's through form. how to insert Word (.DOC and .DOCX) and excel document files into SQL Server Database directly using File Upload Control in ASP.Net. Download Download Free Files API. Download Free Files API. I have already explained how to save and retrieve files in SQL Server database using ASP.Net in my article Save and Retrieve Files from SQL Server

Insert INTO sql command using, In this article I will explain how to insert data into database using a Web Service in ASP.Net using C# and VB.Net. The data will be inserted to database using a Web Method and the inserted data will be displayed in GridView.

  • You should show us the table definition. Maybe you've forgotten to make the pk column IDENTITYwith autoincrement. Btw, you're open for SQL-Injection. You should use SqlParameters.
  • my table defn is student(SName,SID,SPhone,SBranch,SCourse,SFee) all are belong to varchar type except fee (fee is real type)
  • @rangasathish - that's not a table definition. 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.
  • Side-note: the conn.Close is redundant since you're using using-statement anyway.
  • @TimSchmelter thanks for reminding :) but i used to do that. Anyway, I just update my answer.
  • I would vote +2 if I could for the promotion of parametrized queries
  • @JohnWoo: Anyway, +1 because of using parameters,using-statement,error-handling and one possible reason for the error.
  • You might also want to mention that 'Error here' should be replaced with appropriate error handling(f.e. logging). Don't ever swallow exceptions.
  • Please, consider adding a bit more detail and explanation to your code.