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#.
tomcalefAsked:
Who is Participating?
 
traxionConnect With a Mentor Commented:
Or:

DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
DataView objView = SqlDataSource6.Select(dsArgs) as DataView; /* Added 'as DataView' */
DataTable dtData = objView.ToTable();

TextBox1.Text = dtData.Rows(0)("In_Date").ToString();
0
 
traxionCommented:
DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();
DataView objView = (DataView)dataSource.Select(dsArgs);
DataTable dtData = objView.ToTable();

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

0
 
tomcalefAuthor Commented:
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?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
renjurdevanCommented:
TextBox1.Text  = objDs.Tables[0].Rows[0]["In_Date"] ;

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

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

Regards
Renju
0
 
traxionCommented:
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
0
 
tomcalefAuthor Commented:
The error message now is system.data.datatable.rows is a property but used like a method.
0
 
tomcalefAuthor Commented:
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.
0
 
traxionCommented:
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();
0
 
traxionCommented:
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();
0
 
tomcalefAuthor Commented:
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?)"

0
 
traxionCommented:
try the very last example.
0
 
tomcalefAuthor Commented:
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
0
 
traxionCommented:
: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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.