Solved

ObjectDataSource field accessing using C#

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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