Click here to Skip to main content
15,884,959 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi, i want to ask, how to insert selected data into sql server.
fyi, I'm use visual studio 2008
this my html code,

ASP.NET
<asp:TemplateField HeaderText="Add">
            <ItemTemplate>
                <%# Eval("TagAdd") %>
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="TagAdd" runat="server" AutoPostBack="True"   
DataTextField="TagAdd" DataValueField="TagAdd">
                <asp:ListItem Value="0">Select Action</asp:ListItem>  
                <asp:ListItem Value="True">Enable</asp:ListItem>
                <asp:ListItem Value="False">Disable</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>

and here the code behind

VB
Dim TagAdd As DropDownList = CType(control.FindControl("ddlTagAdd"), SelectedItem).Text

cmd.CommandText = "INSERT INTO [ModuleDetails](ModuleName, SubModule, TagAdd, TagEdit, TagDelete) VALUES(@ModuleName, @SubModule, @TagAdd, @TagEdit, @TagDelete)"

cmd.Parameters.AddWithValue("@TagAdd", TagAdd.SelectedItem)


however, the error shows that SelectedItem is not declared(at Dim line).

can you help me on how to declared dropdownlist?
Posted
Updated 7-Sep-15 16:07pm
v2
Comments
ChauhanAjay 7-Sep-15 22:14pm    
Change the following line from
<asp:DropDownList ID="TagAdd" runat="server" AutoPostBack="True"
DataTextField="TagAdd" DataValueField="TagAdd">
to
<asp:DropDownList ID="ddlTagAdd" runat="server" AutoPostBack="True"
DataTextField="TagAdd" DataValueField="TagAdd">
Member 11966529 7-Sep-15 22:22pm    
i have change the code, it shows error at code behind. it says "Type 'SelectedItem' is not defined" and it refer to "Dim TagAdd As DropDownList = CType(control.FindControl("ddlTagAdd"), SelectedItem).Text" line.
ChauhanAjay 7-Sep-15 22:35pm    
try using the below code
Dim TagAdd As DropDownList = CType(control.FindControl("ddlTagAdd"))
Dim x as String = TagAdd.SelectedValue.ToString();
Member 11966529 7-Sep-15 22:47pm    
the above code shows
error "Name 'ddlTagAdd' is not declared" refer to line " Dim x As String = ddlTagAdd.SelectedValue.ToString()"

error "Name 'TagAdd' is not declared" refer to "cmd.Parameters.AddWithValue("@TagAdd", TagAdd.SelectedItem)"

error "Syntax error in cast operator; two arguments separated by comma are required." refer to "Dim TagAdd As DropDownList = CType(control.FindControl("ddlTagAdd"))"
ChauhanAjay 7-Sep-15 22:57pm    
Dim x As String = CType(control.FindControl("ddlTagAdd"), DropDownList).SelectedValue.ToString();

1 solution

HTML code

ASP.NET
<asp:templatefield headertext="Add" xmlns:asp="#unknown">
            <itemtemplate>
                <%# Eval("TagAdd") %>
            </itemtemplate>
            <footertemplate>
                <asp:dropdownlist id="ddlTagAdd" runat="server" autopostback="True">
DataTextField="TagAdd" DataValueField="TagAdd">
                <asp:listitem value="0">Select Action</asp:listitem>  
                <asp:listitem value="True">Enable</asp:listitem>
                <asp:listitem value="False">Disable</asp:listitem>
                </asp:dropdownlist>
            </footertemplate>
        </asp:templatefield>


code behind

VB
Dim TagAdd As String = CType(control.FindControl("ddlTagAdd"), DropDownList).SelectedValue.ToString

cmd.CommandText = "INSERT INTO [ModuleDetail](ModuleName, SubModule, TagAdd, TagEdit, TagDelete) VALUES(@ModuleName, @SubModule, @TagAdd, @TagEdit, @TagDelete)"

cmd.Parameters.AddWithValue("@TagAdd", TagAdd.ToString)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900