Help with UserControl in Code Behind

Discussion in 'ASP .Net Web Controls' started by GVaught, Jun 11, 2004.

  1. GVaught

    GVaught Guest

    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!
     
    GVaught, Jun 11, 2004
    #1
    1. Advertising

  2. GVaught

    ranganh Guest

    Hi,

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

    "GVaught" wrote:

    > 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!
    >
     
    ranganh, Jun 11, 2004
    #2
    1. Advertising

  3. GVaught

    GVaught Guest

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

    "ranganh" <> wrote in message
    news:...
    > Hi,
    >
    > Can you explain what you want to have in usercontrol and what is that you

    are facing problem.
    >
    > "GVaught" wrote:
    >
    > > 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!
    > >
     
    GVaught, Jun 11, 2004
    #3
  4. GVaught

    ranganh Guest

    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.




    "GVaught" wrote:

    > I thought I did that. did you read the entire post?
    >
    > "ranganh" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > Can you explain what you want to have in usercontrol and what is that you

    > are facing problem.
    > >
    > > "GVaught" wrote:
    > >
    > > > 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!
    > > >

    >
    >
    >
     
    ranganh, Jun 11, 2004
    #4
  5. GVaught

    GVaught Guest

    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.

    "ranganh" <> wrote in message
    news:...
    > 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.
    >
    >
    >
    >
    > "GVaught" wrote:
    >
    > > I thought I did that. did you read the entire post?
    > >
    > > "ranganh" <> wrote in message
    > > news:...
    > > > Hi,
    > > >
    > > > Can you explain what you want to have in usercontrol and what is that

    you
    > > are facing problem.
    > > >
    > > > "GVaught" wrote:
    > > >
    > > > > 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!
    > > > >

    > >
    > >
    > >
     
    GVaught, Jun 13, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    655
    Alphonse Giambrone
    Jun 28, 2003
  2. Michael Evanchik
    Replies:
    1
    Views:
    446
    Michael Evanchik
    Jul 30, 2003
  3. Shiva
    Replies:
    1
    Views:
    2,729
    Shiva
    Sep 2, 2004
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    540
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Reik
    Replies:
    1
    Views:
    259
    Brock Allen
    Apr 12, 2005
Loading...

Share This Page