Solved

ObjectDataSource field accessing using C#

Posted on 2007-11-16
4
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

733 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