?
Solved

User Control Connection String Property

Posted on 2007-10-01
16
Medium Priority
?
778 Views
Last Modified: 2013-12-17
I have a Server(User) Control that takes 2 properties(ConnectionString&ConnectionStringName) from the eveloper at design time;
1) ConectionString:
 [Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Bindable(true)]
        [Category("Misc")]
        [DefaultValue("")]
        [Localizable(true)]
        public virtual string ConnectionString
        {
            get
            {
                String s = (String)ViewState["ConnectionString"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ConnectionString"] = value;
            }
        }
2) ConnectionStringName:
        [Bindable(true)]
        [Category("Misc")]
        [DefaultValue("")]
        [Localizable(true)]
        public virtual string ConnectionStringName
        {
            get
            {
                String s = (String)ViewState["ConnectionStringName"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["ConnectionStringName"] = value;
            }
        }
And OnLoad event of my server control I write that code if ConnectionString is null then take the named (ConnectionStringName Property) connection string from Web.Config like;
        protected override void OnLoad(EventArgs e)
        {
            if (ConnectionString == null || ConnectionString.Trim()=="")
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString.ToString();
            }
            base.OnLoad(e);
        }
    }

My problem is;
Now in design time  when ConnectionStringName is written but ConnectionString not wriiten by the developer, I got this error;
""The 'ConnectionString' property has not been initialized."
What should I do to correct this error message?
I'm using VS 2005 ASP.Net with C#.
Thanks,
0
Comment
Question by:kosturdur
  • 8
  • 7
16 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 19989570
give a default value for your "ConnectionString" varialbe...say ""
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19989801
Hi jaiganeshsrinivasan,
---give a default value for your "ConnectionString" varialbe...say ""
if I do that it gives that error instead;
"Format of the initialization string does not conform the specification starting index at 0."
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19989804
I thing the Editor;
 [Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

don't allow empty string.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Rejojohny
ID: 19992081
the code that you have in load needs to be moved with the property for the connetionstring ..
   public virtual string ConnectionString
        {
            get
            {
                String s = (String)ViewState["ConnectionString"];

if (s == null || s.Trim()=="")
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString.ToString();
            }
                return s;
            }

This will ensure that if connectionstring is not set, it reads from web.config .. if connectionstring peroperty is set by with a different string, then that new value will be used ..

Rejo
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19994133
Hi Rejojohny,
When I changed the property like;
        [Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Bindable(true)]
        [Category("Misc")]
        [DefaultValue((string)null)]
        [Localizable(true)]
        public virtual string ConnectionString
        {
            get
            {
                String s = (String)ViewState["ConnectionString"];
                if (s == null || s == "")
                {
                    s = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString.ToString();
                }
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                String ss = value;
                if (ss == null || ss == "") { value = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString.ToString(); }
                ViewState["ConnectionString"] = value;
            }
        }
This time I got this message on the control and near ConnectionString property at design time ;
"Object reference not set to an instance of an object."

At run-time it works perfect.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19994764
which line?

Rejo
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19994766
I mean which line do you get tha error?

Rejo
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19996966
"Object reference not set to an instance of an object." message is near the property's name in Property window and on the server control that is on a page.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19998108
don't you get a compilation error? did you try and add a breakpoint to check which line in the control gives that error ..

Rejo
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19998536
No there is no compilation error. It work at run time. This is just a message, not a compilation error or run time error.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19998676
>>This is just a message
do you have error handlers (try catch blocks) in your control? if yes, is it showing error in messagebox? debug .. add a breakpoint within the control to check where the message is been triggered from ..

Rejo
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19999074
Rejojohny, I sent an e-mail to you with the screenshot of the message.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20000095
I saw the screenshot .. as I said, its a runtime error within the control .. you can even debug the control  by adding a breakpoint ...
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20000129
is you control part of the same project or a different project?
0
 
LVL 5

Author Comment

by:kosturdur
ID: 20005476
It's a different project.
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 20031290
add the project as to the existing project where you are consuming the control to create a "solution" . Then you will be able to debug this "control" project by adding a breakpoint within the code..

Sorry for been late with the response .. have been tied up  with work ..

Rejo
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question