How to store the Selecteditem value from Dropdownlist in the database?

Asked

Viewed 304 times

4

I want to register a client and one of his attributes is sex

Database: the sex attribute is a nvarchar

ASP.NET: I am working with a listview to enter client 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 I will save in the attribute (BD sex) what the user selects in Radiobuttonlist?

I thought I’d use:

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

in sqlDataSource Inserted Event, what do you think?

  • This way it works?

  • I see some strange points in your implementation. 1 - Why do you use Nvarchar? Do you need to store data in Unicode? Sex is what? Number? or the initial? M/F? I understand that this is not directly relevant with your question but if you are using only M/F could use a char.

  • Solved? If positive, accept the answers because your question is still open :-(

2 answers

1

As your field awaits a nvarchar, I suppose you want to pass the text "Male" or "Female".

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 one detail, if you’ve been doing this at the OnSelectedIndexChanged="rblSexo_SelectedIndexChanged, this causes a row to be inserted into the table each time the user changes the sexo, another the OnSelectedIndexChanged will only work if you declare the AutoPostBack="true".

0

Since the radio button is a value 1 or 2, you can change the parameter type to Int32 and your Internet would be:

string = "INSERT INTO [Doente] (sexo) VALUES (@sexo)";
command.Parameters.Add("@sexo", Convert.ToInt32(rblSexo.SelectedItem.Value));
  • Base de dados: o atributo sexo é um nvarchar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.