Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DetailsView, swap textbox for dropdownlist, not inserting through sqldatasource

Posted on 2009-05-06
6
Medium Priority
?
760 Views
Last Modified: 2012-05-06
Hi,

I am using a details view to run inserts on a sql database. One of the 3 columns I am inserting to in a int. By default the details view generates a textbox - I have changed that through the designer to a drop down box and used a sql data source inside the row template to populate with a choice of options.

when I run the code and fill in the details, select an option from the drop down list I've created, the databaes is not being populated? any ideas?
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
            CellPadding="2" DataKeyNames="ID" DataSourceID="TestDS" DefaultMode="Insert" 
            ForeColor="Black" GridLines="None" Height="50px" Width="500px" 
            oniteminserted="ForwardPage" >
            <FooterStyle BackColor="Tan" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
                HorizontalAlign="Center" />
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" 
                    InsertVisible="False" ReadOnly="True">
                </asp:BoundField>
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:TemplateField HeaderText="BodyText" SortExpression="BodyText">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BodyText") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Height="300px" 
                            Text='<%# Bind("BodyText") %>' TextMode="MultiLine" Width="450px"></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("BodyText") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Language" SortExpression="Lang">
                    <EditItemTemplate>
                        <asp:TextBox ID="dropdownlist1" runat="server" Text='<%# Bind("Lang") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:DropDownList ID="Textbox3" runat="server" AppendDataBoundItems="True" 
                            DataSourceID="SqlDataSource1" DataTextField="Languages" 
                            DataValueField="ID" SelectedValue='<%# Bind("Lang") %>'>
                            <asp:ListItem Text="Select a language" Value="-1" />
                        </asp:DropDownList>
                        &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:###############3 %>" 
                            SelectCommand="SELECT [ID], [Languages] FROM [Languages] ORDER BY [Languages]">
                        </asp:SqlDataSource>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Lang") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" InsertText="Save testimonial" 
                    ShowCancelButton="False" ShowInsertButton="True" />
            </Fields>
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <InsertRowStyle BackColor="LightGoldenrodYellow" />
            <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:DetailsView>
        <asp:SqlDataSource ID="TestDS" runat="server" 
            ConnectionString="<%$ ConnectionStrings:################# %>" 
            DeleteCommand="DELETE FROM [Testimonials] WHERE [ID] = @ID" 
            InsertCommand="INSERT INTO [Testimonials] ([Title], [BodyText], [Lang]) VALUES (@Title, @BodyText, @Lang)"
            SelectCommand="SELECT * FROM [Testimonials]" 
            
            UpdateCommand="UPDATE [Testimonials] SET [Title] = @Title, [BodyText] = @BodyText [Lang]=@Lang WHERE [ID] = @ID" 
            EnableViewState="False">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="BodyText" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="BodyText" Type="String" />
                <asp:ControlParameter ControlID="DetailsView1" DbType="Int32" Name="Lang" 
                    PropertyName="SelectedValue" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>

Open in new window

0
Comment
Question by:CJMilner
[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
  • 3
  • 3
6 Comments
 
LVL 10

Accepted Solution

by:
Christian de Bellefeuille earned 1500 total points
ID: 24320817
You don'T get any error?

First:
Your have a DropDownList named "Textbox3" (i would suggest to rename it to ddlLanguage).

Second:
In your insertParameters, you have written: ControlID="DetailsView1"...
It should be ControlID="ddlLanguage"?

0
 
LVL 1

Author Comment

by:CJMilner
ID: 24320997
Thanks for your comment.

I don't get an error the page just reloads with empty textboxes and no entry in to the database, I've rebuild the page and now I get the an entry in the database but just no content in the Lang column. My lang field of the details view is a template build, added from scratch with a default named dropdownlist, and a gui added datasource for the language selections.

It seems that I'm missing somewhere to tell the insert query to collect the lang column from the dropdown list in the template field I've just added - how do I do that?!

Thanks in advance.
0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24321161
Can you show me your latest code?

0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 1

Author Comment

by:CJMilner
ID: 24321189
I got annoyed with it not working using the VS2008 control's, so I just deleted the details view and created the textboxes (and drop down) manuall, then bound them to the sql data source.

This is working code below.
<table>
            <tr>
                <th>Title</th>
                <td><asp:TextBox runat="server" ID="txtTitle" Width="450px"></asp:TextBox></td>
            </tr>
            <tr>
                <th valign="top">Body</th>
                <td><asp:TextBox runat="server" Width="450px" Height="250px" ID="txtBody" 
                        TextMode="MultiLine"></asp:TextBox></td>
            </tr>
            <tr>
                <th>Language</th>
                <td>
                    <asp:DropDownList ID="ddlLanguage" runat="server" 
                    DataSourceID="SqlDataSource1" DataTextField="Languages" 
                    DataValueField="ID" AppendDataBoundItems="True">
                        <asp:ListItem Text="Select a language" Value="-1" />
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:############### %>" 
                    SelectCommand="SELECT [ID], [Languages] FROM [Languages] ORDER BY [Languages]" 
                        oninserted="ForwardToTestimonials">
                    </asp:SqlDataSource>
                </td>
            </tr>
        </table>
        <asp:Button ID="btnInsert" runat="server" Text="Insert testimonial" 
        onclick="SaveTestimonial" />
    <p>
        <asp:SqlDataSource ID="TestimonialsDS" runat="server" 
            ConnectionString="<%$ ConnectionStrings:################ %>" 
            InsertCommand="INSERT INTO Testimonials(Title, BodyText, Lang) VALUES (@Title, @BodyText, @Lang)" 
            SelectCommand="Select * from Testimonials">
            <InsertParameters>
                <asp:ControlParameter ControlID="txtTitle" Name="Title" PropertyName="Text" />
                <asp:ControlParameter ControlID="txtBody" Name="BodyText" PropertyName="Text" />
                <asp:ControlParameter ControlID="ddlLanguage" Name="Lang" 
                    PropertyName="SelectedValue" />
            </InsertParameters>
        </asp:SqlDataSource>
    </p>

Open in new window

0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24321346
I know its a little late, but here's your solution.
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23171694.html

Sorry for the late answer... but i'm not a give up :P.

The DropDown control can't be seen from outside the DetailView.  This is why its not working with the ControlParameter.    So if you want to use it in a DetailView, you have to trap the Inserting event of your datasource, and in this event you call the FindControl to find your drop down in the detailview, and Insert its value as a parameter like this:




Private Sub ds1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles TestimonialsDS.Inserting
        e.Command.Parameters("@Lang").Value = CType(DetailsView1.FindControl("ddlLanguage"), DropDownList).SelectedValue
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:CJMilner
ID: 24321397
I see and understand, thanks for the answer. Noted for next time! :)
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

610 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