[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1873
  • Last Modified:

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#.
0
tomcalef
Asked:
tomcalef
  • 7
  • 5
  • 2
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
traxionCommented:
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now