How retrive data in sqldatasource?

V

VB Programmer

I have a sqldatasource on a webform. How do I (in code) retrieve the value
of a particular column in the first row? Do I have to save the
sqldatasource SELECT results into a datatable and look at it that way? Code
samples would help.

Thanks!
 
A

Alec MacLean

If your data is in a SqlDataReader, then (assuming you're happy retrieving
the data as a single rowset using an key value):

While sdr.Read
'Read values from named columns in the data reader.
'Columns match the output cols from the SQL query.
yourIntValue = CInt(sdr("yourIntColumn"))
yourStringValue = CStr(sdr("yourVarcharColumn"))
End While


If your data is in a dataset (which can include all the source rows if you
need to), then:

private sub yourHandler()
dim yourDS as dataset

'... load the dataset...

'Access the row by number and the column by name...
yourIntValue =
Cint(yourDS.Tables("YourTableName").Rows(rowNumber).Item("yourIntColumn"))

'...Or you can use integer positions for both if you want, as demonstrated
for the string version.
yourStringValue =
CStr(yourDS.Tables("YourTableName").Rows(rowNumber).Item(columnNumber))

...

End Sub

Object indexes are always zero-based in dotnet, so first row is zero (0).

(That's from memory, so syntax might not be exact, but should be close
enough to point you in right direction)

Al
 
V

VB Programmer

Using ASP.NET 2.0. Is there an easy way to do this with an SQLDataSource
object that is on a webform?
 
S

Soydaner Ulker

/*
Below code should help you.

*/

DataView dw = (DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);


foreach (DataColumn dc in dw.Table.Columns)
{
Response.Write(dc.Caption + ";");
}
Response.Write("\r\n");



foreach (DataRow dr in dw.Table.Rows)
{
foreach (DataColumn dc in dw.Table.Columns)
{
Response.Write(dr[dc.ToString()].ToString() + ";");
}
Response.Write("\r\n");

}


/*
Also see the link
http://aspnet.4guysfromrolla.com/articles/022206-1.aspx

*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top