Solved

Bind Gridview to dataset using VB.NET

Posted on 2009-05-06
2
4,309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

734 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