User Control Connection String Property

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,
LVL 5
kosturdurAsked:
Who is Participating?
 
RejojohnyConnect With a Mentor Commented:
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
 
Jai STech ArchCommented:
give a default value for your "ConnectionString" varialbe...say ""
0
 
kosturdurAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
kosturdurAuthor Commented:
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
 
RejojohnyCommented:
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
 
kosturdurAuthor Commented:
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
 
RejojohnyCommented:
which line?

Rejo
0
 
RejojohnyCommented:
I mean which line do you get tha error?

Rejo
0
 
kosturdurAuthor Commented:
"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
 
RejojohnyCommented:
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
 
kosturdurAuthor Commented:
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
 
RejojohnyCommented:
>>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
 
kosturdurAuthor Commented:
Rejojohny, I sent an e-mail to you with the screenshot of the message.
0
 
RejojohnyCommented:
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
 
RejojohnyCommented:
is you control part of the same project or a different project?
0
 
kosturdurAuthor Commented:
It's a different project.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.