Home All Groups Group Topic Archive Search About

How retrive data in sqldatasource?

Author
22 Feb 2006 7:39 PM
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!

Author
23 Feb 2006 7:36 PM
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



Show quote
"VB Programmer" <d***@emailme.com> wrote in message
news:O$qD9e%23NGHA.456@TK2MSFTNGP15.phx.gbl...
>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!
>
Author
25 Feb 2006 4:28 AM
VB Programmer
Using ASP.NET 2.0.  Is there an easy way to do this with an SQLDataSource
object that is on a webform?

Show quote
"Alec MacLean" <alec.maclean@NO19SPAM60.copeohs.com> wrote in message
news:uRQc5BLOGHA.1832@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>
> "VB Programmer" <d***@emailme.com> wrote in message
> news:O$qD9e%23NGHA.456@TK2MSFTNGP15.phx.gbl...
>>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!
>>
>
>
Author
14 Mar 2006 2:22 PM
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

*/
---
Posted via www.DotNetSlackers.com

AddThis Social Bookmark Button