473,408 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Code behind problem

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

Nov 19 '05 #1
4 1281
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

"Kenneth P" <Ke******@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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

Nov 19 '05 #2
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=Diskow eb;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").Defaul tview
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=Diskow eb;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

Nov 19 '05 #3
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=Diskow eb;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=Diskow eb;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

Nov 19 '05 #4
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

"Kenneth P" <Kenneth P@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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=Diskow eb;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").Defaul tview
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=Diskow eb;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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ursula Peter-Czichi | last post by:
Found a different way to do it - onto the next problem... >-----Original Message----- >Software: WIN XP Professional, IIS 5.1, Visual Studio.Net >2002 - >I have figured out why my .aspx files...
2
by: Mark Fox | last post by:
Hello, In VS.NET 2003 when I add server controls to the web form it sometimes adds their declarations to the code behind and sometimes not. Is there a way to tell VS.NET to parse the web form...
5
by: Girish | last post by:
hey all, im building a asp.net project in vs.net. Is there a way to have the codebehind files sit in a different folder outside the website folder altogether? example: The aspx files are in...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
1
by: Me | last post by:
Hi, I inherited a web application that has several very similar forms for gathering user data. User's must choose the form that best fits their needs and fill it out. Each form has about 15...
1
by: pbishop | last post by:
Hi, I'm fairly new to ASP.Net. I have Visual Web Developer 2005 Express. Except for the default.aspx, whenever I create a new Web form, no code behind (aspx.vb) page is created. The same thing...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
5
by: Joe | last post by:
Hi, When I start a new VS2005 website project, have a .aspx page and I drop a control onto it and when I double-click on the event, it brings up the .aspx.vb file so that I can add the VB event...
5
by: mesut | last post by:
Hi there, how are you colleagues? I try to set a linkaddress in code behind for a <asp:hyperlinkserver control. but I think I have some syntax problem. I don't know how to fix it. What's...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.