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

Luis Esteban Valencia

I will explain the problems;

1. Now it takes two clicks for updating.

2. After updating only ID appears in the updated row, all other row values
disappers but the actual data is there in the table.

3. After updating with two clicks "No record available for the selection
criteria".

4. How can I avoid using Session from the fllowing sub and just put
databind().

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

How can I overcome these problems?

ayha
 
D

David Jessee

Well, for #4, I can tell you how I do databinding. It might work for you...

In your code Behind....

Protected Readonly Property TheDataSource() as Datatable
Get
' The code that gets your data table for binding, based on all
'Of your page properties (prefereable from some otehr data access
'Class so that all of the implementation doesn't clutter the page
Return theDataTable
End Get
End Property

then in your aspx file

<asp:dataGrid>

so when you call databind, your grid knows enough to bind itself.

For #3, Why is that happening, is it because the table is not being created,
or is it becauser no rows are returned?
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top