Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 30th, 2008, 01:22 AM
Registered User
 
Join Date: Sep 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to asad_black
Default update records problem in datagrid

i have datagrid with edit column and also a textfield which search records.

when i edit record without searching records it updates record succesfully.

but when i search record and click on the edit link it goes to first item of the datagrid...!

for example:

if i search asad it retrieve the asad's row but when i click on the edit link it moves to first item of the datagrid.

my code is:



<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="Server">

    Sub btnSearch_OnClick(ByVal sender As Object, ByVal e As EventArgs)
        dgrdlogin.CurrentPageIndex = 0
        ShowDataGrid()
    End Sub

    Sub ShowDataGrid()
        Dim objConnection As OleDbConnection
        Dim objCommand As OleDbCommand
        Dim objAdapter As OleDbDataAdapter
        Dim objDataSet As DataSet
        Dim strSearch As String
        Dim strSQLQuery As String

        strSearch = txtSearch.Text

        If Len(Trim(strSearch)) > 0 Then
            ' Set up our connection.
            objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
              & "DATA SOURCE=" _
                  & Server.MapPath("nwind.mdb;"))

            ' Set up our SQL query text.
            strSQLQuery = "SELECT * from login " _
                & "WHERE firstname LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
                 & "ORDER BY firstname;"
            '& "OR lastname LIKE '%" & Replace(strSearch, "'", "''") & "%' " _


            ' Create new command object passing it our SQL query
            ' and telling it which connection to use.
            objCommand = New OleDbCommand(strSQLQuery, objConnection)

            ' Get a DataSet to bind the DataGrid to
            objAdapter = New OleDbDataAdapter(objCommand)
            objDataSet = New DataSet()
            objAdapter.Fill(objDataSet)

            ' DataBind DG to DS
            dgrdlogin.DataSource = objDataSet
            dgrdlogin.DataBind()

            objConnection.Close()
        Else
            txtSearch.Text = "Enter Search Here"
        End If
    End Sub



    Dim conNorthwind As OleDbConnection

    Dim cmdSql As OleDbCommand

    Dim strSql As String

    Sub Page_Load()

        conNorthwind = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
             & "DATA SOURCE=" _
                 & Server.MapPath("nwind.mdb;"))

        If Not IsPostBack Then

            doBinding()

        End If

    End Sub

    Sub doBinding(Optional ByVal sortBy As String = "id")


        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; " _
                      & "DATA SOURCE=" _
                          & Server.MapPath("nwind.mdb;")

        Dim dbConnection As OleDbConnection = New OleDbConnection(strConn)



        Dim strSQL As String = "SELECT * FROM login order by firstname "





        Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter(strSQL, strConn)



        Dim dataSet As DataSet = New DataSet

        dataAdapter.Fill(dataSet)



        '=== databind to DataGrid called dgSocks

        dgrdlogin.DataSource = dataSet

        dgrdlogin.DataBind()





    End Sub



    Sub pager(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs)

        dgrdlogin.CurrentPageIndex = e.NewPageIndex

        doBinding()

    End Sub

    Sub dgrdlogin_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

        dgrdlogin.EditItemIndex = e.Item.ItemIndex

        doBinding()

    End Sub

    Sub dgrdlogin_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

        Dim intmyID As Integer

        Dim txtusername As TextBox

        Dim strusername As String

        Dim txtpw As TextBox

        Dim strpw As String


        Dim txtfirstname As TextBox

        Dim strfirstname As String

        Dim txtlastname As TextBox

        Dim strlastname As String



        intmyID = dgrdlogin.DataKeys(e.Item.ItemIndex)

        txtfirstname = e.Item.Cells(1).Controls(0)


        strfirstname = txtfirstname.Text

        txtpw = e.Item.Cells(4).Controls(0)

        strpw = txtpw.Text

        txtusername = e.Item.Cells(3).Controls(0)

        strusername = txtusername.Text

        txtlastname = e.Item.Cells(2).Controls(0)

        strlastname = txtlastname.Text




        strSql = "Update login Set firstname=@firstname, lastname=@lastname, username=@username, pw=@pw " _
        & "where ID=@myID"

        '
        cmdSql = New OleDbCommand(strSql, conNorthwind)



        cmdSql.Parameters.Add("@firstname", strfirstname)

        cmdSql.Parameters.Add("@lastname", strlastname)




        cmdSql.Parameters.Add("@username", strusername)

        cmdSql.Parameters.Add("@pw", strpw)

        cmdSql.Parameters.Add("@myID", intmyID)

        conNorthwind.Open()

        cmdSql.ExecuteNonQuery()

        conNorthwind.Close()

        dgrdlogin.EditItemIndex = -1

        doBinding()
        Response.Write("update successfully")


    End Sub

    Sub dgrdlogin_CancelCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

        dgrdlogin.EditItemIndex = -1

        doBinding()

    End Sub

</script>

<html>
<head>
    <title>ExpertDataGridEditlogin.aspx</title>
</head>
<body>
    <form id="Form1" runat="Server">
        <asp:DataGrid ID="dgrdlogin" OnEditCommand="dgrdlogin_EditCommand" OnUpdateCommand="dgrdlogin_UpdateCommand"
            OnCancelCommand="dgrdlogin_CancelCommand" DataKeyField="ID" AutoGenerateColumns="False"
            CellPadding="10" HeaderStyle-BackColor="LightGrey" AllowPaging="true" PageSize="5"
            OnPageIndexChanged="pager" PagerStyle-HorizontalAlign="center" PagerStyle-Mode="NumericPages"
            runat="Server" BackColor="White">
            <Columns>
                <asp:BoundColumn HeaderText="Employee ID" DataField="ID" ReadOnly="True" />
                <asp:BoundColumn HeaderText="First Name" DataField="firstname" />
                <asp:BoundColumn HeaderText="Last Name" DataField="lastname" />
                <asp:BoundColumn HeaderText="User Name" DataField="username" />
                <asp:BoundColumn HeaderText="Password" DataField="pw" />
                <asp:EditCommandColumn EditText="Edit!" UpdateText="Update!" CancelText="Cancel!" />
            </Columns>
            <HeaderStyle BackColor="LightGray" />
        </asp:DataGrid>
        <asp:TextBox ID="txtSearch" runat="server" Text="please Enter First Name" />
        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_OnClick" />
    </form>
</body>
</html>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Update Problem In DataGrid prasanta2expert ASP.NET 1.0 and 1.1 Basics 7 January 9th, 2007 07:36 AM
Update Problem In DataGrid prasanta2expert ASP.NET 1.0 and 1.1 Professional 3 January 6th, 2007 03:55 AM
Problem with update database in datagrid annie_stwg ASP.NET 1.0 and 1.1 Basics 4 April 1st, 2006 12:10 AM
Update Records bspeck Dreamweaver (all versions) 5 October 5th, 2004 03:05 PM
Another Chapter 17 problem (DataGrid Update) bolibompa BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 May 11th, 2004 12:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.