Solved

Bind Gridview to dataset using VB.NET

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now