• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1126
  • Last Modified:

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)
0
R8VI
Asked:
R8VI
  • 3
  • 3
2 Solutions
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now