SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3

Customize the webpart properties for dynamic connection to SQL server and to enable paging in Grid view.

In Part 2 of the post, we looked at how to execute a SQL statement against a database and display the result in the web part as a gridview. Before going to Part 3 of developing a reusable SQL Data viewer web part for Sharepoint, please follow the steps in Part II. 

In Part III of the post, we will enhance this web part and add properties to the web part so that connection to SQL Server and the SQL statement can be configured after the webpart is added to the page. We will also throw in an eventhandler for the gridview to handle paging. If you have lots of records, we want them to show in pages. We will also look at how to configure the page size through properties.


Note: The example below shows how to save configuration data in properties in plain text.If you feel such a method is not suitable for your security needs, you should find a way to use encryption on the data being saved in the properties and a method to decrypt that when accessing from code. 


We will first create a webpart property called ServerName and also a method called ServerName

See Code Listing Below


public class DataViewer : WebPart

{


        private string servername;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("ServerName"), Category("Data Properties")]

        public string ServerName

        {

            get { return servername; }

            set { servername = value; }

        }


In the above code, we declared a private data member called servername and a get,set method called ServerName


We also created a webpart property called “ServerName” which will be displayed in a section called “Data Properties”


We will now create some more properties, see code listing below

 

    public class DataViewer : WebPart

    {

        private string servername;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("ServerName"), Category("Data Properties")]

        public string ServerName

        {

            get { return servername; }

            set { servername = value; }

        }

        private string dbname;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("DatabaseName"), Category("Data Properties")]

        public string DatabaseName

        {

            get { return dbname; }

            set { dbname = value; }

        }

        private string username;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("UserName"), Category("Data Properties")]

        public string UserName

        {

            get { return username; }

            set { username = value; }

        }

        private string password;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("Password"), Category("Data Properties")]

        public string Password

        {

            get { return password; }

            set { password = value; }

        }

        private string innerpassword;

        [Personalizable(), WebBrowsable(false),

        WebDisplayName("InnerPassword"), Category("Data Properties")]

        public string InnerPassword

        {

            get { return innerpassword; }

            set { innerpassword = value; }

        }

        private string sqlquery;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("SQLQuery"), Category("Data Properties")]

        public string SQLQuery

        {

            get { return sqlquery; }

            set { sqlquery = value; }

        }

        private int datarows;

        [Personalizable(), WebBrowsable(true),

        WebDisplayName("RowsPerPage"), Category("Data Properties")]

        public int DataRows

        {

            get { return datarows; }

            set { datarows = value; }

        }


All the properties are similar in declaration accept for a property called “InnerPassword”. You will notice that the WebBrowsable property for innerpassword is set to false.  This property will not be visible to users and it is created so that the password can be stored internally in this property.


Build the project by clicking on Build>>Build Dataviewer


Once you have successfully build the assembly, go to the sharepoint site where the dataviewer webpart was originally added and refresh the page.

Modify the dataviewer webpart properties and you will see a section called “Data Properties” as shown below

 

The “Data Properties” section havs 6 text boxes.

·         Server Name = [name of the SQL Server]

·         DatabaseName= Name of the Database

·         UserName= SQL server login account

·         Password = SQL Server Password

·         SQLQuery= T-SQL Statement to query the database

·         RowsPerPage = number of rows to be displayed in the gridview for a page


In the next section, we will now add code to capture the values entered in these properties and then execute it in the webpart.


We will modify the CreateChildMethods and instead of using static SQL Strings or connections, we will read the connection and sql statement from the properties.


The following codes have been added

if (password != "******")

{

     InnerPassword = password;

     password = "******";

}


string strConnection = "UID=" + username + ";PWD=" + innerpassword + ";Initial Catalog=" + dbname + ";Data Source=" + servername + ";";

                   

string sql = sqlquery;

objDB.StrConnection = strConnection;

DataTable dt = new DataTable();

dt = objDB.GetSQlResult(sql);


Let’s examine the code above. If the password ( this is the password property) is not equal to “******” then we will assign the password to Innerpassword and set the password to “******”


The reason for doing this is, if someone entered a SQL Server password on the webpart, we do not want to show the password to the user again, instead we will show “******” to prevent other people from accessing the password of SQL server by looking at the properties.


In the next line, we are building the SQL server connection string by reading the properties of the web part.

In the sql string we get the value from the sqlquery property of the webpart.


We are now dynamically reading the SQL connection string and the query from the webpart properties, next step is just execute the GetResult method of our DBAccess object and bind the datatable returned to the gridview.


We will now add code to enable paging in the gridview. We also need to specify the page size. We will set the allowpaging property of gridview to true and also add an eventhandler to enable paging for gridview. The pagesize will be assigned from the “datarows” properties.

 

