Help with UserControl in Code Behind

G

GVaught

PatrioticsignI have an aspx page that references a user-control called connect-strings.ascx. This user control uses connections to various databases. Below is the part I am using.

'declared at the top
<%@Control Language="VB" %> ' declare user control

Public ReadOnly Property SQLConnectionString() As String
Get
'*****************************************************************
Return "server=(local);initial catalog=choice;Connection Timeout=20;Integrated Security=SSPI" '*****************************************************************
End Get
End Property
I did have my Page_Load within my aspx page, which worked fine. However, I decided I wanted to move the info to my code-behind file. I pretty much have everything else defined within my code-behind page except this one line of code:

Dim strConnect As String = New ctlConnectStrings.SQLConnectionString 'problem is underllined

In my declarations I've tried many things. I've reviewed my own ASP.Net book on User Controls, My help within VS.Net 2003 to no avail. This is what I have so far:

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.UserControl
Imports System.Data

Public Class WebOrder
Inherits System.Web.UI.Page

Protected WithEvents ddlClients As System.Web.UI.WebControls.DropDownList
Protected WithEvents dgrClients As System.Web.UI.WebControls.DataGrid
Protected WithEvents outError As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents msgHello As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents SqlConnection As System.Data.SqlClient.SqlConnection
'Here I've tried the following:
Protected WithEvents ctlConnectStrings AS System.Web.UI.UserControl

I also tried writing a separate new Class and referencing the Inherits UserControl and then I wasn't sure what to enter after that and the End Class statement. I did add the appropriate references inside the connect-strings.ascx @control statement.

Inside my aspx page I have these referenced entries. However, they were there mainly because of the Page_Load code.
<%@ Register TagPrefix="wrox" TagName="connect" src="connect-strings.ascx" %>
<wrox:connect id="ctlConnectStrings" runat="server">


I seem not to find the magic formula for this one line in the code-behind page.

'Code-behind file called WebOrder.aspx.vb

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

'see if the user has been authenticated
If User.Identity.IsAuthenticated Then

'display the properties
msgHello.InnerHtml = "End of Deliveries for <b>" & User.Identity.Name & "</b><br />"

Else

msgHello.InnerHtml = "I'm sorry, you were not authenticated"

End If

If Not IsPostBack Then
Dim strConnect As String = New ctlConnectStrings.SQLConnectionString 'problem underlined
Dim objConnect As New SqlConnection(strConnect)
Dim cmd As SqlCommand
objConnect.Open()

cmd = New SqlCommand("select * from tblClientsSub WHERE Account='" & User.Identity.Name & "'", objConnect)
ddlClients.DataSource = cmd.ExecuteReader()
ddlClients.DataTextField = "ClientName"
ddlClients.DataValueField = "ClientSubCtr"
ddlClients.DataBind()

objConnect.Close()

End If

End Sub

HELP!
 
R

ranganh

Hi,

Can you explain what you want to have in usercontrol and what is that you are facing problem.
 
G

GVaught

I thought I did that. did you read the entire post?

ranganh said:
Hi,

Can you explain what you want to have in usercontrol and what is that you are facing problem.
connect-strings.ascx. This user control uses connections to various
databases. Below is the part I am using.Timeout=20;Integrated Security=SSPI"
'*****************************************************************I decided I wanted to move the info to my code-behind file. I pretty much
have everything else defined within my code-behind page except this one line
of code:book on User Controls, My help within VS.Net 2003 to no avail. This is what
I have so far:UserControl and then I wasn't sure what to enter after that and the End
Class statement. I did add the appropriate references inside the
connect-strings.ascx @control statement.
 
R

ranganh

Dear GVaught,

You have to declare an instance of the Usercontrol as follows:-


Dim uc as New ctlConnectStrings()

then you can access the proprety as follows:-

string strConnect = uc.SQLConnectionString;

hope it helps.
 
G

GVaught

Thanks for the info. I tried your suggestion several ways to no avail. I
finally decided to place the reference to the server info directly in the
Code behind page for my WebOrder.aspx. It is connecting now.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top