Visual Studio, and sharepoint web parts

have the following piece of code with all the references added. and the connection string stored in my web config file. I build the project and copy and paste the dll to the server where my sharepoint bin directory is. But when i load the web part the information retrieved from the database is not displayed.
I get the error message that I have pasted below the code.
Can someone please help and point out where i am going wrong. Any help is much appreciated thanks in advance

 public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
    {
        //public int yourVar = 0;

        public class foo
        {

           public static int yourVar;

       }

       public static int GetYourDataObject()
        {
            using (SqlConnection connection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constring1"].ConnectionString))
            {
                connection1.Open();
                using (SqlCommand command1 = new SqlCommand("SELECT COUNT(x) As COUNTVAR FROM table", connection1))
                {
                     foo.yourVar = Convert.ToInt32(command1.ExecuteReader());
                    return foo.yourVar;


                }

            }


        }
        protected override void RenderWebPart(HtmlTextWriter output)
        {
           foo.yourVar = GetYourDataObject();
            output.Write(@"</BR><span style='font-size:10pt;'>Display the number: </span>");
            output.Write(@"<span style='color:#61AE09; font-size:11pt;'>" + foo.yourVar + "</span></BR>");

        }

    }

Error Message in Sharepoint

An error occured when previewing the Web Part.   at Microsoft.SharePoint.WebPartPages.WebPartPreview.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at ASP._layouts_wpprevw_aspx.__Render__control9(HtmlTextWriter __w, Control parameterContainer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Page.Render(HtmlTextWriter writer)
   at Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
R8VIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

REA_ANDREWCommented:
Just out of interest replace

using (SqlConnection connection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constring1"].ConnectionString))

with this

using (SqlConnection connection1 = new SqlConnection("Your actual connection string not a reference"))

Could you tell me if this works like this.  I do not propose to keep it like this, just want to dig a little deeper.

Andrew
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
R8VIAuthor Commented:
Hi,

I have tried that and still get the same error that I have pasted before.
Were am i going wrong.
Any help is much appreciated

Thanks
0
REA_ANDREWCommented:
try this now to test:

Andrew
        protected override void RenderWebPart(HtmlTextWriter output)
        {
           base.RenderWebPart(output);
           //foo.yourVar = GetYourDataObject();
           // output.Write(@"</BR><span style='font-size:10pt;'>Display the number: </span>");
           // output.Write(@"<span style='color:#61AE09; font-size:11pt;'>" + foo.yourVar + "</span></BR>");
 
        }

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

R8VIAuthor Commented:
This is what is shown

This is a preview of the Web Part. To use this Web Part, open a Web Part Page and then add this Web Part from the ToolPane.  

The name of the solution. none of the output is shown as it is commented out

Thanks Again
 
 
 

 
0
REA_ANDREWCommented:
Create a usercontrol and add this usercontrol to the webpart.
        protected override void RenderWebPart(HtmlTextWriter output)
        {
           YourCustomControl custom1 = new YourCustomControl();
           Controls.Add(custom1);
           base.RenderWebPart(output);
           //foo.yourVar = GetYourDataObject();
           // output.Write(@"</BR><span style='font-size:10pt;'>Display the number: </span>");
           // output.Write(@"<span style='color:#61AE09; font-size:11pt;'>" + foo.yourVar + "</span></BR>");
 
        }

Open in new window

0
R8VIAuthor Commented:
Sorry about this but i am confused now
I replaced my code with ur last code and have these errors

Error      1      The type or namespace name 'YourCustomControl' could not be found (are you missing a using directive or an assembly reference?)      

Error      3      The best overloaded method match for 'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some invalid arguments      

Error      4      Argument '1': cannot convert from 'YourCustomControl' to 'System.Web.UI.Control'      
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.