Saturday, February 25, 2012

question - inserting with formview control

Using formview control, I'm trying to insert a record. In the process of inserting, I want to save the categoryid from a shared class. The code runs fine but categoryid gets saved as null...Any pointers?? [When I display, it shows the value]

thanks

protected void savebutton_click(object sender, EventArgs e)
{
//this statement runs fine under debug...but values do not get saved?
SqlDataSource1.InsertParameters.Add("@.category", SharedValues.category.ToString());
SqlDataSource1.Insert();
}

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="itemid" DefaultMode="edit" OnDataBound="FormView1_DataBound">
<EditItemTemplate>

Title:
<asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>'>
</asp:TextBox><br/>
Description:
<asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' Rows="10" TextMode="MultiLine" Width="500px" Height="166px">
</asp:TextBox><br/>


<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>

</EditItemTemplate>
<InsertItemTemplate>
Title:
<asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>'>
</asp:TextBox><br/>
Description:
<asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' Rows="10" TextMode="MultiLine" Width="500px" Height="166px">
</asp:TextBox><br/>

<asp:label ID="categoryLbl" runat="server" Text='<%# SharedValues.category %>'></asp:label>
<br/>

<div class="actionbuttons">
<Club:RolloverButton ID="GreenRolloverButton3" CommandName="Insert" Text="Save"
runat="server" OnClick="savebutton_click" />
<Club:RolloverLink ID="GreenRolloverLink2" Text="Cancel" runat="server" NavigateURL="Classifieds.aspx" />
</div>
</InsertItemTemplate>

</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
InsertCommand="insert into tads (title,[description], categoryid) values (@.title,@.description, @.category)"
SelectCommand="Select title, [description], categoryid from tads whereitemid=@.itemid"
UpdateCommand="update tads set title = @.title, [description] = @.description where itemid = @.itemid" >
<SelectParameters>
<asp:QueryStringParameter Name="itemid" QueryStringField="itemid" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="description" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="category" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>

You shouldn't need to handle the Button's Click event at all. The FormView automatically knows how to insert items with a data source control. The command name of the button should be enough to get the insert to happen. Also, rather than adding the parameter to the data source's InsertParameters collection, you should handle the data source's Inserting event and add a SqlParameter to the command:
void SqlDataSource1_Inserting(...) {
SqlParameter parameter = new SqlParameter("@.param_name", <param value>);
e.Command.Parameters.Add(parameter);
}
Thanks,
Eilon

No comments:

Post a Comment