By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

Proper DataBinding

P: n/a
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\Machine Shop.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.md b")

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.DataGridPageChangedEvent Args) 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.
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"Luis Esteban Valencia" <lu*******@haceb.com> wrote in message
news:uh*************@TK2MSFTNGP09.phx.gbl...
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\Machine Shop.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.md b")

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.DataGridPageChangedEvent Args) 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.
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/

Nov 19 '05 #2

P: n/a

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.DataGridPageChangedEvent Args) Handles
grdProfilesResult.PageIndexChanged
grdProfilesResult.CurrentPageIndex = e.NewPageIndex
grdProfilesResult.DataSource = Session("PDataSource")
grdProfilesResult.DataBind()
End Sub

How can I overcome these problems?

ayha

--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/
"David Jessee" <dj*****@houston.rr.com> escribió en el mensaje
news:e6**************@TK2MSFTNGP12.phx.gbl...
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.

"Luis Esteban Valencia" <lu*******@haceb.com> wrote in message
news:uh*************@TK2MSFTNGP09.phx.gbl...
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\Machine Shop.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.md b")

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.DataGridPageChangedEvent Args) 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.
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/


Nov 19 '05 #3

P: n/a
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 id="x" runat="server" DataSource='<%# TheDataSource %>'
<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?
"Luis Esteban Valencia" <lu*******@haceb.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.DataGridPageChangedEvent Args) Handles
grdProfilesResult.PageIndexChanged
grdProfilesResult.CurrentPageIndex = e.NewPageIndex
grdProfilesResult.DataSource = Session("PDataSource")
grdProfilesResult.DataBind()
End Sub

How can I overcome these problems?

ayha

--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/
"David Jessee" <dj*****@houston.rr.com> escribió en el mensaje
news:e6**************@TK2MSFTNGP12.phx.gbl...
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.

"Luis Esteban Valencia" <lu*******@haceb.com> wrote in message
news:uh*************@TK2MSFTNGP09.phx.gbl...
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\Machine Shop.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.md b")
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.DataGridPageChangedEvent Args) 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.
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/



Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.