gv.AllowPaging = true;

gv.PageSize = datarows;

gv.PageIndexChanging += new GridViewPageEventHandler(this.gv_PageIndexChanging);


We will need to manually create the method for the event handler as below


protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)

       

{

      gv.PageIndex = e.NewPageIndex;

      string strConnection = "UID=" + username + ";PWD=" + innerpassword + ";Initial Catalog=" + dbname + ";Data Source=" +    servername + ";";

     

      DbAccess objdb = new DbAccess();

      objdb.StrConnection = strConnection;

      DataTable dt = objdb.GetSQlResult(sqlquery);

      gv.DataSource = dt;

      gv.DataBind();

}


Note: The gridview should not be instantiated inside the CreateChildControls Method anymore because the gv_PageIndexChanging will not be able to access the object, we will therefore instantiate the gridview as a member of the class.


  public class DataViewer : WebPart

    {

        GridView gv = new GridView();


Build the project. Go to the sharepoint page where the webpart has been added previously. Refresh the page.When the webpart reloads you may see an error message. This is expected because the database connections and SQL query has not been set.


 

 

You will now need to edit the dataview webpart properties to make the error go away and execute your SQL Statement.


Change the Title to : Employees

Expand the Dataproperties section and enter your connection information


 

 

Once the properties have been entered, click “Apply” and then click “OK”. You will now see that the web part displays the result of your sql query.

Important: The RowsPerPage must have a number greater than 0.

When the page loads, the page should similar to this, base on your data and your SQL Statement


  

 


You can now add more dataviewer webparts on the page and connect it to different SQL Servers or run different SQL Statements.  Example below

 

You can now build a report dashboard based on this dataviewer webpart and re-using it multiple times on the same page by simply configuring the connection and query properties.


Posted 12-08-2007 11:33 AM by sam

Comments

Links (12/9/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (12/9/2007) « Steve Pietrek’s SharePoint Stuff
on 12-09-2007 4:50 PM

Pingback from  Links (12/9/2007) « Steve Pietrek’s SharePoint Stuff

Iain wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 01-23-2008 11:06 AM

Hi,

I have tried following you blog and everything is working except the paging. Please could you post the full code for DataViewer.cs so I can see where my mistake is.

Thanks

Iain wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 01-23-2008 11:19 AM

Hi,

I moved

gv.AllowPaging = true;

gv.PageSize = datarows;

gv.PageIndexChanging += new GridViewPageEventHandler this.gv_PageIndexChanging);

to appear directly below:

Controls.Add(gv);

and it now works.

Thanks

John wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-15-2008 11:42 PM

Why is "AllowPaging" and "PageSize" required to come after Controls.Add(gv) before this will work properly?

Lloyd wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-24-2008 3:55 AM

I have tried to set this up - and follwed all items, I cannot see the data when the sharepoint webpart loads.....

Can anyone help me - I really need this to work it will make my life easier.

After completeing part 3 I guess that I dont need the DBAccess.cs anymore as it is all in DataViewer. Is this correct or am i missing something

Thanks in advance !

Lloyd wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-30-2008 9:25 AM

Ignore above - all is fine and working

Craig Matthews wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 05-28-2008 1:28 PM

Excellent three articles.  I needed something exactly like this and it was also a good practice (for me) to read and understand the code as I'm transitioning to C# from VB.NET.  Thanks for the code explanations.

Leon wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 06-05-2008 2:06 PM

This is how my code looks but my paging still does not work:

gv.DataSource = dt;

gv.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;

gv.GridLines = GridLines.Horizontal;

gv.CellPadding = 1;

gv.DataBind();

gv.HeaderStyle.BackColor = System.Drawing.Color.LightGray;

Controls.Add(gv);

gv.AllowPaging = true;

gv.PageSize = datarows;

gv.PageIndexChanging += new GridViewPageEventHandler(this.gv_PageIndexChanging);

sams wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 06-10-2008 4:41 PM

I cannot get the pagination to work. Sam, could you put up your code here? Thanks so much.

Atul wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 06-23-2008 10:10 AM

Hello

I have two questions

1. How can I set pre-defined filter property?

2. How can i restrict to user to use only select sql query? I dont want to allow them delete sql option?

Atul

JohnnyRae wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 07-11-2008 5:23 PM

I am a "newbie", having very little experience in .net and no experience in moss/wss in college, the task of just figuring out what i need to work on learning is intimidating at times, I can not tell you how much this tutorial helped, Although brief and to the point it really explained how some of the code fit together...  this was a great start for my understanding of webparts  

JK wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 07-21-2008 9:05 AM

Hey Leon, code should be like this :

           Controls.Add(grid);

           grid.AllowPaging = true;

           grid.PageSize = 2;

           grid.PageIndexChanging += new GridViewPageEventHandler(this.grid_PageIndexChanging);

           grid.DataSource = table;

           grid.DataBind();

George E, Haney III, PMP, MCP wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 07-28-2008 8:40 AM

WOW! Excellent stuff that I spent hours looking for.  I appriciate it.  

With the three posts and then so many comments, I am really a bit lost on what the resultant code should look like...

Can I beg someone to post the source? It will save me many hours of peicing it together among many many others who need this important product.

George E, Haney III, PMP, MCP wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 07-28-2008 8:41 AM

Is this on CodePlex by any chance?

George E, Haney III, PMP, MCP wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 07-28-2008 8:41 AM

Is this on CodePlex by any chance?

Pakistani wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 08-06-2008 3:30 PM

Hi.

Thanks alot. Can't appreciate it enough.

Can you give the whole DataViewer.cs file for download, because I made it through part I & II, but when in last part I did not get to see any data in my web - part when I typed in my user id, password, servername, database name and query.

?

Best Regards

Stef wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 08-14-2008 10:30 AM

Sam you are so good!! Thanks.

Dave wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 08-19-2008 3:07 PM

Great post!  Helpful on many levels!  

Shepherd wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 08-26-2008 2:53 PM

Great Posting. Thanks for sharing ur knowledge.

noLogo wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 09-01-2008 7:09 AM

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Data;

using System.Data.SqlClient;

namespace DataViewer

{

   public class DataViewer : WebPart

   {

       GridView gv = new GridView();

       private string servername;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("ServerName"), Category("Data Properties")]

       public string ServerName

       {

           get { return servername; }

           set { servername = value; }

       }

       private string dbname;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("DatabaseName"), Category("Data Properties")]

       public string DatabaseName

       {

           get { return dbname; }

           set { dbname = value; }

       }

       private string username;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("UserName"), Category("Data Properties")]

       public string UserName

       {

           get { return username; }

           set { username = value; }

       }

       private string password;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("Password"), Category("Data Properties")]

       public string Password

       {

           get { return password; }

           set { password = value; }

       }

       private string innerpassword;

       [Personalizable(), WebBrowsable(false),

       WebDisplayName("InnerPassword"), Category("Data Properties")]

       public string InnerPassword

       {

           get { return innerpassword; }

           set { innerpassword = value; }

       }

       private string sqlquery;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("SQLQuery"), Category("Data Properties")]

       public string SQLQuery

       {

           get { return sqlquery; }

           set { sqlquery = value; }

       }

       private int datarows;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("RowsPerPage"), Category("Data Properties")]

       public int DataRows

       {

           get { return datarows; }

           set { datarows = value; }

       }

       protected override void CreateChildControls()

       {

           base.CreateChildControls();

           try

           {

               DBAccess objDB = new DBAccess();

               DataTable dt = new DataTable();

               if (password != "******")

               {

                   InnerPassword = password;

                   password = "******";

               }

               string strConnection = "UID=" + username + ";PWD=" + innerpassword + ";Initial Catalog=" + dbname + ";Data Source=" + servername + ";";

               string sql = sqlquery;

               objDB.StrConnection = strConnection;

               dt = objDB.GetSQlResult(sql);

               Controls.Add(gv);

               gv.AllowPaging = true;

               gv.PageSize = datarows;

               gv.PageIndexChanging += new GridViewPageEventHandler(this.gv_PageIndexChanging);

               gv.DataSource = dt;

               gv.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;

               gv.GridLines = GridLines.Horizontal;

               gv.CellPadding = 1;

               gv.DataBind();

               gv.HeaderStyle.BackColor = System.Drawing.Color.LightGray;

               string rowcount;

               rowcount = dt.Rows.Count.ToString();

               Label sqllbl1 = new Label();

               sqllbl1.Text = rowcount;

               Controls.Add(sqllbl1);

           }

           catch (Exception ex)

           {

               Label lbl = new Label();

               lbl.Text = "Error Occurred: ";

               lbl.Text += ex.Message;

               Controls.Add(lbl);

           }

       }

       protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)

       {

           gv.PageIndex = e.NewPageIndex;

           string strConnection = "UID=" + username + ";PWD=" + innerpassword + ";Initial Catalog=" + dbname + ";Data Source=" + servername + ";";

           DBAccess objdb = new DBAccess();

           objdb.StrConnection = strConnection;

           DataTable dt = objdb.GetSQlResult(sqlquery);

           gv.DataSource = dt;

           gv.DataBind();

       }

   }

}

