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