Link to home
Start Free TrialLog in
Avatar of tomcalef
tomcalef

asked on

Pull value to textbox from sqldatasource, Asp.net C#

I am trying to pull a date value from a sqldatasource that only selects 1 row with a few columns. The name of the date column is In_Date. It would be the first row third column. I am trying to pull the value so I can use it for a calculation, so the goal is to get it to a label or textbox. I'm not sure what else I can post to explain, so If you need more information please ask. The aplication is a web asp.net page in c#.
Avatar of traxion
traxion

DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
DataView objView = (DataView)dataSource.Select(dsArgs);
DataTable dtData = objView.ToTable();

Then, TextBox1.Text = objView.Rows(0)("In_Date");

Avatar of tomcalef

ASKER

I'm getting an error that says "    'system.data.dataview' does not contain a definition for 'Rows'

I have replaced datasource with sqldatasource6 (the data source i'm trying to use) and everything else was left the same. Any ideas or do you need more information?
TextBox1.Text  = objDs.Tables[0].Rows[0]["In_Date"] ;

where objDs is dataset and assumes that first table first row contains data

Regards
Renju
in traxion: solution i thk instead of dataview' it is dtData
 
TextBox1.Text = dtData .Rows(0)("In_Date");

Regards
Renju
Yes, I'm sorry.  You're populating the DataTable from the DataView so it should be:

Textbox1.Text = dtData.Rows(0)("In_Date");

Thanks Renju
The error message now is system.data.datatable.rows is a property but used like a method.
This is the code thus far:
DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
        DataView objView = (DataView) SqlDataSource6.Select(dsArgs);
        DataTable dtData = objView.ToTable();

        TextBox1.Text = dtData.Rows[0]["In_Date"].ToString();
       
When I press the button that runs this action I get a box that highlights
DataTable dtData = objView.ToTable();
and a box comes up saying      NullReferenceException was unhandled by usercode

it suggests adding the new keyword but I have no idea where.
hmm...
Let's try it without the direct cast:

DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
DataView objView = SqlDataSource6.Select(dsArgs);
DataTable dtData = objView.ToTable();

TextBox1.Text = dtData.Rows(0)("In_Date").ToString();
You could also utilize a property of the SelectArguments to make sure something is set, like the StartRowIndex:

dsArgs.StartRowIndex = 0

so the code would look like this:

DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
dsArgs.StartRowIndex = 0;
DataView objView = SqlDataSource6.Select(dsArgs);
DataTable dtData = objView.ToTable();

TextBox1.Text = dtData.Rows(0)("In_Date").ToString();
ASKER CERTIFIED SOLUTION
Avatar of traxion
traxion

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It doesn't seem to like that either.
Code:
DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
        DataView objView = SqlDataSource6.Select(dsArgs);
        DataTable dtData = objView.ToTable();

           Label1.Text = dtData.Rows[0]["In_Date"].ToString();

Error
"Error      1      Cannot implicitly convert type 'System.Collections.IEnumerable' to 'System.Data.DataView'. An explicit conversion exists (are you missing a cast?)"

try the very last example.
I was getting very frustrated by this till I found an issue with my datasource, Sorry for the hold up. My source wasn't selecting the right data and causing an empty return. Your solution was on the money traxion. Thank you
:D  You're welcome.  It was frustrating me as well!  I didn't have a test platform and I was converting from my VB code to C# so I was worried the symantics weren't correct.  Glad you got it working!

Thanks,

Dustin