I am trying to show data on datagridview but error occurs
Form1.vb code is as below

Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader
Imports System.String
Imports System.Configuration
Imports System.ComponentModel


Public Class Form1
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            con = New SqlConnection("data source=USER-PC; database=demo1; Integrated security=SSPI;")
            con.Open()
            cmd = New SqlCommand("insert into Demorecord1 values('" & TextBox1.Text & "','" & TextBox2.Text & "')", con)
            da = New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds)
            MsgBox("Record inserted successfully")

            TextBox1.Text = ""
            TextBox2.Text = ""

        Catch ex As Exception
            'con.Close()'
            'Me.Close()'

        End Try
    End Sub

    Private Shared Function getConnectionString() As String
        Throw New NotImplementedException
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MsgBox("Please enter name")
        Else
            'con.Open()

            display()
            Dim response
            response = MsgBox("Are you sure to delete this record", vbYesNo)
            If response = vbYes Then
                deletedata()
                MsgBox("data deleted successfully")
            Else

                TextBox1.Text = ""
                TextBox2.Text = ""
            End If
        End If

    End Sub
    Public Sub deletedata()
        con = New SqlConnection("data source=USER-PC; database=demo1; Integrated security=SSPI;")
        con.Open()

        Dim query As String
        query = "delete from Demorecord1 where name='" & TextBox1.Text & "'"

        cmd = New SqlCommand(query, con)

        cmd.CommandText = query
        cmd.ExecuteNonQuery()

    End Sub

    Public Sub display()

        Dim cmd As New SqlCommand("select * from Demorecord1 where Name='" & TextBox1.Text & "'")
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        da = New SqlDataAdapter(cmd)

        Dim ds As New DataSet("Demorecord1")
        ds.Tables.Add("DemoDataSet", "Demorecord1")
        da.Fill(ds, "DemoDataSet")

        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

        TextBox1.DataBindings.Add("text", ds.Tables(0), "Name")
        TextBox2.DataBindings.Add("text", ds.Tables(0), "Pass")

        con.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DemoDataSet.Demorecord' table. You can move, or remove it, as needed.
        Me.DemorecordTableAdapter.Fill(Me.DemoDataSet.Demorecord)


    End Sub

    Private Sub DemorecordBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub


    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

End Class

above is form1.vb

database is Demo1
table is Demorecord1 which contains field Name and Pass only
Insert query is executing successfully
due to datagrid cant execute update or delete query
Please suggest me or tell me my mistake

In DeleteData() procedure, you mention the commandText Property two times, One in Line 71. cmd = New SqlCommand(query, con) Second one is at Line 73. cmd.CommandText = query . Remove the second One.

But I do not find your Update query and also dataGridView Control.

Iam trying to modify Your Delete and Disply Sub proceduer, if you don't mind.

deletedata procedure will be as

 Private Sub deletedata()
        con = New SqlConnection("data source=USER-PC; database=demo1; Integrated security=SSPI;")
        con.Open()
        Dim query As String
        query = "Delete From Demorecord1 Where name='" & TextBox1.Text & "'"
        cmd = New SqlCommand(query, con)
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        con.Close()
    End Sub

Display procedure will be as

Public Sub display()
        Dim cmd As New SqlCommand("Select * From Demorecord1 Where Name='" & TextBox1.Text & "'")
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        dr=cmd.ExecuteReader

        If dr.HasRows Then
            dr.Read()
            TextBox2.Text=dr.Item("Pass")
        endif
        dr.close()
        cmd.Dispose()
        con.Close()
    End Sub

Again, I also suggest you always do your query Parameterized.

Edited 2 Years Ago by Santanu Das

You could try changing your connection string to

SqlConnection("Server=.\SQLEXPRESS;Database=demo1;Trusted_Connection=yes;")

Change the server name to whatever your local installation is named.

This question has already been answered. Start a new discussion instead.