Solved

Bind Gridview to dataset using VB.NET

Posted on 2009-05-06
2
4,303 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

786 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