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

  • SEARCH
  • Home
  • 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 8641425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:31:17+00:00 2026-06-12T11:31:17+00:00

Im working on a method to make all my SQL Querys and then databind

  • 0

Im working on a method to make all my SQL Querys and then databind them to Repeaters, DataLists And so on…

protected void sqlQuery(Control controlName, String query) {
    SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();

    conn.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
    cmd.Connection = conn;

    try {
        cmd.CommandText = query;

        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        adapter.Fill(dt);
        controlName.DataSource = dt;
        controlName.DataBind();

    } catch (Exception error) {
        Label1.Text = "Error!<br/>" + error;
    }
}

Then i would call the method with my control name that i want to databind to.
Like:

sqlQuery(Repeater1, "SELECT * FROM someTable");
sqlQuery(DataList1, "SELECT * FROM someTable");

But that dosent work now since it dont know the control type when i just use Control..

So how would i do this?

  • 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-06-12T11:31:18+00:00Added an answer on June 12, 2026 at 11:31 am

    The problem occurs because Control does not have the properties/methods you require, and these properties are not defined on an interface or base control type.

    Therefore you have to use something called reflection, this allows you to the call properties and methods (as well as lots of other things) that you require without having any compile time knowledge of these, of course if you pass in a control without these you will get a nasty exception and there is a performance hit when using reflection – but in your case it would be trivial.

    I dont have an IDE handy but something along these lines should do it.

    public static void DatabindControlFromSqlQuery(Control control,string query)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString(); 
        using (var connection = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand(connection))
            {
                cmd.CommandText = query;
                var dt = new DataTable();
                var adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
    
                var type = control.GetType();
                var dataSourceProp = type.GetProperty("DataSource");
    
                dataSourceProp.SetValue(control,dt);
    
                var dataBindMethod = type.GetMethod("DataBind");
                dataBindMethod.Invoke(control,null);
    
            }
        }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a UITableViewController. The method that fails is: - (void) testLoc
This method is working totally right in matlab. but, when I compiled it in
I am working in getView() method in the code below. I am trying to
I am working on a method that will allow me to pull in the
I'm working on a Method which does some task asynchronously. Let's call it: AsyncMethod();
For me Javascript focus() method is working fine if I use it with a
Both methods MyBehavior::CreateSerializer() are not called of some reason, but ReplaceBehavior() method is working.
My delete method is not working, here is what is happening at the moment.
I was working on a demo method and found something strange(at least to me
I'm working on a Refresh() extension method for ObservableCollection which adds, removes or replaces

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.