Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Data to Variable ASP.NET 2.0

Status
Not open for further replies.

GavW

Programmer
Jan 13, 2006
58
GB
Hey all!

i was wondering if somebody could help me with this problem.

In .NET 1.1 i used the following syntax to load data into a variable:

foreach (DataRow dr in dataset.Tables["tbl"].Rows)
{
string name = Convert.ToString(dr["Name"]);
}

This will not work however with my .NET 2.0 created DataSets and was wondering how best to acheive this result?

Thanks
 
Which bit "doesn't work" and what errors are you getting? You can still loop through DataRows in a DataTable in version 2.0 of the framework.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ok well what I am trying to do is assign the SQL query that I have set up within my DataTable in my DataSet.

I will explain my situation in more detail:

I am trying to extract all records from my products table (tblProducts) where the ProductID is equal to a given value (therefore only one record).

My DataSet (ds1) consists of the tblProducts DataTable and a "GetProductsByProductID" method within my table adapter which performs the SQL query:

"SELECT * FROM tblProducts WHERE ProductID = @ProductID"

I have worked out how to populate a Repeater using this method:

tblProductsTableAdapter adapter = new tblProductsTableAdapter();
Repeater1.DataSource = adapter.GetProductsByProductID("value");
Repeater1.DataBind();

I just haven't figured out the syntax required to loop through the datatable using the query above, assigning the data to variables.
 
And what error do you get if you try the for each loop that you posted above?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
well i tried doing this:

string productid = Request.QueryString["ProductID"];

DataSet ds = new DataSet;
tblProductsTableAdapter adapter = new tblProductsTableAdapter();
ds = adapter.GetProductByProductID(Convert.ToInt32(productid));

foreach (DataRow dr in ds.Tables["tblProducts"].Rows)
{
string name = Convert.ToString(dr["Name"]);
}

When attempting to compile that I get:

Cannot implicitly convert type 'ds1.tblProductsDataTable' to 'System.Data.DataSet'
 
I don't see where you create the table named "tblProducts". If you don't specify a table name use:
Code:
foreach (DataRow dr in ds.Tables[[b]0[/b]].Rows)

Jim
 
Which line do you get that error on? I don't see any reference to a DataSet named "ds1" in any of your above code...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
tblProducts I have created in my Dataset (ds1.xsd) using VS 2005 which provides the nice interface for DataSets.

The codebehind file that this code resides in has the line "using ds1TableAdapters;"

The error is generated on the line that reads
ds = adapter.GetProductByProductID(Convert.ToInt32(productid));

what I am trying to do is assign the "GetProductsByProductID" query that I have created in the tblProductsTableAdapter to the dataset within my ForEach Loop. This is what is generating the error.
 
I'm getting confused as to what code you have where and what you are trying to do with it. Please create a simple test page with only the relevant code that produces the problem and paste it here (along with any additonal code such as your "GetProductByProductID" method).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Alright I have only two relevant items; the page I am trying to display the data on (products.aspx) and the ds1 Dataset (ds1.xsd)

products.aspx codebehind looks like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using ds1TableAdapters;

public partial class products : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string productid = Request.QueryString["ProductID"];
tblProductsTableAdapter adapter = new tblProductsTableAdapter();
ds = adapter.GetProductByProductID(Convert.ToInt32(productid));

foreach (DataRow dr in ds.Tables["tblProducts"].Rows)
{
string name = Convert.ToString(dr["Name"]);
}
}



The DataTable within the ds1.xsd interface looks similiar to this:

TBLPRODUCTS
ProductID
Category
Name
Price
Image
TBLPRODUCTSTABLEADAPTER
Fill,GetProducts()
FillByProductID,GetProductByProductID(@ProductID)


All that I require my products.aspx page to do is run the GetProductByProductID query and assign the Category, Name, Price and Image field data to individual variables.
 
Calling GetProductByProductID(@ProductID)
should just fill your existing dataset, there is no reason to create a new dataset. I belive the error is happening because the function returns a datatable, not a dataset.
 
ok so I have edited the two lines where I had included "ds" so that they now look like this:

DataSet ds1 = adapter.GetProductByProductID(Convert.ToInt32(productid));

foreach (DataRow dr in ds1.Tables["tblProducts"].Rows)

This returns the error:
Cannot implicitly convert type 'yankee.tblProductsDataTable' to 'System.Data.DataSet'

That is the problem jbenson but therefore how would I code this page to produce the results that I want using the datatable returned rather than a dataset?
 
Try:
DataTable dt1 = adapter.GetProductByProductID(Convert.ToInt32(productid));
 
thanks a lot mate I have managed to edit the code to get it to perform what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top