noLogo wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 09-02-2008 5:08 AM

encryption methods?anyone looked into encrypting the username & password?if so please let me know how this is possible...

i'm looking at FormsAuthenticationTicket?

Jeff Poste wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 09-03-2008 6:45 PM

Thanks for the listing noLogo.  I also found that you also had to add

gv.PageSize = datarows;

to the gv_PageIndexChanging method, or the number of rows would not stay set.

As to security, Instead of using forms authentication where you have to pass security info, i found that windows authentication worked well.  To go against the Northwind database, for example, I used the connection string:

string strConnection = "Data Source=" + servername + ";Initial Catalog=" + dbname + ";Integrated Security=True";

and it worked fine for my user ID.  I realize that you can't always use individual user IDs, but just mention this in case it is of any interest to anybody.

Knut wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 10-09-2008 10:35 AM

Atul,

regarding your question how to restrict the T-SQL to a select:

You can change the line

SqlDataAdapter da = new SqlDataAdapter(sql, cnn);

into:

SqlDataAdapter da = new SqlDataAdapter("select " + sql, cnn);

(it´s in DBAccess.cs, not in DataViewer.cs)!

Regards,

Knut

Knut Friebe wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 10-10-2008 7:57 AM

For anybody who is interested in this, I implemented two additional functions:

