Solved

ObjectDataSource field accessing using C#

Posted on 2007-11-16
4
742 Views
Last Modified: 2013-11-26
Using Visual Web Developer 2005 Express and concentrating on C#

My C# knowledge is limited but want to do this in C# if possible, if its not it would at least let me sleep tonight.

I have added a ObjectDataSource and pointed a Formview component to it successfully (and it works !!!)

In the C# Page_Load i want to access a field in the ObjectDataSource and have not the foggiest idea how to do it, i don't just want to DataBind the field i want to get it into a String so i can beat up the value, whisk it slightly then eventually stuff it out again.

I have been able to create a new SqlConnection and get the field that way its just that i'm annoyed that i can't figure out how to get the field that is already available on the ASP component !

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string WhatsTheValue = ObjectDataSource1["GIVEMETHESTUPIDVALUE"];
        }

    }


0
Comment
Question by:corbpm
  • 2
4 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 20298140
not sure.. if you need a value from the data you fetched and boud to contol and on postback you are finding value?

but from datasouce you can get and set QueryText .. like
ObjectDataSource1.SelectCommand and others... but not value
for value you must look into the control which you have bound with datasource?
thanks
0
 
LVL 6

Accepted Solution

by:
ventaur earned 500 total points
ID: 20298411
You can get at the value, but only via the FormView, after it is data bound to. Check out the code below.
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" OnDataBound="FormView1_DataBound">

...

 

 

protected void FormView1_DataBound(object sender, EventArgs e) {

	// Get the data item after type casting it appropriately (assuming a DataSet or DataTable).

	// * Cast to a business class if using that approach.

	DataRowView Data = FormView1.DataItem as DataRowView;

	if (Data != null) {

		string SomeValue = (string)Data["SomeValue"];

		// Do some stuff with the value or get another value.

	}

}

Open in new window

0
 
LVL 6

Expert Comment

by:ventaur
ID: 20298439
If that doesn't suit your needs, get the data via a connection, command, data adapter, and DataSet. Then, get your value(s) to manipulate accordingly from the DataSet. Finally, manually bind the data to the FormView.

FormView1.DataSource = Data.Tables[0]; // Data is the DataSet filled.
FormView1.DataBind();
0
 

Author Comment

by:corbpm
ID: 20298714
Thanks that works !!!!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now