Solved

ObjectDataSource field accessing using C#

Posted on 2007-11-16
4
741 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

20 Experts available now in Live!

Get 1:1 Help Now