Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an accept button that is the one that saves the data for the database.  I wanted to know how I can insert the ComboBox options


C#
private async void BTAccept_Click(object sender, RoutedEventArgs e)
        {
            string rbRiesgo;
          try
            {
                const string SqlString = "insert into AllergyTable(DuoDate,PetName, Category,Risk,Treatment,Note)values(@duoDate,@petName,@category,@risk,@treatment,@note)";

                 using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    

                    conn.Open();

                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            
                            cmd.CommandText = SqlString;
                            cmd.Parameters.Clear();

                            cmd.Parameters.Add("@duoDate", SqlDbType.Date).Value = DateAllergy.Date.Value.LocalDateTime;
                            cmd.Parameters.Add("@petName", SqlDbType.VarChar, 40).Value = txtnombre.Text;
                            cmd.Parameters.Add("@category", SqlDbType.VarChar, 40).Value = cbCategoria.Text;
                            
                            

                            

                            if (RBBajo.IsChecked == true)
                            {
                                rbRiesgo = "0";
                                cmd.Parameters.Add("@risk", SqlDbType.VarChar, 20).Value = RBBajo.Content;
                            }
                            if (RBMedio.IsChecked == true)
                            {
                                rbRiesgo = "1";
                                cmd.Parameters.Add("@risk", SqlDbType.VarChar, 20).Value = RBMedio.Content;
                            }
                            if(RBAlto.IsChecked == true)
                            {
                                rbRiesgo = "2";
                                cmd.Parameters.Add("@risk", SqlDbType.VarChar, 20).Value = RBAlto.Content;
                            }


                            cmd.Parameters.Add("@treatment", SqlDbType.VarChar, 50).Value = txtTratamiento.Text;
                            cmd.Parameters.Add("@note", SqlDbType.VarChar, 50).Value = txtNotas.Text;


                            cmd.ExecuteNonQuery();
                            conn.Close();

                            MessageDialog Md = new MessageDialog("The record has been inserted");
                            await Md.ShowAsync();

                        }
                    }

                }
            }

            catch (Exception ex)
            {
                var Msg = new MessageDialog("Exception:" + ex.Message);
                Msg.Commands.Add(new UICommand("Close"));

            }

        }

XML


<ComboBox Margin="10,20,0,0" x:Name="cbCategoria" SelectedItem="{Binding Item,Mode=TwoWay}"  Width="180">
                            <ComboBoxItem IsSelected="True">
                                <TextBlock x:Name="txtAlimento"   x:Uid="Alimento" Style="{StaticResource TextTextStyle}"/>
                            </ComboBoxItem>
                            <ComboBoxItem>
                                <TextBlock x:Name="txtMedioAmbiente"   x:Uid="MedioAmbiente"  Style="{StaticResource TextTextStyle}"/>
                            </ComboBoxItem> </ComboBox>


What I have tried:

I have entered in the line of adding for Combobox the database selectItem, Text etc.
C#
cmd.Parameters.Add("@category", SqlDbType.VarChar, 40).Value = cbCategoria;
Posted
Updated 7-Dec-21 7:23am
Comments
[no name] 26-Nov-21 13:58pm    
Under "ITEM SELECTION":

https://docs.microsoft.com/en-us/windows/apps/design/controls/combo-box
javier ramos lopez 26-Nov-21 16:24pm    
I want to know how to implement the code to enter the TextBlock that is in the ComboboxItem for the database

You're doing it wrong.

You've already got a view-model, and the controls are bound to properties on that view-model.

Rather than extracting values directly from the controls to insert into your database, use the properties on the view-model which are bound to those controls.

So instead of:
C#
cmd.Parameters.Add("@category", SqlDbType.VarChar, 40).Value = cbCategoria.Text;
you would have:
C#
cmd.Parameters.Add("@category", SqlDbType.VarChar, 40).Value = Item;
 
Share this answer
 
Comments
javier ramos lopez 3-Dec-21 11:37am    
If I put Item when pressing the save button it does not put the data in the database
at the end I have focused on the combobox theme by removing the Textblock and leaving: <ComBoboxItem Content=""/>


now the code to insert the Content of the ComBoboItem into the database:


C#
cmd.Parameters.Add("@category", SqlDbType.VarChar, 40).Value = cbCategoria.SelectedBoxItem.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