Code behind problem

G

Guest

Hi,

I have a .aspx file named testConnStr.aspx witin a try, catch, finally
block that works just very nice, shows the data in a datagrid. I have one
header saying "test ConnStr" and two server objects, a label and a datagrid.

So I decided to do it with code behind.

What happens?

Everything is normal, but the output is ONLY the header "test ConnStr"!!!!

The two server objects are vaniched.

Any ideas?

TIA

Kenneth P
 
J

Johann MacDonagh

Could you clarify or post some sample code? Did you have a single file and
tried to convert it to a code behind setup?

Thanks,
Johann MacDonagh
 
G

Guest

Hi Johann,

This is the one that works.

******************************************************
<%@ Page Language="vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)

Dim ConnStr as String

ConnStr =
"SERVER=localhost;LANGUAGE=swedish;DATABASE=Diskoweb;UID=sa;pwd=ohlywohly;"

Dim conn As SqlConnection = New SqlConnection(ConnStr)

Try
conn.Open()

Status.Text = "Connection strängen lyckades!"

Dim da As SqlDataAdapter = New SqlDataAdapter ("Select * From
calculate Order By Users_ID", conn)

Dim ds As New DataSet
da.Fill(ds,"calculate")

DataGrid1.DataSource=ds.Tables("calculate").Defaultview
DataGrid1.DataBind()

Catch ex As SqlException
Status.Text = ex.Message

Finally
conn.Close()

End Try

End Sub

Sub dgrdTitles_SortCommand( s As Object, e As
DataGridSortCommandEventArgs)
'BindDataGrid( e.SortExpression )
End Sub

</script>
<html>
<head>
<title>Sample ASP.NET Page</title>
</head>
<body>
<h1>Koppla till datakällan med 'Connection objektet'
</h1>
<asp:label id="Status" runat="server"></asp:label>
<br />
<br />
<asp:DataGrid id="DataGrid1" runat="server" font-names="verdana"
font-size="x-small" BorderWidth="1" BorderColor="black" BorderStyle="solid"
GridLines="none" CellSpacing="2" CellPadding="2" AutoGenerateColumns="False">
<AlternatingItemStyle backcolor="palegoldenrod" />
<ItemStyle backcolor="beige" />
<HeaderStyle forecolor="white" backcolor="brown" font-bold="true" />
<columns>
<asp:BoundColumn runat="server" DataField="Users_ID"
HeaderText="AnvID" />
<asp:BoundColumn runat="server" DataField="CalcDateTime"
HeaderText="Datum/Tid" />
<asp:BoundColumn runat="server" DataField="DataPattern"
HeaderText="Mönster" />
<asp:BoundColumn runat="server" DataField="BerGrund"
HeaderText="Grund" />
<asp:BoundColumn runat="server" DataField="ProcSats"
HeaderText="%" DataFormatString="{0:c2}" />
<asp:BoundColumn runat="server" DataField="DatKonv"
HeaderText="DatumKonv" />
<asp:BoundColumn runat="server" DataField="StartDate"
HeaderText="Startdatum" DataFormatString="{0:d}" />
<asp:BoundColumn runat="server" DataField="EndDate"
HeaderText="Sludatum" DataFormatString="{0:d}" />
<asp:BoundColumn runat="server" DataField="dblAmount"
HeaderText="Belopp" DataFormatString="{0:c2}" />
</columns>
</asp:DataGrid>
</body>
</html>
***************************************************

And here's the

********************************************************
<%@ Page Language="vb" Debug="True" AutoEventWireup="false"
Codebehind="testConnStr.aspx.vb" Inherits="testSqlServ2kConnStr.testConnStr"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>testConnStr</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<h1>Koppla till datakällan med 'Connection objektet'
</h1>
<asp:label id="Status" runat="server"></asp:label>
<br>
<br>
<asp:DataGrid id="DataGrid1" runat="server" font-names="verdana"
font-size="x-small" BorderWidth="1"
BorderColor="black" BorderStyle="solid" GridLines="none" CellSpacing="2"
CellPadding="2" AutoGenerateColumns="False">
<AlternatingItemStyle backcolor="palegoldenrod" />
<ItemStyle backcolor="beige" />
<HeaderStyle forecolor="white" backcolor="brown" font-bold="true" />
<columns>
<asp:BoundColumn runat="server" DataField="Users_ID" HeaderText="AnvID" />
<asp:BoundColumn runat="server" DataField="CalcDateTime"
HeaderText="Datum/Tid" />
<asp:BoundColumn runat="server" DataField="DataPattern"
HeaderText="Mönster" />
<asp:BoundColumn runat="server" DataField="BerGrund" HeaderText="Grund" />
<asp:BoundColumn runat="server" DataField="ProcSats" HeaderText="%"
DataFormatString="{0:c2}" />
<asp:BoundColumn runat="server" DataField="DatKonv"
HeaderText="DatumKonv" />
<asp:BoundColumn runat="server" DataField="StartDate"
HeaderText="Startdatum" DataFormatString="{0:d}" />
<asp:BoundColumn runat="server" DataField="EndDate"
HeaderText="Sludatum" DataFormatString="{0:d}" />
<asp:BoundColumn runat="server" DataField="dblAmount"
HeaderText="Belopp" DataFormatString="{0:c2}" />
</columns>
</asp:DataGrid>
</body>
</HTML>
********************************

And the code behund

*********************************
Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Imports System.IO

Public Class testConnStr
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Status As System.Web.UI.WebControls.Label
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim ConnStr As String

ConnStr =
"SERVER=localhost;LANGUAGE=swedish;DATABASE=Diskoweb;UID=sa;pwd=ohlywohly;"

Dim conn As SqlConnection = New SqlConnection(ConnStr)

Try
conn.Open()

Status.Text = "Connection strängen lyckades!"

Dim da As SqlDataAdapter = New SqlDataAdapter("Select * From
calculate Order By Users_ID", conn)

Dim ds As New DataSet
da.Fill(ds, "calculate")

DataGrid1.DataSource = ds.Tables("calculate").DefaultView
DataGrid1.DataBind()

Catch ex As SqlException
Status.Text = ex.Message

Finally
conn.Close()

End Try

End Sub

Sub dgrdTitles_SortCommand(ByVal s As Object, ByVal e As
DataGridSortCommandEventArgs)
'BindDataGrid( e.SortExpression )
End Sub

End Class

********************

Kenneth P
 
G

Guest

Johann,

I've made a shorter one, the only thing you have to change is the
coonectiostring to one of your own

<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<%@ Page Language="vb" %>
<HTML>
<HEAD>
<title>Sample ASP.NET Page</title>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
Dim ConnStr as String
ConnStr =
"SERVER=localhost;LANGUAGE=swedish;DATABASE=Diskoweb;UID=sa;pwd=ohlywohly;"
Dim conn As SqlConnection = New SqlConnection(ConnStr)
Try
conn.Open()
Status.Text = "Connection string OK!"
Catch ex As SqlException
Status.Text = ex.Message
Finally
conn.Close()
End Try
End Sub

</script>
</HEAD>
<body>
<h1>Connect to datasource
</h1>
<asp:label id="Status" runat="server"></asp:label>
<br>
<br>
</body>
</HTML>

***********************************************

and here's the code behind that only shows the header info "Coonect to
datasource"

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="connStrSql.aspx.vb" Inherits="testSqlServ2kConnStr.connStrSql"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>connStrSql</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<h1>Connect to datasource
</h1>
<asp:label id="Status" runat="server"></asp:label>
<br>
<br>
</body>
</HTML>

********

Imports System.Data.SqlClient
Imports System.Data

Public Class connStrSql
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Status As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim ConnStr As String
ConnStr =
"SERVER=localhost;LANGUAGE=swedish;DATABASE=Diskoweb;UID=sa;pwd=ohlywohly;"
Dim conn As SqlConnection = New SqlConnection(ConnStr)
Try
conn.Open()
Status.Text = "Connection string OK!"
Catch ex As SqlException
Status.Text = ex.Message
Finally
conn.Close()
End Try
End Sub

End Class

Kenneth P
 
J

Johann MacDonagh

Sorry it took so long to reply. I had some trouble installing VB.net to my
Visual Studio 2003 installation. I'm still having trouble, so I'll try to
answer your question without being able to test it out.

The problem is that the Page_Load method is not hooked up to an event.
ASP.net does not know that it needs to run Page_Load when the Page reaches
the Load event, because you have not told it to do so.

Change the line:

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

to:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

In doing that, you are telling ASP.net to call that method when the Page
reaches the Load event. Otherwise, it's just arbitrary code sitting there.

Hope this helps,
Johann MacDonagh
 

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,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top