Custom Control with Connection String

A

Andrew Robinson

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
 
J

JV

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:

System.Diagnostics.Debugger.Break();
 
R

Rob T

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

Dim debug As System.Diagnostics.Debug

debug.write(myvalues & vbcrlf)
 
K

Kevin Yu [MSFT]

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
rights."
 
N

~~~ .NET Ed ~~~

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.
 
S

Steven Cheng[MSFT]

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
http://groups.google.com/groups?hl=zh-CN&lr=&threadm=uqDHTrtdEHA.3988@tk2m
sftngp13.phx.gbl&rnum=1&prev=/groups%3Fhl%3Dzh-CN%26lr%3D%26q%3D%2523%2BGett
ing%2BProject%2BFolder%2Bduring%2BDesign%2BTime


here are some similiar VB.NET code :

[VB.Net]
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Module DTEUtilsModule DTEUtils

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

Public Enum VisualStudioVersionEnum VisualStudioVersion
[VS2002]
[VS2003]
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 &
System.Diagnostics.Process.GetCurrentProcess().Id.ToString()
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 =
GetVisualStudioMoniker(VisualStudioVersion.VS2002)
Dim objDTE As Object = GetMSDEVFromGIT(strMoniker)

If objDTE Is Nothing Then
strMoniker =
GetVisualStudioMoniker(VisualStudioVersion.VS2003)
objDTE = GetMSDEVFromGIT(strMoniker)
End If

Return objDTE
End Function

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

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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strUrl = proj.Properties.Item("URL").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strPath = proj.Properties.Item("LocalPath").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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)
prot.EnumRunning(pMonkEnum)
pMonkEnum.Reset()

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 =
Me.GetService(GetType(IWebFormsDocumentService))
Return svc.DocumentUrl

End Function

End Module


HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Andrew Robinson

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


Steven Cheng said:
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
http://groups.google.com/groups?hl=...l&rnum=1&prev=/groups?hl=zh-CN&lr=&q=%23+Gett
ing%2BProject%2BFolder%2Bduring%2BDesign%2BTime


here are some similiar VB.NET code :

[VB.Net]
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Module DTEUtilsModule DTEUtils

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

Public Enum VisualStudioVersionEnum VisualStudioVersion
[VS2002]
[VS2003]
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 &
System.Diagnostics.Process.GetCurrentProcess().Id.ToString()
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 =
GetVisualStudioMoniker(VisualStudioVersion.VS2002)
Dim objDTE As Object = GetMSDEVFromGIT(strMoniker)

If objDTE Is Nothing Then
strMoniker =
GetVisualStudioMoniker(VisualStudioVersion.VS2003)
objDTE = GetMSDEVFromGIT(strMoniker)
End If

Return objDTE
End Function

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

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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strUrl = proj.Properties.Item("URL").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strPath = proj.Properties.Item("LocalPath").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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)
prot.EnumRunning(pMonkEnum)
pMonkEnum.Reset()

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 =
Me.GetService(GetType(IWebFormsDocumentService))
Return svc.DocumentUrl

End Function

End Module


HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Andrew Robinson

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


Steven Cheng said:
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
http://groups.google.com/groups?hl=...l&rnum=1&prev=/groups?hl=zh-CN&lr=&q=%23+Gett
ing%2BProject%2BFolder%2Bduring%2BDesign%2BTime


here are some similiar VB.NET code :

[VB.Net]
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Module DTEUtilsModule DTEUtils

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

Public Enum VisualStudioVersionEnum VisualStudioVersion
[VS2002]
[VS2003]
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 &
System.Diagnostics.Process.GetCurrentProcess().Id.ToString()
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 =
GetVisualStudioMoniker(VisualStudioVersion.VS2002)
Dim objDTE As Object = GetMSDEVFromGIT(strMoniker)

If objDTE Is Nothing Then
strMoniker =
GetVisualStudioMoniker(VisualStudioVersion.VS2003)
objDTE = GetMSDEVFromGIT(strMoniker)
End If

Return objDTE
End Function

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

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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strUrl = proj.Properties.Item("URL").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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

Try
Dim projs() As Object = DTE.ActiveSolutionProjects
Dim proj As Object = projs(0)
strPath = proj.Properties.Item("LocalPath").Value
Catch exc As Exception
Console.WriteLine(exc.Message,
exc.GetBaseException.GetType.Name)
Console.WriteLine(exc.StackTrace)
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)
prot.EnumRunning(pMonkEnum)
pMonkEnum.Reset()

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 =
Me.GetService(GetType(IWebFormsDocumentService))
Return svc.DocumentUrl

End Function

End Module


HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

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 asp.net'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 asp.net runtime context to access the cache.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

recoil

I honestly would not worry about breaking future versions of vs.net.
We still get developers wanting .NET 1.0 and I get the feeling vs.net
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.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top