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