&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="AccessDataSource1" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="AccessDataSource1" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        
       Dim sqlinsert As String

        sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
          "VALUES(@sysaccount, @syspw)"

        Dim cmd As New OleDbCommand(sqlinsert, con1)

    cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
        cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))

        Try
            con1.Open()
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        End Try

        con1.Close()

    End Sub

This is my program so far, I can add a new record, but I have to close all program and rerun it again, I will see the new record was in the gridview.


this program view;


gridview is on the top.

outside under the gridview there are two textboxes.
textbox1
textbox2


addbutton savebutton


(when click addbutton, the curson will go to the textbox1... after finish text in, click the savebutton, the savebutton will do the insert a new record, but how to do the refresh with the savebutton. Thanks.)

Dani AI

Generated

Quick diagnosis and options (short): the symptom you hit comes from mixing declarative data binding (GridView.DataSourceID -> AccessDataSource) with manual binding. You have two safe routes:

  • Keep the AccessDataSource in markup and let it do CRUD. After your INSERT you must rebind so the GridView re-reads the AccessDataSource (this is the quick fix suggested by ). Also make sure the AccessDataSource has proper Insert/Update/Delete commands and that the GridView’s DataKeyNames is set to the primary key (id) so built‑in delete/update work.

  • Or remove DataSourceID and control everything in code (the approach in ’s BindGridView). If you choose this, you must implement deleting/updating yourself (GridView events + SQL) because the declarative AccessDataSource is gone.

Common pitfalls and fixes

  • Don’t use WinForms code: DataGridView.CurrentRow is not available in ASP.NET GridView. Use GridView DataKeys or the RowDeleting/RowCommand events to get the row id.
  • With OleDb/Jet the parameter order matters (OleDb treats parameters positionally). Either use ? placeholders or ensure you add parameters in the exact order used in the SQL.
  • Avoid server-side MsgBox for web apps — use a Label or logging, and always dispose connections (Using blocks) or close in Finally.

Example: handle GridView RowDeleting (manual-binding route)

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
    Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)
    Using conn As New OleDb.OleDbConnection(connectionString)
        Using cmd As New OleDb.OleDbCommand("DELETE FROM sysdep WHERE id = ?", conn)
            cmd.Parameters.AddWithValue("?", id)
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
    GridView1.DataBind()   ' rebind after change
End Sub

Checklist to get everything working

  • If you keep DataSourceID: configure AccessDataSource Delete/Update and DataKeyNames="id".
  • If you bind in code: set DataKeyNames, handle RowDeleting/RowCommand, and rebind after inserts/deletes.
  • Use Using blocks, proper parameter ordering, and show errors to the browser (Label) instead of MsgBox.

Following those suggestions will let refresh the GridView immediately after save and restore delete/update functionality depending on which binding model is chosen.

Recommended Answers

All 4 Replies

Try adding the line below to the bottom of your Button4_Click sub.

GridView1.DataBind()

Hi,

You need to bind the GridView, after inserting records into the database. Here is the sample code.

Imports System.Data
Imports System.Data.OleDb
Partial Class DemoPage1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridView()
        End If
    End Sub

    Private Sub BindGridView()
        ' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
        Dim dt As DataTable = New DataTable()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
        conn.Open()
        da.Fill(dt)
        da.Dispose()
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        'Code to add new record to SQL Server table
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")

        Dim sqlinsert As String

        sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
          "VALUES(@sysaccount, @syspw)"

        Dim cmd As New OleDbCommand(sqlinsert, con1)

        cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
        cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))

        Try
            con1.Open()
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        End Try
        con1.Close()

        BindGridView()
    End Sub
End Class

Hi,

You need to bind the GridView, after inserting records into the database. Here is the sample code.

Imports System.Data
Imports System.Data.OleDb
Partial Class DemoPage1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridView()
        End If
    End Sub

    Private Sub BindGridView()
        ' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
        Dim dt As DataTable = New DataTable()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
        conn.Open()
        da.Fill(dt)
        da.Dispose()
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        'Code to add new record to SQL Server table
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")

        Dim sqlinsert As String

        sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
          "VALUES(@sysaccount, @syspw)"

        Dim cmd As New OleDbCommand(sqlinsert, con1)

        cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
        cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))

        Try
            con1.Open()
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web Message")
        End Try
        con1.Close()

        BindGridView()
    End Sub
End Class

Thanks your code, I will try this on Monday, ACTUALLY, I DID SOMTHING ELSE (refresh()) TO GET A PROBLEM, YOU KNOW THE DATASOURCE/DATASOURCEID THAT HAS TO BE REOMVED, SO I DELETE ALL REFRESH(), anyway, I WILL TRY YOUR SAMPLE TO SEE IF I WILL GET THE SAME PROBLEM DATASOURCE/DATASOURCEID. Thanks.

Thanks your code, I will try this on Monday, ACTUALLY, I DID SOMTHING ELSE (refresh()) TO GET A PROBLEM, YOU KNOW THE DATASOURCE/DATASOURCEID THAT HAS TO BE REOMVED, SO I DELETE ALL REFRESH(), anyway, I WILL TRY YOUR SAMPLE TO SEE IF I WILL GET THE SAME PROBLEM DATASOURCE/DATASOURCEID. Thanks.

Thanks,again, it was the datasource/datasoruceid problem after I binding the access, and I delete the datasourceid on the html, it is working, but all the bulid the delete,update are not working, I got error message on girdview1 not handle the rowdeleting, how am I going to fix this.

Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click

        'Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        'Dim sqldelete As String
        'Dim i As Integer

        'sqldelete = "DELETE FROM sysdep WHERE id=" & DATAGRIDVIEW.CURRENTROW.Cells(0).ToString

        ''Debug.Print(DataGridView1.CurrentRow.Cells(0).Value)

        'Dim cmd2 As New OleDbCommand(sqldelete, con1)

        'Try
        '    con1.Open()
        '    cmd2.ExecuteNonQuery()
        'Catch ex As OleDbException
        '    MsgBox(ex.Message, MsgBoxStyle.Critical, "error")
        'Catch ex As InvalidOperationException
        '    MsgBox(ex.Message, MsgBoxStyle.Critical, "error")
        'Catch ex As Exception
        '    MsgBox(ex.Message, MsgBoxStyle.Critical, "error")
        'End Try


        'con1.Close()

        'BindGridView()

    End Sub

Thanks. don't know how to fix the DATAGRIDVIEW.CURRENTROW.Cells(0).ToString
, actually this is not for the currentrow?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.