Community discussion forum

Problem in update Records when i search Record in datagrid (new)

  • 1 month ago

    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" %>

    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

    ExpertDataGridEditlogin.aspx

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 1 month ago

    http://forums.asp.net/t/1326643.aspx

    this is my actual problem

Post a reply

Enter your message below

Sign in or Join us (it's free).