473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to reference a control inside a formview template from a sqldatasource?

I'm trying to create a formview bound to a sqldatasource, and use a
stored procedure to insert data from the formview into several tables.
I have some simplified code below, the real code is much longer but
this should illustrate the problem:

<asp:FormView ID="bookFormView" runat="server"
DataSourceID="bookSqlDataSource">
<ItemTemplate>
<asp:textBox id="name"
text='<%# Eval("name") %>'
runat="server" />
<asp:textbox id="city"
text='<%#Eval("city") %>'
</ItemTemplate>
<InsertItemTemplate>
<asp:textBox id="name"
text='<%# Bind("name") %>'
runat="server" />
<asp:textbox id="city"
text='<%#Bind("city") %>'
runat = "server" />
</InsertItemTemplate>
</asp:FormView>

<asp:SqlDataSource ID="bookSqlDataSource" runat="server"
ConnectionString="<%$
ConnectionStrings:lapubsDBConnectionString %>"
ProviderName="<%$
ConnectionStrings:lapubsDBConnectionString.Provide rName %>"
InsertCommandType="StoredProcedure"
InsertCommand="usp_add_person"
SelectCommand="select name, city from view_namecity" >
<InsertParameters>
<asp:ControlParameter Name="Name" ControlID="???"
PropertyName="Text" Type="String" ConvertEmptyStringToNull="TRUE"
Size="20" />
<asp:ControlParameter Name="City" ControlID="???"
PropertyName="Text" Type="String" ConvertEmptyStringToNull="TRUE"
Size="20" />
</InsertParameters>
</asp:SqlDataSource>

The problem I'm having is in what to specify in the ControlID attribute
of the SQLdatasource's insertParameter. It seems that the only object
it will recognize is the Formview (ie. ControlID="bookFormView"). But
that isn't specifically the control that the param should be bound to.
But it doesn't understand if I specify ControlID="name" -- I think
that's because the page doesn't have such a control, the control is
embedded in the insertTemplate of the FormView. I've even tried
something like ControlID="bookFormview.FindControl('name')" but that
doesn't work either. How can I get a handle for the control that's
embedded inside the formview's template?

Or am I approaching this the wrong way? Should I specify the parameter
list inside the insertCommand: insertCommand="usp_add_person @name,
@city"? I can't really do that either because a couple of the
parameters are coming in from session and application variables, not
from the formview at all. I have to use a stored procedure because I
need to insert into multiple tables, and the app design won't allow me
to use triggers either.

Thanks, I hope someone can understand this question and has some
experience with this!

-- Ned

Jul 7 '06 #1
2 9864
Ned,

You simply need to use the Bind() method when binding any of your
fields to the FormView and then when you Insert or Update it will
automatically read the new values into your parameters. Use the
regular <asp:Parameter /to fill the values from the FormView, and
<asp:SessionParameter /to fill from session variables.

To make things easy on yourself, make sure all of your parameter names
match from your stored proc, to your parameter variables in your
SqlDataSource, to your FormView.

This should allow everything to just work. It is really pretty nice,
once you get used to it.

One more thing you might run into if you use Guids in your database.
There is currently a bug when it comes to passing Guids throught he
<asp:Parameter /variable. The wizard will default the type="object"
but you will want to just delete that and leave the type out and
everything will work.

Also, you should be able to use the FormView wizard to configure all of
this.

HTH,
Chris

Jul 8 '06 #2
Chris,

Sorry for the late reply and thanks for your help -- this did work. I
was making it more complicated than it was. I assumed a control inside
a formview was a control, so was trying to use a controlParameter
instead of just a Parameter. At the moment I am not using GUIDs but I
may modify the code to use them.

Thanks again.

-- Ned
chris wrote:
Ned,

You simply need to use the Bind() method when binding any of your
fields to the FormView and then when you Insert or Update it will
automatically read the new values into your parameters. Use the
regular <asp:Parameter /to fill the values from the FormView, and
<asp:SessionParameter /to fill from session variables.

To make things easy on yourself, make sure all of your parameter names
match from your stored proc, to your parameter variables in your
SqlDataSource, to your FormView.

This should allow everything to just work. It is really pretty nice,
once you get used to it.

One more thing you might run into if you use Guids in your database.
There is currently a bug when it comes to passing Guids throught he
<asp:Parameter /variable. The wizard will default the type="object"
but you will want to just delete that and leave the type out and
everything will work.

Also, you should be able to use the FormView wizard to configure all of
this.

HTH,
Chris
Jul 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pete | last post by:
I seem unable to get the FormView with the SqlDataSource control in ASP.NET 2.0 beta 2 to insert into my MySQL 4.11 database corectly. When I run the page and click on the Insert link a SQL insert...
3
by: sck10 | last post by:
Hello, I am creating a form for users to enter information about a lab and the members of the lab. I have one form (FormView) that they use to enter information about that lab. The keyvalue is...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
0
by: nate | last post by:
the error returned is this: Server Error in '/AnnAccRpt' Application. -------------------------------------------------------------------------------- Could not find control 'DropDownList1' in...
3
by: Marcial | last post by:
Greetings all! After a week of toiling I've finallay decided to post the code below with hopes that someone can point out my error(s). Basically I have a Gridview/Formview on a ASP.NEt 2.0...
0
by: davidgreen24 | last post by:
Suppose I have a SQLDataSource object that returns 20 rows. I have a FormView object bound to this control. Now when I click a hyperlink (with querystring param called productID) I want the row...
3
by: YMPN | last post by:
Hi Everyone, I'm deen from Riyadh. Please do help me with some problem i have. I have this formview control setup to recieved inputs from user (textbox, dropdownlist, others). After...
0
by: LiamLiamLiam | last post by:
G'day all. I having a problem with my formview. I'll ty to explain my situation as best as i can. I have a page with a search field at the top which is just a simple asp:textbox. Below that i...
0
by: Cirene | last post by:
I have a formview that is calling a stored procedure (thru an sqldatasource) to update 2 tables. When I execute the update it runs fine, with no errors. But the data does NOT change. Any ideas...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.