Hi,
the following code displays a search result in a datagrid and is working
without problems. But I think the way I have done is not correct especially
databind(). could anyone help me to rearange it?
Sub grdProfilesResu lt_Edit(ByVal sender As Object, ByVal e As
DataGridCommand EventArgs)
grdProfilesResu lt.EditItemInde x = e.Item.ItemInde x
grdProfilesResu lt.DataSource = Session("PDataS ource")
grdProfilesResu lt.DataBind()
End Sub
Sub grdProfilesResu lt_Cancel(ByVal Sender As Object, ByVal e As
DataGridCommand EventArgs)
grdProfilesResu lt.EditItemInde x = e.Item.ItemInde x - 1
grdProfilesResu lt.DataSource = Session("PDataS ource")
grdProfilesResu lt.DataBind()
End Sub
Sub grdProfilesResu lt_Update(ByVal Sender As Object, ByVal e As
DataGridCommand EventArgs)
If Page.IsValid Then
Session("PDataS ource") = Nothing
Dim strID As String
strID = CType(e.Item.Fi ndControl("txtI D"), Label).Text
Dim strName As String
strName = CType(e.Item.Fi ndControl("txtN ame"), TextBox).Text
Dim Con As New Data.OleDb.OleD bConnection( _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=c:\InetP ub\wwwroot\Mach ineShop\Data\Ma chineShop.mdb")
Dim cmd As New OleDbCommand
cmd.CommandText = "Update Profiles SET Name = '" & strName & "'
Where MilitaryID = '" & strID & "'"
cmd.CommandType = CommandType.Tex t
Con.Open()
Try
cmd.ExecuteNonQ uery()
Catch ex As Exception
Response.Write( ex.ToString)
Finally
Con.Close()
End Try
grdProfilesResu lt.EditItemInde x = -1
BindData()
End If
End Sub
Private Sub BindData()
lblMessage.Visi ble = False
Dim SQLADDER As String = ""
Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand
Dim rdr As OleDbDataReader
Dim txtProfileID As String = Request.Form("t xtProfileID")
Dim txtProfileName As String = Request.Form("t xtProfileName")
Dim txtRank As String = Request.Form("t xtRank")
Dim txtProfession As String = Request.Form("t xtProfession")
If LCase(txtProfil eID) <> "any" Then
SQLADDER = SQLADDER & " AND ID = '" & txtProfileID & "' "
End If
If LCase(txtProfil eName) <> "any" Then
SQLADDER = SQLADDER & " AND Name = '" & txtProfileName & "' "
End If
If LCase(txtRank) <> "any" Then
SQLADDER = SQLADDER & " AND Rank = '" & txtRank & "' "
End If
If LCase(txtProfes sion) <> "any" Then
SQLADDER = SQLADDER & " AND Profession = '" & txtProfession & "'
"
End If
If Len(SQLADDER) > 0 Then
SQLADDER = " WHERE " & Mid(SQLADDER, 5)
End If
Dim strSQL As String = "Select * from Profiles " & SQLADDER
Dim Con As New Data.OleDb.OleD bConnection( _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=c:\InetP ub\wwwroot\Mach ineShop\Data\Sh op.mdb")
Con.Open()
cmd = New OleDbCommand(st rSQL, Con)
Dim adapter As OleDbDataAdapte r = New OleDbDataAdapte r
adapter.SelectC ommand = cmd
Dim PDataSet As DataSet = New DataSet
adapter.Fill(PD ataSet)
Dim boolFlag As Boolean = True
If PDataSet.Tables Is Nothing Or PDataSet.Tables (0).Rows.Count = 0
Then
lblMessage.Visi ble = True
lblMessage.Fore Color = System.Drawing. Color.Red
lblMessage.Text = ("No record available for the selection
criteria.")
boolFlag = False
Else
Session("PDataS ource") = PDataSet
boolFlag = True
End If
If boolFlag = True Then
grdProfilesResu lt.DataSource = PDataSet
grdProfilesResu lt.DataBind()
End If
cmd.Dispose()
Con.Close()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub grdProfilesResu lt_PageIndexCha nged(ByVal sender As
System.Object, ByVal e As
System.Web.UI.W ebControls.Data GridPageChanged EventArgs) Handles
grdProfilesResu lt.PageIndexCha nged
grdProfilesResu lt.CurrentPageI ndex = e.NewPageIndex
grdProfilesResu lt.DataSource = Session("PDataS ource")
grdProfilesResu lt.DataBind()
End Sub
Here datagrid_update is not working, "Instance of object not found " error
occurs.
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/