Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2003, 03:54 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataSet and DataAdapter Objects

I'm using the following code to populate two DataGrid controls. As you can see, I create two DataSet objects and two DataAdapter objects. The code works fine, but I'm wondering if I could I have accomplished the same result with only one DataSet object and/or one DataAdapter object? If so, how?

Thanks in advance for your help!

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="c#" runat="server">
void Page_Load()
{
    // declare assign string variables
    string strConnect = @"provider=Microsoft.Jet.OleDb.4.0;data source=c:\begaspnet\ch12\northwind.mdb";
    string strSql = "SELECT CompanyName, ContactName FROM Suppliers";
    string strSqlEmp = "SELECT LastName, FirstName FROM Employees";

    // create connection objects and DataSet objects
    OleDbConnection objConnect = new OleDbConnection(strConnect);
    DataSet objDataSet = new DataSet();
    DataSet objDataSet2 = new DataSet();

    // create the DataAdapter objects and us it to fill the DataSets object
    OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSql, objConnect);
    OleDbDataAdapter objDataAdapter2 = new OleDbDataAdapter(strSqlEmp, objConnect);
    objDataAdapter.Fill(objDataSet, "SupplierContacts");
    objDataAdapter2.Fill(objDataSet2, "Employees");

    // assign dataset to the datagrid controls
    dgSupplierContacts.DataSource = objDataSet;
    dgEmployees.DataSource = objDataSet2;

    //bind data to the datagrid controls
    dgSupplierContacts.DataBind();
    dgEmployees.DataBind();

}
</script>

<html>
<body>
<asp:datagrid id="dgSupplierContacts" runat="server" cellpadding="5" font-name="verdana" font-size="10pt"
    headerstyle-backcolor="#dcdcdc" headerstyle-forecolor="blue" />
<br><br>
<asp:datagrid id="dgEmployees" runat="server" cellpadding="5" font-name="verdana" font-size="10pt"
    headerstyle-backcolor="#dcdcdc" headerstyle-forecolor="blue" />
</body>
</html>
 
Old November 14th, 2003, 04:26 PM
Authorized User
 
Join Date: Jul 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont have a great deal of expience with C# so you might need to change some of the syntax to the changes I made to ensure it works, but this shohuld do the trick.

A dataset can contain any number of datatables. About the only thinkg to note is that you need to specify the name of the table you wish to access when pulling the data out of the dataset.




{
    // declare assign string variables
    string strConnect = @"provider=Microsoft.Jet.OleDb.4.0;data source=c:\begaspnet\ch12\northwind.mdb";
    string strSql = "SELECT CompanyName, ContactName FROM Suppliers";
    string strSqlEmp = "SELECT LastName, FirstName FROM Employees";

    // create connection objects and DataSet objects
    OleDbConnection objConnect = new OleDbConnection(strConnect);
    DataSet objDataSet = new DataSet();
   //Delete this Line - DataSet objDataSet2 = new DataSet();

    // create the DataAdapter objects and us it to fill the DataSets object
    //Rearrange and modify the below lines
    OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSql, objConnect);
    objDataAdapter.Fill(objDataSet, "SupplierContacts");
    OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSqlEmp, objConnect);
    objDataAdapter.Fill(objDataSet, "Employees");


    // assign dataset to the datagrid controls
    //Modify the below lines
    dgSupplierContacts.DataSource = objDataSet.tables("SupplierContacts").DefaultView;
    dgEmployees.DataSource = objDataSet.tables("Employees").DefaultView;


    //bind data to the datagrid controls
    dgSupplierContacts.DataBind();
    dgEmployees.DataBind();
}


 
Old November 16th, 2003, 01:44 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Craig. I see that I can accomplish this task with only one DataSet object. Very helpful. Your code works beautifully.
 
Old January 10th, 2006, 10:29 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hiya.
i'v have kind of same problem, but instead of two datagrid i only have one but i want to populate it with data from two tables, is that possible?


        DBAnslutning();
        strSQL = "SELECT artNr, produktNamn, farg, langd, antal, pris FROM Kundvagn WHERE Kundvagn.kundNr ='" + Session["Knr"].ToString() + "';";
        strSQL2 += "SELECT Summa, Frakt FROM FraktKostnad WHERE FraktKostnad.kundNr ='" + Session["Knr"].ToString() + "';";
        objDataAdapter = new OleDbDataAdapter(strSQL, objConnection);
        objDataAdapter = new OleDbDataAdapter(strSQL2, objConnection);
        objDataSet = new DataSet();

        objDataAdapter.Fill(objDataSet, "Kundkorg");

        dgLista.DataSource = objDataSet;

        dgLista.DataBind();

        objConnection.Close();


        return objDataSet;


thus far it doesent work?

plz help.
 
Old January 13th, 2006, 03:42 PM
Authorized User
 
Join Date: Dec 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

strSQL = "SELECT artNr, produktNamn, farg, langd, antal, pris FROM Kundvagn WHERE Kundvagn.kundNr ='" + Session["Knr"].ToString() + "';";
        strSQL2 += "SELECT Summa, Frakt FROM FraktKostnad WHERE FraktKostnad.kundNr ='" + Session["Knr"].ToString() + "';";
        objDataAdapter = new OleDbDataAdapter(strSQL, objConnection);
        objDataSet = new DataSet();
        objDataAdapter.Fill(objDataSet, "Kundkorg");

        objDataAdapter = new OleDbDataAdapter(strSQL2, objConnection);
        objDataAdapter.Fill(objDataSet, "FraktKostnad");

The problem was that you create your second dataAdapter over the first one... It destroyed the previous data...






Similar Threads
Thread Thread Starter Forum Replies Last Post
How many DataAdapter in a Form melvik ADO.NET 5 November 2nd, 2008 03:14 AM
dataadapter/command/dataset sql timeout rajn ASP.NET 1.0 and 1.1 Professional 0 April 1st, 2008 02:58 PM
dataadapter --- dataset thas123 ASP.NET 2.0 Basics 0 December 13th, 2006 08:58 AM
DataAdapter.Fill() kadesskade VB.NET 2002/2003 Basics 1 July 10th, 2006 02:57 AM
Dynamic DataAdapter from Typed Dataset dhay1999 ADO.NET 3 May 3rd, 2005 07:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.