Proper DataBinding

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

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 grdProfilesResult_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
grdProfilesResult.EditItemIndex = e.Item.ItemIndex
grdProfilesResult.DataSource = Session("PDataSource")
grdProfilesResult.DataBind()
End Sub

Sub grdProfilesResult_Cancel(ByVal Sender As Object, ByVal e As
DataGridCommandEventArgs)
grdProfilesResult.EditItemIndex = e.Item.ItemIndex - 1
grdProfilesResult.DataSource = Session("PDataSource")
grdProfilesResult.DataBind()
End Sub

Sub grdProfilesResult_Update(ByVal Sender As Object, ByVal e As
DataGridCommandEventArgs)

If Page.IsValid Then

Session("PDataSource") = Nothing

Dim strID As String
strID = CType(e.Item.FindControl("txtID"), Label).Text

Dim strName As String
strName = CType(e.Item.FindControl("txtName"), TextBox).Text

Dim Con As New Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\InetPub\wwwroot\MachineShop\Data\MachineShop.mdb")

Dim cmd As New OleDbCommand

cmd.CommandText = "Update Profiles SET Name = '" & strName & "'
Where MilitaryID = '" & strID & "'"
cmd.CommandType = CommandType.Text
Con.Open()

Try
cmd.ExecuteNonQuery()

Catch ex As Exception
Response.Write(ex.ToString)
Finally
Con.Close()
End Try

grdProfilesResult.EditItemIndex = -1
BindData()

End If
End Sub

Private Sub BindData()
lblMessage.Visible = False

Dim SQLADDER As String = ""
Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand
Dim rdr As OleDbDataReader

Dim txtProfileID As String = Request.Form("txtProfileID")
Dim txtProfileName As String = Request.Form("txtProfileName")
Dim txtRank As String = Request.Form("txtRank")
Dim txtProfession As String = Request.Form("txtProfession")

If LCase(txtProfileID) <> "any" Then
SQLADDER = SQLADDER & " AND ID = '" & txtProfileID & "' "
End If

If LCase(txtProfileName) <> "any" Then
SQLADDER = SQLADDER & " AND Name = '" & txtProfileName & "' "
End If

If LCase(txtRank) <> "any" Then
SQLADDER = SQLADDER & " AND Rank = '" & txtRank & "' "
End If

If LCase(txtProfession) <> "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.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\InetPub\wwwroot\MachineShop\Data\Shop.mdb")

Con.Open()

cmd = New OleDbCommand(strSQL, Con)
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter
adapter.SelectCommand = cmd

Dim PDataSet As DataSet = New DataSet
adapter.Fill(PDataSet)
Dim boolFlag As Boolean = True

If PDataSet.Tables Is Nothing Or PDataSet.Tables(0).Rows.Count = 0
Then
lblMessage.Visible = True
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = ("No record available for the selection
criteria.")
boolFlag = False
Else
Session("PDataSource") = PDataSet
boolFlag = True
End If

If boolFlag = True Then
grdProfilesResult.DataSource = PDataSet
grdProfilesResult.DataBind()
End If
cmd.Dispose()
Con.Close()
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindData()
End If

End Sub

Private Sub grdProfilesResult_PageIndexChanged(ByVal sender As
System.Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
grdProfilesResult.PageIndexChanged
grdProfilesResult.CurrentPageIndex = e.NewPageIndex
grdProfilesResult.DataSource = Session("PDataSource")
grdProfilesResult.DataBind()
End Sub

Here datagrid_update is not working, "Instance of object not found " error
occurs.
 
D

David Jessee

Man....you've got a lot of code there....

go to the Exception Catch block and either (1) Place a Breakpoint there and
Inspect ex when the program goes into break mode, or (2) have it write out
ex.Message & ex.StackTrace to see exactly which procedure call is raising
the error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top