- possibility to sort

- SQL and Windows Authentication may be defined as Property in the Webpart configuration

Additionally, I changed the gv_PageIndexChanging event handler. Now, this handler doesn´t open the connection to the SQL server. It uses the data set which was read by CreateChildControls(). So, only one time the data set is read, and if the user is sorting or paging, the data set is not re-read from the server.

I would like to get a feedback from an ASP.NET expert about that - does it cause performance issues?

Regards,

Knut

Here is the code (DataViewer.cs):

//------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Web.UI.WebControls.WebParts;

namespace DataViewer

{

   public class DataViewer : System.Web.UI.WebControls.WebParts.WebPart

   {

       GridView gv = new GridView();

       public enum myAuthentification

       {

           SQL,

           Windows

       };

       private string servername;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("ServerName"), Category("Data Properties")]

       public string ServerName

       {

           get { return servername; }

           set { servername = value; }

       }

       private string dbname;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("DatabaseName"), Category("Data Properties")]

       public string DatabaseName

       {

           get { return dbname; }

           set { dbname = value; }

       }

       // msdn.microsoft.com/.../ms948927.aspx

       private myAuthentification authentification;

       [DefaultValue(myAuthentification.SQL),

       Personalizable(), WebBrowsable(true),

       WebDisplayName("Authentification"), Category("Data Properties")]

       public myAuthentification serverauth

       {

           get { return authentification; }

           set { authentification = value; }

       }

       private string username;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("UID"), Category("Data Properties")]

       public string UserName

       {

           get { return username; }

           set { username = value; }

       }

       private string password;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("PWD"), Category("Data Properties")]

       public string Password

       {

           get { return password; }

           set { password = value; }

       }

       private string innerpassword;

       [Personalizable(), WebBrowsable(false),

       WebDisplayName("InnerPassword"), Category("Data Properties")]

       public string InnerPassword

       {

           get { return innerpassword; }

           set { innerpassword = value; }

       }

       private string sqlquery;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("SQLQuery Select"), Category("Data Properties")]

       public string SQLQuery

       {

           get { return sqlquery; }

           set { sqlquery = value; }

       }

       private int datarows;

       [Personalizable(), WebBrowsable(true),

       WebDisplayName("RowsPerPage"), Category("Data Properties")]

       public int DataRows

       {

           get { return datarows; }

           set { datarows = value; }

       }

       protected void gv_gridView_Sorting(object sender, GridViewSortEventArgs e)

       {

           // see: ryanolshan.com/.../c-gridview-sorting-paging-w-o-a-datasourcecontrol-datasource

           GridViewSortExpression = e.SortExpression;

           int pageIndex = gv.PageIndex;

           DataTable m_DataTable = gv.DataSource as DataTable;

           if (m_DataTable != null)

           {

               DataView m_DataView = new DataView(m_DataTable);

               gv.DataSource = SortDataTable(m_DataTable, false);

               gv.DataBind();

               gv.PageIndex = pageIndex;

               gv.PageSize = datarows;

           }

       }

   private string GridViewSortDirection

   {

       get { return ViewState["SortDirection"] as string ?? "ASC"; }

       set { ViewState["SortDirection"] = value; }

   }

   private string GridViewSortExpression

   {

       get { return ViewState["SortExpression"] as string ?? string.Empty; }

       set { ViewState["SortExpression"] = value; }

   }

   private string GetSortDirection()

   {

       switch (GridViewSortDirection)

       {

           case "ASC":

               GridViewSortDirection = "DESC";

               break;

           case "DESC":

               GridViewSortDirection = "ASC";

               break;

       }

       return GridViewSortDirection;

   }

   protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)

       {

           if (dataTable != null)

           {

               DataView dataView = new DataView(dataTable);

               if (GridViewSortExpression != string.Empty)

               {

                   if (isPageIndexChanging)

                   {

                       dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);

                   }

                   else

                   {

                       dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());

                   }

               }

               return dataView;

           }

           else

           {

               return new DataView();

           }

       }

       protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)

       {

           gv.AllowSorting = true;

           gv.AutoGenerateColumns = true;

           gv.PageIndex = e.NewPageIndex;

           gv.PageSize = datarows;

           DataTable m_DataTable = gv.DataSource as DataTable;

           if (m_DataTable != null)

           {

               DataView m_DataView = new DataView(m_DataTable);

               gv.DataBind();

               gv.PageIndex = e.NewPageIndex;

               gv.PageSize = datarows;

           }

       }

       protected override void CreateChildControls()

       {

           base.CreateChildControls();

           {

               base.CreateChildControls();

               try

               {

                   if (password != "******")

                   {

                       InnerPassword = password;

                       password = "******";

                   }

                   string strConnection;

                   if (authentification.Equals(myAuthentification.SQL))

                   {

                       strConnection = "UID=" + username + ";PWD=" + innerpassword + ";Initial Catalog=" + dbname + ";Data Source=" + servername + ";";

                   }

                   else

                   {

                       strConnection = "Initial Catalog=" + dbname + ";Data Source=" + servername + ";Integrated Security=SSPI;";

                   }

                   DbAccess objdb = new DbAccess();

                   objdb.StrConnection = strConnection;

                   DataTable dt = objdb.GetSQLResult(sqlquery);

                   Controls.Add(gv);

                   gv.AutoGenerateColumns = true;

                   gv.AllowSorting = true;

                   gv.AllowPaging = true;

                   gv.PageSize = datarows;

                   gv.PageIndexChanging += new GridViewPageEventHandler(this.gv_PageIndexChanging);

                   gv.Sorting += new GridViewSortEventHandler(this.gv_gridView_Sorting);

                   gv.DataSource = dt;

                   gv.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;

                   gv.GridLines = GridLines.Horizontal;

                   gv.CellPadding = 1;

                   gv.DataBind();

                   gv.HeaderStyle.BackColor = System.Drawing.Color.LightGray;

                   string rowcount;

                   rowcount = dt.Rows.Count.ToString();

                   Label sqllbl1 = new Label();

                   sqllbl1.Text = rowcount + " record(s)";

                   Controls.Add(sqllbl1);

               }

               catch (Exception ex)

               {

                   Label lbl = new Label();

                   lbl.Text = "Error Occurred: ";

                   lbl.Text += ex.Message;

                   Controls.Add(lbl);

               }

           }

       }

   }

}

