Solved

Bind Gridview to dataset using VB.NET

Posted on 2009-05-06
2
4,300 Views
Last Modified: 2013-11-08
I have a datagridview that I need to bind to a dataset that contains data from a Access database table.   The datagridview needs to allow the user to edit existing data as well as add new data.   When the user is done I need to have whatever changes are made to existing data as well as the new data saved back to the database.   What is the best approach to take to acomplish this?

I am able to populate the dataset and bind it to the grid.  My problem is save the changes back to the database.
0
Comment
Question by:Mike_Stevens
2 Comments
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24318322
0
 
LVL 6

Accepted Solution

by:
BALMUKUND KESHAV earned 500 total points
ID: 24334157
Here is an ASPX page retriving/editing/updating/deleting  data from mysql table using VB.net
Pl.make necessary correcitons at your end a/c to your environment and use :

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="MySql.Data" %>
<%@ Import Namespace="MySql.Data.MysqlClient" %>

<script runat=server>
    Private strConnection As String = "server=192.168.1.44; user id=bmk; password=bmkpass; database=test; pooling=false;"
   
    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        If Not IsPostBack Then
            BuildDataList()
        End If
    End Sub
   
    Sub Edit_Grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        dgEmps.EditItemIndex = e.Item.ItemIndex
        BuildDataList()
    End Sub

    Sub CancelEdit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
        dgEmps.EditItemIndex = -1
        BuildDataList()
    End Sub
   
Sub Update_Grid(ByVal Sender as Object, ByVal E as DataGridCommandEventArgs)
    Dim TheID as String
    Dim LastName as String
    Dim FirstName as String
    TheID = E.Item.Cells(2).Text
    LastName = CType(e.Item.Cells(0).Controls(0), TextBox).Text
    FirstName = CType(e.Item.Cells(1).Controls(0), TextBox).Text
        Dim DBConn As MySqlConnection
        Dim DBUpdate As New MySqlCommand
        DBConn = New MySqlConnection(strConnection)
    DBUpdate.CommandText = "Update Employee " _
        & "Set LastName = '" & LastName &"', " _
        & "FirstName = '" & FirstName & "' " _
        & "Where ID = " & TheID
       DBUpdate.Connection = DBConn
        DBUpdate.Connection.Open()
        DBUpdate.ExecuteNonQuery()
    DBConn.Close
    dgEmps.EditItemIndex = -1
    BuildDataList
End Sub
    Sub Delete_Grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        dgEmps.EditItemIndex = e.Item.ItemIndex
        Dim TheID As String
        TheID = e.Item.Cells(2).Text
        Dim DBConn As MySqlConnection
        Dim DBdelete As New MySqlCommand
        DBConn = New MySqlConnection(strConnection)
        DBdelete.CommandText = "delete from Employee " _
            & "Where ID = " & TheID
        DBdelete.Connection = DBConn
        DBdelete.Connection.Open()
        DBdelete.ExecuteNonQuery()
        DBConn.Close()
        dgEmps.EditItemIndex = -1
        BuildDataList()
    End Sub

    Sub CommandAdd_Click(ByVal Sender As Object, ByVal E As CommandEventArgs)
        Dim DBConn As MySqlConnection
        Dim DBAdd As New MySqlCommand
        DBConn = New MySqlConnection(strConnection)
        DBAdd.CommandText = "Insert Into Employee (LastName) " _
        & "values ('')"
        DBAdd.Connection = DBConn
        DBAdd.Connection.Open()
        DBAdd.ExecuteNonQuery()
        DBConn.Close()
        dgEmps.EditItemIndex = 0
        BuildDataList()
    End Sub
Sub BuildDataList ()
        Dim DBConn As MySqlConnection
        Dim DBCommand As MySqlDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New MySqlConnection(strConnection)
        DBCommand = New MySqlDataAdapter _
            ("Select * From Employee " _
            & "Order By LastName, FirstName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employee")
        dgEmps.DataSource = _
            DSPageData.Tables("Employee").DefaultView
        dgEmps.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Adding Rows to a DataGrid Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:Label
    id="lblMessage"
    runat="server"
/>
<BR><BR>
<asp:datagrid
    id="dgEmps"
    runat="server"
    autogeneratecolumns="false"
    oneditcommand="Edit_Grid"
    oncancelcommand="Canceledit"
    onupdatecommand="Update_Grid"
    ondeletecommand="Delete_Grid"
    >
    <columns>
        <asp:boundcolumn
            HeaderText="Last Name"
            DataField="LastName"
        />
        <asp:boundcolumn
            HeaderText="First Name"
            DataField="FirstName"
        />
        <asp:boundcolumn
            datafield="ID"
            visible=False
            readonly=true
        />
        <asp:editcommandcolumn
             edittext="Edit"
             updatetext="Update"
             canceltext="Cancel"
             itemstyle-wrap="false"
             headertext="Edit"
             headerStyle-wrap="false"
        />
        <asp:ButtonColumn Text="Delete" CommandName="Delete"/>
    </columns>
</asp:datagrid>
<BR><BR>
  <asp:LinkButton
    id="butAdd"
    text="Add"
    commandname="Add"
    oncommand="CommandAdd_Click"
    runat="server"
  />
</form>
</BODY>
</HTML>


Bm Keshav

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now