c# – How to store the value of the SelectedItem of the DropDownList in the database?

Question:

I want to register a customer and one of his attributes is gender

Database: the sex attribute is an nvarchar

ASP.NET: I'm working with a listview to insert customer data

<asp:SqlDataSource ID="clientes" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Loja %>"
        ProviderName="<%$ ConnectionStrings:Loja.ProviderName %>"
        EnableCaching="true"
        SelectCommand="SELECT [Id], [sexo] FROM Cliente ORDER BY Id" 
        InsertCommand="INSERT INTO [Cliente] ([sexo]) VALUES (@sexo)" 
        UpdateCommand="UPDATE [Cliente] SET [sexo] = @sexo WHERE [Id] = @Id" 
        DeleteCommand="DELETE FROM [Doente] WHERE [Id] = @Id">
        <InsertParameters>
            <asp:Parameter Name="sexo" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="sexo" Type="String" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>

    <asp:ListView ID="listaClientes" runat="server" DataKeyNames="Id"
        DataSourceID="clientes">
        <ItemTemplate>

        </ItemTemplate>
        <EditItemTemplate>

        </EditItemTemplate>
        <InsertItemTemplate>

                    Sexo:
                    <asp:RadioButtonList ID="rblSexo" runat="server"
                        RepeatDirection="Horizontal"
                        OnSelectedIndexChanged="rblSexo_SelectedIndexChanged">
                        <asp:ListItem Text ="Masculino" Value="1" />
                        <asp:ListItem Text ="Feminino" Value="2" />
                    </asp:RadioButtonList><br />
        </InsertItemTemplate>
    </asp:ListView>

My doubt is how will I store in the attribute (BD gender) what the user selects in the RadioButtonList?

I thought of using:

string = "INSERT INTO [Doente] (sexo) VALUES (@sexo)";
command.Parameters.Add("@sexo", rblSexo.SelectedItem.Value);

in the Inserted Event of sqlDataSource, what do you think?

Answer:

As your field expects an nvarchar , I suppose you want to pass the text "Male" or "Female", if that is correct, you would pass it this way.

protected void rblSexo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = myConnString;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO [Doente] (sexo) VALUES (@sexo)";
                cmd.Parameters.Add("@sexo", SqlDbType.NVarChar).Value = rblSexo.SelectedItem.Text;
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqle)
            {
            }
            finally
            {
                conn.Close();
            }
        }

Just a detail, if you are doing this in the event of OnSelectedIndexChanged="rblSexo_SelectedIndexChanged , this makes a row inserted in the table each time the user changes the sexo , another OnSelectedIndexChanged only work if you declare AutoPostBack="true" .

Scroll to Top