DataBind

  • Thread starter Thread starter RN1
  • Start date Start date
R

RN1

Consider the following code which binds records from a DB to a
Repeater:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(".....")
sqlDapter = New SqlDataAdapter("SELECT * FROM NETUsers",
sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"
rptrUsers.DataBind()

sqlConn.Close()
End Sub

Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
If (Page.IsPostBack) Then
rptrUsers.DataBind()
End If
End Sub
</script>

<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemDataBound="BindData"
runat="server">
<HeaderTemplate>
<table>
<tr>
<th>NAME</th>
<th>PHONE</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<th>
<asp:LinkButton ID="lnkName" Text='<%# Container.DataItem("FirstName")
& " " & Container.DataItem("LastName") %>' runat="server"></
asp:LinkButton>
</th>
<th><%# Container.DataItem("Phone") %></th>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>

When the page loads for the first time, the Repeater displays all the
data from the DB but when I click a LinkButton in the Repeater, I get
an error that says "Server Application Unavailable".

The If condition in the sub named BindData, I believe, is the cause of
the error but why is that line causing the error?

Thanks,

Ron
 
Consider the following code which binds records from a DB to a
Repeater:

<script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
        Dim dSet As DataSet
        Dim sqlConn As SqlConnection
        Dim sqlDapter As SqlDataAdapter

        sqlConn = New SqlConnection(".....")
        sqlDapter = New SqlDataAdapter("SELECT * FROM NETUsers",
sqlConn)

        dSet = New DataSet()
        sqlDapter.Fill(dSet, "Users")

        rptrUsers.DataSource = dSet
        rptrUsers.DataMember = "Users"
        rptrUsers.DataBind()

        sqlConn.Close()
    End Sub

    Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
        If (Page.IsPostBack) Then
            rptrUsers.DataBind()
        End If
    End Sub
</script>

<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemDataBound="BindData"
runat="server">
<HeaderTemplate>
<table>
<tr>
<th>NAME</th>
<th>PHONE</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<th>
<asp:LinkButton ID="lnkName" Text='<%# Container.DataItem("FirstName")
& " " & Container.DataItem("LastName") %>' runat="server"></
asp:LinkButton>
</th>
<th><%# Container.DataItem("Phone") %></th>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>

When the page loads for the first time, the Repeater displays all the
data from the DB but when I click a LinkButton in the Repeater, I get
an error that says "Server Application Unavailable".

The If condition in the sub named BindData, I believe, is the cause of
the error but why is that line causing the error?

Thanks,

Ron

Hi Ron

I don't know if this will help but I notice that your code is such
that when you click the link button the rptUser.DataBind() method is
execute twice, once during Page_load and again during the click event,
with no changes to the data.

Try adding the condition in Page_load:

if(not IsPostback) then rptUsers.DataBind() end if
 
Back
Top