I'm working on my very first web part for SharePoint 2010 in Visual Studio 2010. Please keep that in mind as you continue reading.

I'm trying to build a web part that runs a simple query against a SQL database (not my SharePoint db) and displays the results when the user hits a refresh button. The results will always be one column of data, and between 0 and 6 rows.

I've tried using a DataList, a GridView, and a ListView and setting their DataSource to the DataTable I fill with the query results, but I can never get the object to populate any data when I test it.

Here's my .ascx.cs file:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ActiveMoxyUsers.AMUWebPart
    public partial class AMUWebPartUserControl : UserControl
        public DataTable UserList = null;
        public SqlConnection MoxyConn = new SqlConnection(@"server=***;database=***;user id=***;password=***");

        protected void Page_Load(object sender, EventArgs e)
            UserList = new DataTable();

        protected void Button1_Click(object sender, EventArgs e)

        protected void UpdateList()
            if (UserList != null)
            using (SqlDataAdapter adapter = new SqlDataAdapter(@"SELECT DISTINCT CASE WHEN login_name LIKE '%\%' THEN UPPER(REPLACE(login_name, 'WSDOMAIN\', '')) ELSE UPPER(login_name) END AS UserID FROM sys.dm_exec_sessions WHERE program_name='Moxy';", MoxyConn))
                DataList1.DataSource = UserList;
                Label1.Text = UserList.Rows[0][0].ToString();

And here's my .ascx file:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMUWebPartUserControl.ascx.cs" Inherits="ActiveMoxyUsers.AMUWebPart.AMUWebPartUserControl" %>
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" 
    RepeatColumns="1" ShowFooter="False" ShowHeader="False">
    <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Refresh" 
    onclick="Button1_Click" />

Label1 exists specifically to check whether or not my query is returning any data at all, and it is.

What am I missing? Why won't my DataList display my results?


Right after your DataList1.DataSource = UserList; you need to bind it to the DataList using DataList1.DataBind();

DataList1.DataSource = UserList;
Label1.Text = UserList.Rows[0][0].ToString();
  • Excellent first step, but now I get a small grey box (about a quarter the size of the grid boxes in the background of this page) above Label1 in the web part with no data in it, so I'm still missing something. – newuser Jun 3 '11 at 15:35
  • 2
    You don't have an <ItemTemplate>. You have no way defining how to display your results. I believe the GridView control has a property called 'AutoGenerateColumns' that you can set to true, and will automatically create your columns and rows. It appears the DataList doesn't, so you need to define how the data should show. – Tim Gabrhel Jun 3 '11 at 15:41
  • 2
    MSDN DataList – Tim Gabrhel Jun 3 '11 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.