Dave wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 10-29-2008 2:19 PM

Knut,

I tried your code - but now I get:

Error Occurred: Specified argument was out of the range of valid values. Parameter name: value

Ideas?

Thanks,

Dave

Knut Friebe wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 11-13-2008 8:33 AM

Dave,

do you have a any additional information (Line number)?

Regards,

Knut

Uber Macedonian wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 11-14-2008 3:23 AM

Dont forget to add the following line

[assembly: AllowPartiallyTrustedCallers] in AssemblyInfo.cs

and using System.Security; also in AssemblyInfo.cs

Just a reminder

Edit the web.config and in the Level atribute of the trust section change the WSS_Minimal to WSS_Medium

Malathy wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 11-21-2008 9:29 AM

Great post !... It helped me a lot in understanding about webparts..Thanks to you :-)

webpart encryption? | keyongtech wrote webpart encryption? | keyongtech
on 01-18-2009 11:29 AM

Pingback from  webpart encryption? | keyongtech

Hason wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-06-2009 4:06 PM

Thanks for a great post. I'm trying to pass in parameters to my query using a connection from aonther web part. Specifically a date filter web part. Anyone have any tips?

Robert wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-21-2009 4:16 PM

Any chance someone can email me the files?  I have VS2005 but it doesn't have the C# features.

Robert wrote re: Developing a reusable SQL Data viewer WebPart for Sharepoint -Part 3
on 04-21-2009 4:23 PM

oops - email is ragtemp@yahoo.com

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.