Databinding the GridView in C#

Last post 12-22-2008 9:08 AM by Polemus. 6 replies.

Sort Posts:

  • Databinding the GridView in C#

    10-31-2008, 9:31 AM
    • Member
      4 point Member
    • zafarjaffary
    • Member since 10-19-2008, 4:21 PM
    • Posts 50

    Hi all

    I have the following code and I am pulling the data from SQL server and then saving it to c:\temp it is working fine. But Now i want to bind my GridView which created on my ASPX page. instead creating and saving the file to hard drive. I want to fill my GridView.

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.Xml;
    using System.IO;

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    GetContacts("Toronto North");

    GridView1.DataSource = contactListDataSet.Tables[0];
    GridView1.DataBind();

    }

    public static void GetContacts(string TeamName)

    {

    //Database Connection string

    String sConnection = "server=HTCWEBDEMO;";SqlConnection htc_SqlConnection = new SqlConnection(sConnection);

    htc_SqlConnection.Open();

    // Get the Data through the provider.

    string getContactList_SQLStatement = "SELECT Title,FirstName,LastName,PhoneNumber,Faxnumber, Email FROM ContactList WHERE TeamName='" + TeamName +"'; SELECT FirstName, LastName FROM ContactList WHERE TeamMorty ='True' and TeamName='" + TeamName + "';";

    SqlDataAdapter htc_SqlDataAdapter = new SqlDataAdapter(getContactList_SQLStatement, sConnection);

    DataSet ContactListDataSet = new DataSet();ContactListDataSet.DataSetName = "ContactsList";

    htc_SqlDataAdapter.Fill(ContactListDataSet);

    ContactListDataSet.Tables[0].TableName = "Contact";

    ContactListDataSet.Tables[1].TableName = "Morty";

    // Write data to files: ContactList.xml.

    //ContactListDataSet.WriteXml("c:/temp/ContactList.xml");

    GridView1.DataSource = contactListDataSet.Tables[0];
    GridView1.DataBind();

    htc_SqlConnection.Close();

    }

    }

  • Re: Databinding the GridView in C#

    10-31-2008, 11:38 AM
    • Member
      621 point Member
    • pushya
    • Member since 06-20-2008, 7:12 PM
    • Posts 137
    Hi, you first need to remove the dataset that you are binding the gridview in the page_load section. the data set is a local variable of the the function "GetContacts(string TeamName)" and hence you cant call it in page load. though you are assigning the dataset to the gridview in your function because immediately you are again assigning it when it has no value, so it displays nothing. so what you can do is modify your program like below: protected void Page_Load(object sender, EventArgs e) { // the return here would be the dataset form this function GridView1.DataSource = GetContacts("Toronto North");; GridView1.DataBind(); } public dataset GetContacts(string TeamName) { //Database Connection string String sConnection = "server=HTCWEBDEMO;database=hometrust;user id=ht_admin;password=htc_tr!st";SqlConnection htc_SqlConnection = new SqlConnection(sConnection); htc_SqlConnection.Open(); // Get the Data through the provider. string getContactList_SQLStatement = "SELECT Title,FirstName,LastName,PhoneNumber,Faxnumber, Email FROM ContactList WHERE TeamName='" + TeamName +"'; SELECT FirstName, LastName FROM ContactList WHERE TeamMorty ='True' and TeamName='" + TeamName + "';"; SqlDataAdapter htc_SqlDataAdapter = new SqlDataAdapter(getContactList_SQLStatement, sConnection); DataSet ContactListDataSet = new DataSet();ContactListDataSet.DataSetName = "ContactsList"; htc_SqlDataAdapter.Fill(ContactListDataSet); ContactListDataSet.Tables[0].TableName = "Contact"; ContactListDataSet.Tables[1].TableName = "Morty"; // Write data to files: ContactList.xml. //return the dataset or the table if you need to return contactListDataSet; }
  • Re: Databinding the GridView in C#

    10-31-2008, 12:15 PM
    • Member
      4 point Member
    • zafarjaffary
    • Member since 10-19-2008, 4:21 PM
    • Posts 50

    your reply is unreadable for me. you put all in one line can you please format sothat at least i can read it?

    Crying 

    Hi, you first need to remove the dataset that you are binding the gridview in the page_load section. the data set is a local variable of the the function "GetContacts(string TeamName)" and hence you cant call it in page load. though you are assigning the dataset to the gridview in your function because immediately you are again assigning it when it has no value, so it displays nothing. so what you can do is modify your program like below: protected void Page_Load(object sender, EventArgs e) { // the return here would be the dataset form this function GridView1.DataSource = GetContacts("Toronto North");; GridView1.DataBind(); } public dataset GetContacts(string TeamName) { //Database Connection string String sConnection = "server=HTCWEBDEMO;database=hometrust;user id=ht_admin;password=htc_tr!st";SqlConnection htc_SqlConnection = new SqlConnection(sConnection); htc_SqlConnection.Open(); // Get the Data through the provider. string getContactList_SQLStatement = "SELECT Title,FirstName,LastName,PhoneNumber,Faxnumber, Email FROM ContactList WHERE TeamName='" + TeamName +"'; SELECT FirstName, LastName FROM ContactList WHERE TeamMorty ='True' and TeamName='" + TeamName + "';"; SqlDataAdapter htc_SqlDataAdapter = new SqlDataAdapter(getContactList_SQLStatement, sConnection); DataSet ContactListDataSet = new DataSet();ContactListDataSet.DataSetName = "ContactsList"; htc_SqlDataAdapter.Fill(ContactListDataSet); ContactListDataSet.Tables[0].TableName = "Contact"; ContactListDataSet.Tables[1].TableName = "Morty"; // Write data to files: ContactList.xml. //return the dataset or the table if you need to return contactListDataSet; }

  • Re: Databinding the GridView in C#

    10-31-2008, 1:11 PM
    • Member
      621 point Member
    • pushya
    • Member since 06-20-2008, 7:12 PM
    • Posts 137
    Hi, hmm..i did post it after formatting it. yet it displays in a single line only. am not sure why. probably has something do with my browser. i am using chrome. i will try posting form a different browser and see. pushya.
  • Re: Databinding the GridView in C#

    10-31-2008, 1:28 PM
    Answer
    • Member
      621 point Member
    • pushya
    • Member since 06-20-2008, 7:12 PM
    • Posts 137

    Hi,

    What i mentinoed in my previous post is that. You are declaring the dataset in the function " GetContacts", so the dataset is local to it. In the page_load event, when you bid the gridview with the dataset, the dataet doesnt exist. you are also binding it in the function "GetContacts". Either leave that as it is. Or get the function to return the dataset and assign that to the gridview. I have made the change in the code to reflect this. check it out.

    you can change your code as follows:

     

    protected void Page_Load(object sender, EventArgs e)

    {

    GridView1.DataSource = GetContacts("Toronto North");
    GridView1.DataBind();

    }

    public dataset GetContacts(string TeamName)

    {

    //Database Connection stringString sConnection = "server=HTCWEBDEMO;database=hometrust;user id=ht_admin;password=htc_tr!st";SqlConnection htc_SqlConnection = new SqlConnection(sConnection);

    htc_SqlConnection.Open();

    // Get the Data through the provider.

    string getContactList_SQLStatement = "SELECT Title,FirstName,LastName,PhoneNumber,Faxnumber, Email FROM ContactList WHERE TeamName='" + TeamName +"'; SELECT FirstName, LastName FROM ContactList WHERE TeamMorty ='True' and TeamName='" + TeamName + "';";

    SqlDataAdapter htc_SqlDataAdapter = new SqlDataAdapter(getContactList_SQLStatement, sConnection);

    DataSet ContactListDataSet = new DataSet();ContactListDataSet.DataSetName = "ContactsList";

    htc_SqlDataAdapter.Fill(ContactListDataSet);

    ContactListDataSet.Tables[0].TableName =
    "Contact";

    ContactListDataSet.Tables[1].TableName = "Morty";

    // Write data to files: ContactList.xml.

    //ContactListDataSet.WriteXml("c:/temp/ContactList.xml");

    //return your dataset here or whatever you want to bind your grid with. change the return value of your function accordingly.

    return ContactListDataSet;

    htc_SqlConnection.Close();

    }

     pushya.

     

  • Re: Databinding the GridView in C#

    10-31-2008, 3:29 PM
    • Member
      4 point Member
    • zafarjaffary
    • Member since 10-19-2008, 4:21 PM
    • Posts 50

    Thanks it is working now.

     

    But the idea puting on the gridview you have changed this morning. Now they want in the xml format. which did earlier.

  • Re: Databinding the GridView in C#

    12-22-2008, 9:08 AM
    • Member
      41 point Member
    • Polemus
    • Member since 10-06-2005, 3:49 AM
    • South Africa
    • Posts 24
    The only way to predict the future is to create it
Page 1 of 1 (7 items)