Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:14:27+00:00 2026-05-29T22:14:27+00:00

I am making a simple web part which queries an SQL database to pull

  • 0

I am making a simple web part which queries an SQL database to pull a list of people into a drop down list. When a user of that drop down list is selected, it populates a datagrid with additional information about that user via another sql query.

I have the SQL Server and SQL Database as custom properties.

My problem is that when the custom properties are changed and ‘Ok’ is hit, the databound control does not populate. Instead, I have to then refresh the page again for my data to bind.

I know this is an issue with web part life cycle so was hoping you guys could assist me.

I did try putting my databinding code into the OnPreRender as suggested here, but I then found that when the user was changed in the drop down box, it would re-bind all the data and automatically set the user back at the first in the list.

My entire code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;


namespace SampleProject.SampleWebPart
{
    [ToolboxItemAttribute(false)]
    public class SampleWebPart : WebPart
    {
        public DropDownList dl = null;
        public DataGrid dg = null;
        private string _SqlServer;
        private string _SqlDb;

        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(true),
         WebDisplayName("SQL Server"),
         WebDescription("Specify the SQL Server to connect to. eg. VM188"),
         SPWebCategoryName("Data Connection Properties"),
         DefaultValue(".")]
        public string SqlServer
        {
            get { return _SqlServer; }
            set { _SqlServer = value; }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("Database Name"),
        WebDescription("Specify the Database to connect to. eg. Test"),
        SPWebCategoryName("Data Connection Properties"),
        DefaultValue("Test")]
        public string SqlDb
        {
            get { return _SqlDb; }
            set { _SqlDb = value; }
        }

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(this.SqlServer) || string.IsNullOrEmpty(this.SqlDb))
            {
                //If just added, ask to configure in toolpane first.
                LiteralControl literalCtrl = new LiteralControl(
                string.Format("<a id='MsoFrameworkToolpartDefmsg_{0}' href=\"javascript:ShowToolPane2Wrapper('Edit','129','{0}');\">Open the tool pane and enter a valid value.", this.ID));
                this.Controls.Add(literalCtrl);
            }
            else
            {
                try
                {


                    System.Web.UI.UpdatePanel detailsUpdatePanel = new System.Web.UI.UpdatePanel();
                    detailsUpdatePanel.ID = "DetailsUpdatePanel";
                    detailsUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
                    this.Controls.Add(detailsUpdatePanel);

                    dl = new DropDownList();
                    dg = new DataGrid();

                    dl.AutoPostBack = true;
                    dl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);

                    dl.ID = "selectPersonDl";
                    dg.ID = "personDetailsDg";
                    detailsUpdatePanel.ContentTemplateContainer.Controls.Add(dl);
                    detailsUpdatePanel.ContentTemplateContainer.Controls.Add(dg);

                    if (!Page.IsPostBack)
                    {
                        //Populate the drop down list
                        dl.DataSource = GetNames();
                        dl.DataValueField = "Person";
                        dl.DataTextField = "Person";
                        dl.DataBind();

                        //Populate the data grid
                        dg.DataSource = GetData(dl.SelectedValue);
                        dg.DataBind();
                    }

                }
                catch (Exception ex)
                {
                    Label lblError = new Label();
                    lblError.Text = ex.Message;
                    this.Controls.Add(lblError);
                }

                Label lblServ = new Label();
                lblServ.Text = this.SqlServer;
                this.Controls.Add(lblServ);
                Label lblDb = new Label();
                lblDb.Text = this.SqlDb;
                this.Controls.Add(lblDb);



                base.CreateChildControls();
            }
        }


        protected void dl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            dg.DataSource = GetData(dl.SelectedValue);
            dg.DataBind();
        }

        protected SqlDataReader GetNames()
        {
            SqlConnection cn = new SqlConnection("server=" + this.SqlServer + "; database=" + this.SqlDb + ";integrated security=true");
            SqlCommand cmd = new SqlCommand("Select Person from Sample", cn);
            cn.Open();
            return cmd.ExecuteReader();
        }

        protected SqlDataReader GetData(string person)
        {
            SqlConnection cn = new SqlConnection("server=" + this.SqlServer + "; database=" + this.SqlDb + ";integrated security=true");
            SqlCommand cmd = new SqlCommand("Select DateMod, Sample FROM Sample WHERE Person='" + person + "'", cn);
            cn.Open();
            return cmd.ExecuteReader();

        }

    }
}

NOTE: I did some more thinking – could be it because I have the binding wrapped inside a !IsPostBack if statement? I’m not sure…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:14:29+00:00Added an answer on May 29, 2026 at 10:14 pm

    You have to trigger the databinding either on Page_Load or Page_PreRender.
    You’re right when you say the users selection gets dropped if you just databind it in either these 2 methods.
    The trick to avoid this is to only databind if the page loaded without a postback.
    Your page_prerender or page_load would have to look like this :

    if(!IsPostBack)
    {
      dg.DataSource = GetData();
      dg.DataBind();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a simple web-app which requires login for the admin page. I
We're making some calls to some very simple JSON web services in .NET CF
I'm making a simple 2 player game in XNA and started looking into saving
I'm making a simple web app with PhotoSwipe to display lots of images. How
I'm thinking about making a simple web application to practice custom tags, EL, ...
Ok so I'm making a simple web app using the technologies from the topic,
When making a simple web request is there a way to tell the PowerShell
I'm making a simple web server using PHP and sockets. Everything is working fine
I have a simple web application in which I have created a wizard, each
I'm making a web application with Grails. I've got a list with data that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.