Custom Control with Connection String

Discussion in 'ASP .Net Building Controls' started by Andrew Robinson, Mar 25, 2005.

  1. I have a custom control that makes connections to a SQL db. The controls
    works fine at run time, but it not able to get the connection string in my
    web config file at design time. I had something similar working in a
    previous project, but can't seem to figure out where or how they are
    different. I know this might be a grey area, but any ideas?

    Secondly, is there any standard way of debugging a custom control at design
    time? I have been catching error messages and then just outputting them as
    text to the HtmlTextWriter. Which seems to work ok, but is there possibly a
    better way?

    Andrew Robinson, Mar 25, 2005
    1. Advertisements

  2. Andrew Robinson

    JV Guest

    I tried something ugly: I ran another instance of VS.NET and attached the
    debugger to that instance's process. It worked, but VS.NET ends up totally
    confused in some cases and you have to exit both and start over, though.

    I also just set a breakpoint in the code and noticed it got thrown in some
    surprising instances. I still haven't figured out why. However, keep in
    mind that you can insert a line of code to always throw a breakpoint:

    JV, Mar 25, 2005
    1. Advertisements

  3. Andrew Robinson

    Rob T Guest

    I usually send info to VS's output window...

    Dim debug As System.Diagnostics.Debug

    debug.write(myvalues & vbcrlf)
    Rob T, Mar 25, 2005
  4. Hi Andrew,

    We have reviewed this issue and are currently researching on your first
    question. We will update you ASAP. Thanks for your patience!

    Kevin Yu
    "This posting is provided "AS IS" with no warranties, and confers no
    Kevin Yu [MSFT], Mar 26, 2005
  5. Hell Andrew,
    I also had a problem with custom web controls having a database
    dependency. In the end I had to opt for detecting if it was in design time
    (Context == null, would be nice if like WinForms there was a DesignTime
    property) and then if so simply skip all database related activity
    (databinding etc.)

    As for debugging controls I usually output the info on Debug. If things get
    really ugly with the control (and they usually do) then I:

    1. Select the project that holds the custom controls (a DLL library) and
    make it the default startup project
    2. Alter the properties of the custom controls library project (right mouse
    click on the project)
    a) Select the "Configuration Properties" branch (has a folder icon)
    b) Select the "Debugging" sub-branch
    c) Change the debug type to "Program"
    d) Select the absolute path to devenv.exe as the program to start
    3. Set a breakpoint on the control you are trying to debug (I usually begin
    at the design class)
    4. Press F5 (Start Debugging). A new instance of VS.NET is started, in this
    new instance open the solution/project you want to debug (the same one as
    the "mother" devenv).

    Then if things are ok, as soon as you open a webform in the designer where
    your control is used, DevEnv will try to render your control (design-time
    !!!) and hopefully your breakpoint will come to life and you can do your
    code surgery.
    ~~~ .NET Ed ~~~, Mar 26, 2005
  6. Thanks for all your informative inputs.

    Hi Andrew,

    As for accessing web.config's settings at Design-time, there hasn't any
    buildin API like the System.Configuration.... we used at runtime. However,
    we can use the VS.NET's design-time interfaces to locate the project's
    folder and then read any files in that folder(Read the web.config as a
    normal xml file). Here is a former thread discussing on such question:

    #Getting Project Folder during Design Time

    here are some similiar VB.NET code :

    Imports System.ComponentModel
    Imports System.Runtime.InteropServices

    Module DTEUtilsModule DTEUtils

    Public ReadOnly VS2002MonikerBase As String = "!VisualStudio.DTE.7:"
    Public ReadOnly VS2003MonikerBase As String =

    Public Enum VisualStudioVersionEnum VisualStudioVersion
    End Enum

    Private Class Win32APIClass Win32API
    <DllImport("ole32.dll")> _
    Public Shared Function GetRunningObjectTable()Function
    GetRunningObjectTable(ByVal reserved As
    Integer, <Out()> ByRef prot As UCOMIRunningObjectTable) As Integer
    End Function

    <DllImport("ole32.dll")> _
    Public Shared Function CreateBindCtx()Function CreateBindCtx(ByVal
    reserved As Integer,
    <Out()> ByRef ppbc As UCOMIBindCtx) As Integer
    End Function
    End Class

    Public Overloads Function GetVisualStudioMoniker()Function
    GetVisualStudioMoniker(ByVal version As
    VisualStudioVersion) As String
    Dim strBase As String

    Select Case version
    Case VisualStudioVersion.VS2002
    strBase = VS2002MonikerBase
    Case VisualStudioVersion.VS2003
    strBase = VS2003MonikerBase
    End Select

    Return strBase &
    End Function

    Public Overloads Function GetVisualStudioMoniker()Function
    GetVisualStudioMoniker() As String
    Return GetVisualStudioMoniker(VisualStudioVersion.VS2002)
    End Function

    Public Function GetDTE()Function GetDTE() As Object
    Dim strMoniker As String =
    Dim objDTE As Object = GetMSDEVFromGIT(strMoniker)

    If objDTE Is Nothing Then
    strMoniker =
    objDTE = GetMSDEVFromGIT(strMoniker)
    End If

    Return objDTE
    End Function

    Public Overloads Function GetProjectUrl()Function GetProjectUrl() As
    Return GetProjectUrl(GetDTE())
    End Function

    Public Overloads Function GetProjectUrl()Function GetProjectUrl(ByVal
    DTE As Object) As
    Dim strUrl As String

    Dim projs() As Object = DTE.ActiveSolutionProjects
    Dim proj As Object = projs(0)
    strUrl = proj.Properties.Item("URL").Value
    Catch exc As Exception
    End Try

    If Right(strUrl, 1) = "/" Then strUrl = Left(strUrl, Len(strUrl)
    - 1)

    Return strUrl
    End Function

    Public Overloads Function GetProjectLocalPath()Function
    GetProjectLocalPath() As String
    Return GetProjectLocalPath(GetDTE())
    End Function

    Public Overloads Function GetProjectLocalPath()Function
    GetProjectLocalPath(ByVal DTE As Object)
    As String
    Dim strPath As String

    Dim projs() As Object = DTE.ActiveSolutionProjects
    Dim proj As Object = projs(0)
    strPath = proj.Properties.Item("LocalPath").Value
    Catch exc As Exception
    End Try

    If Right(strPath, 1) = "/" Then strPath = Left(strPath,
    Len(strPath) - 1)

    Return strPath
    End Function

    Public Function GetMSDEVFromGIT()Function GetMSDEVFromGIT(ByVal
    strProgID As String) As Object

    Dim prot As UCOMIRunningObjectTable
    Dim pMonkEnum As UCOMIEnumMoniker

    Win32API.GetRunningObjectTable(0, prot)

    Dim fetched As Integer

    Dim pmon(1) As UCOMIMoniker

    While pMonkEnum.Next(1, pmon, fetched) = 0
    Dim pCtx As UCOMIBindCtx
    Win32API.CreateBindCtx(0, pCtx)

    Dim str As String
    pmon(0).GetDisplayName(pCtx, Nothing, str)

    If str = strProgID Then
    Dim objReturnObject As Object
    prot.GetObject(pmon(0), objReturnObject)
    Return objReturnObject

    End If

    End While

    Return Nothing

    End Function

    Public Function GetCurrentWebFormUrl()Function GetCurrentWebFormUrl()

    Dim svc As IWebFormsDocumentService =
    Return svc.DocumentUrl

    End Function

    End Module

    HTH. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no
    Steven Cheng[MSFT], Mar 28, 2005
  7. Thanks,

    Don't know that I like the answer. Looks like it will break with future
    versions of the IDE. Not something that I want to do right now.


    Andrew Robinson, Mar 28, 2005
  8. Another possible direction?

    Is it possible to gain access to the ASP.NET Cache object at design time?

    I could store values here at design time using a GUID instead of gaining
    access to SQL. I think this would work well for my needs, but I am having
    trouble instantiating the cache object.


    Andrew Robinson, Mar 29, 2005
  9. Hi Andrew,

    Thanks for your followup. First, you're right on your concerns on the
    compatibility problems with the VS.NET IDE's design-time interfaces as
    they'll be changed when new version is released.(every version of VS.NET
    will have some diference with the former one).

    And as for the acessing the's Cache at design-time, I'm afraid this
    is not possible since the ASP.NET's Cache is a runtime memory based storage
    which exists in the ASP.NET's runtime process's memory. So in design-time
    we haven't a certain runtime context to access the cache.


    Steven Cheng
    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no
    Steven Cheng[MSFT], Mar 29, 2005
  10. Andrew Robinson

    recoil Guest

    I honestly would not worry about breaking future versions of
    We still get developers wanting .NET 1.0 and I get the feeling
    2k3 will be the same way. Developers will still be utilizing the tools
    3-4 years down the line and thus an initial investment will probably be
    worth it. Maintaing support for 4-5 different IDE versions however does
    start getting a bit hairy.
    recoil, Mar 29, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.