Reputation: 65
I'm pretty lost at this point (been working at it for a while not and am hitting a wall / deadline) but the error message I am being thrown is after I hit btnupdate to update the fields in the database.
Full Error Message:
Could not find control 'txtTitle' in ControlParameter 'Title'.
Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="floater">
<h3>Books</h3>
<asp:DropDownList
id="DropDownList_Books"
DataSourceId="srcBooks"
DataTextField="Title"
DataValueField="Id"
Runat="server" />
<asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px"
onclick="btnSelect_Click" />
</div>
<asp:GridView
id="grdBooks"
DataSourceID="srcBooks_Description"
Runat="server" Visible="False" />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
</div>
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
Text="Cancel" Visible="False" />
<asp:FormView
id="frmEditBook"
DataKeyNames="Cat_Id"
DataSourceId="srcBooks_Description"
DefaultMode="Edit"
Runat="server" Visible="False"
style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
<EditItemTemplate>
<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />
<asp:TextBox
id="txtTitle"
Text='<%#Bind("Title")%>'
Runat="server" />
<br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<asp:TextBox
id="txtDescription"
Text='<%#Bind("Description")%>'
Runat="server" />
<br />
<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server"
ConnectionString="Data Source=****;;Initial Catalog=***;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="srcBooks_Description" runat="server"
ConnectionString="Data Source=****, 14330";Initial Catalog=****;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=@Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=@Title,
Description=@Description WHERE Cat_Id=@Id">
<SelectParameters>
<asp:ControlParameter
Name="Id"
Type="int32"
ControlID="DropDownList_Books"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Code Behind:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
grdBooks.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
frmEditBook.Visible = false;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
frmEditBook.Visible = true;
btnCancel.Visible = true;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
srcBooks_Description.Update();
}
catch (Exception except)
{
// Handle the Exception.
}
}
}
}
Upvotes: 0
Views: 12581
Reputation: 804
As you can see, the problem is the context. You can also put your sqldatasource inside the formview where the control being refered to is (not properly and very limited but could work)
Let's say you have a detailsview with two dropdowns in it, and one is dependant from another. What works for me is to put the Datasource in the same context (parent control) as the control refered and this will eliminate the need to reference the full path of the control.
My two cents is that this is very reminiscent on how you must reference controls in Access Forms with subforms. :)
<asp:DetailsView runat="server">
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDataSource1">
</asp:DropDownList>
</EditTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDatasource2">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * from table where field1=@field">
<SelectParameters>
<asp:ControlParameter ControlID="ParentDDL" Name="field"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditTemplate>
</asp:TemplateField>
</asp:DetailsView>
Upvotes: 1
Reputation: 711
You -can- refer to the control adequately.
You need to prefix the controlid value of your update parameters with the control id of your enclosing view (FormView 'frmEditBook' in this case)
Replace the UpdateParamters section of your SqlDataSource with the following:
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="frmEditBook$txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="frmEditBook$txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
Note that
ControlId="DropDownList_Books"
remains the same as this control is not within the bounds of the FormView.
I don't recommend using the 'full name' provided by the browser... particularly if you're using master pages... it could change as you rearrange your layouts.
Upvotes: 4
Reputation: 2689
you shouldn't have used <asp:ControlParameter..
but <asp:Parameter..
and the SqlDataSource control with the help of the bind method is intelligent enough to locate the controls to get values from for the parameters.
Update this portion of the sqlDataSource this way:
....
<UpdateParameters>
<asp:Parameter Name="Title" Type="String"/>
<asp:Parameter Name="Description" Type="String"/>
<asp:Parameter Name="Id" Type="Int32"/>
</UpdateParameters>
.....
Please note the Type attribute and supply the relevant type.
(The problem is SqlDataSource control cannot explicitly access controls defined in a databound control)
Upvotes: 0
Reputation: 68410
The problem is that txtTitle
is inside a FormView so it's not accesible in the way you're trying to do on the SqlDataSource. You can't access it direclty by ID.
Here you have an MSDN article that may help to to work with a FormView and SqlDataSource
Upvotes: 0
Reputation: 8393
You need to refer to your control with its full name.
You can easily find the control's full name when you open the page in the browser. Simply view source and you will see it.
Upvotes: 0