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

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.