Click here to Skip to main content
15,895,709 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to validate an URI ? Pin
OriginalGriff2-May-22 8:27
mveOriginalGriff2-May-22 8:27 
GeneralRe: How to validate an URI ? Pin
jsc422-May-22 22:38
professionaljsc422-May-22 22:38 
GeneralRe: How to validate an URI ? Pin
Dave Kreskowiak3-May-22 2:47
mveDave Kreskowiak3-May-22 2:47 
AnswerRe: How to validate an URI ? Pin
Richard Deeming2-May-22 21:45
mveRichard Deeming2-May-22 21:45 
GeneralRe: How to validate an URI ? Pin
Maximilien3-May-22 2:55
Maximilien3-May-22 2:55 
GeneralRe: How to validate an URI ? Pin
Richard Deeming3-May-22 3:19
mveRichard Deeming3-May-22 3:19 
GeneralRe: How to validate an URI ? Pin
Maximilien3-May-22 6:33
Maximilien3-May-22 6:33 
QuestionWPF C# Selected Row from a Listbox in the Database Pin
Diane Dolinski27-Apr-22 3:31
Diane Dolinski27-Apr-22 3:31 
Hi at all

Im new in C# WPF and SQL
since days im sitting on a problem I could not solve, and hope of help but fdid not find the Problem, because I'm not so good in sql:
I try to delete(Löschen) a Dataset from Listbox and the Database behind(MySQL), and i do not find my failure.

Please can you help me?
I tried to send a Screenshot, but dont find a way.... Thanks for your help

XAML-Code
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Artikel" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="60" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Column="0" Grid.Row="0" Margin="10,50,0,0">
            <StackPanel Orientation="Vertical" DataContext="{Binding SelectedItem, ElementName=cbArtikel}">
                <Label x:Name="lblArtikel" FontWeight="Bold" Content="Artikel" Margin="45,5" />
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblName" DockPanel.Dock="Left" Width="100" Height="30" Content="Name:" />
                    <TextBox x:Name="tbName" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding name}"/>
                </DockPanel>
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblBeschreibung" DockPanel.Dock="Left" Width="100" Height="30" Content="Beschreibung:" />
                    <TextBox x:Name="tbBeschreibung" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding beschreibung}" />
                </DockPanel>
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblGroesse" DockPanel.Dock="Left" Width="100" Height="30" Content="Größe:" />
                    <TextBox x:Name="tbGroesse" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding groesse}" />
                </DockPanel>
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblFarbe" DockPanel.Dock="Left" Width="100" Height="30" Content="Farbe:" />
                    <TextBox x:Name="tbFarbe" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding farbe}" />
                </DockPanel>
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblMenge" DockPanel.Dock="Left" Width="100" Height="30" Content="Menge:" />
                    <TextBox x:Name="tbMenge" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding menge}" />
                </DockPanel>
                <DockPanel Width="300" Margin="0,5,0,5">
                    <Label x:Name="lblPreis" DockPanel.Dock="Left" Width="100" Height="30" Content="Preis:" />
                    <TextBox x:Name="tbPreis" DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding preis}" />
                </DockPanel>
            </StackPanel>
        </StackPanel>
        <StackPanel Grid.Column="1" Grid.Row="0">
            <Label Name="lblListArtikel" FontWeight="Bold" FontSize="16" Margin="50,50,0,5">Liste der Artikel</Label>
            <ListBox x:Name="lbArtikel" Width="300" Height="220">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <DockPanel>
                            <TextBlock Text="{Binding name}" FontWeight="Bold" DockPanel.Dock="Left" Width="85" />
                            <TextBlock Text="{Binding beschreibung}" FontWeight="Thin" Width="90" />
                            <TextBlock Text="{Binding groesse}" FontWeight="Thin" Width="30" />
                            <TextBlock Text="{Binding farbe}" FontWeight="Thin" Width="30" />
                            <TextBlock Text="{Binding menge}" FontWeight="Thin" Width="30" />
                            <TextBlock Text="{Binding preis}" FontWeight="Thin" Width="30" />
                        </DockPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button x:Name="btnDelete" Content="Löschen" Width="100" Height="30" Background="Salmon" HorizontalAlignment="Center" Margin="0,15" Click="btnDelete_Click" />
        </StackPanel>
        <DockPanel Grid.Column="0" Grid.Row="1" LastChildFill="False" HorizontalAlignment="Left" Margin="50,0,0,0">
            <Button x:Name="btnAdd" Width="100" Height="30" Click="btnAdd_Click" Content="Hinzufügen" />
        </DockPanel>
        <DockPanel Grid.Column="1" Grid.Row="1" LastChildFill="False" HorizontalAlignment="Right" Margin="0,0,50,0">
            <Button x:Name="btnZurueck" Width="100" Height="30" Content="Zurück" Click="btnZurueck_Click" />
        </DockPanel>
    </Grid>
</Window>


Code-Behind for that WPF:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Odbc;
using System.Data;

namespace WpfApp1
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private OdbcConnection odbcConnection;
        public MainWindow()
        {
            InitializeComponent();
            odbcConnection = new OdbcConnection("DSN=dbdemo2;");
            ArtikelShow();

        }

        // Listbox für Artikel
        private void ArtikelShow()
        {
            try
            {
                string sqlArtikel = "SELECT * FROM artikel";
                OdbcCommand odbcCommand = new OdbcCommand(sqlArtikel, odbcConnection);
                OdbcDataAdapter odbcDataAdapter = new OdbcDataAdapter(odbcCommand);
                DataSet dataSet = new DataSet();
                odbcDataAdapter.Fill(dataSet, "tableArtikel");
                lbArtikel.ItemsSource = dataSet.Tables["tableArtikel"].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType() + Environment.NewLine + ex.Message, "Datenbankfehler");
            }
        }

        // Button Artikel löschen
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lbArtikel.SelectedValue == null)
            {
                MessageBox.Show("Keinen Eintrag zum Löschen ausgewählt");
            }
            else
            {
                try
                {
                    MessageBoxResult messageBoxResult = MessageBox.Show("Wollen Sie wirklich den Artikel aus der Datenbank löschen?", "Bitte bestätigen Sie den Löschvorgang", MessageBoxButton.YesNo);
                    if (messageBoxResult == MessageBoxResult.Yes)
                    {
                        string sqlDelete = "DELETE FROM artikel WHERE id=?";
                        OdbcCommand odbcCommand = new OdbcCommand(sqlDelete, odbcConnection);
                        odbcConnection.Open();
                        odbcCommand.Parameters.AddWithValue("@id", Int32.Parse(lbArtikel.SelectedValue.ToString()));
                        odbcCommand.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.GetType() + Environment.NewLine + ex.Message, "Datenbankfehler");
                }
                finally
                {
                    odbcConnection.Close();
                    ArtikelShow();
                }
            }
        }

        // Button für Artikel hinzufügen -> Eingabe für Artikel
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text.Equals(""))
            {
                MessageBox.Show("Bitte einen Artikelnamen eingeben");
            }
            else
            {
                try
                {
                    string sqlInsert = "INSERT INTO artikel VALUES(NULL, ?, ?, ?, ?, ?, ?)";
                    OdbcCommand odbcCommand = new OdbcCommand(sqlInsert, odbcConnection);
                    odbcConnection.Open();
                    odbcCommand.Parameters.AddWithValue("@name", tbName.Text);
                    odbcCommand.Parameters.AddWithValue("@beschreibung", tbBeschreibung.Text);
                    odbcCommand.Parameters.AddWithValue("@groesse", tbGroesse.Text);
                    odbcCommand.Parameters.AddWithValue("@farbe", tbFarbe.Text);
                    odbcCommand.Parameters.AddWithValue("@menge", Convert.ToInt32(tbMenge.Text));
                    odbcCommand.Parameters.AddWithValue("@preis", Convert.ToDouble(tbPreis.Text));
                    odbcCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.GetType() + Environment.NewLine + ex.Message, "Datenbankfehler");
                }
                finally
                {
                    odbcConnection.Close();
                    ArtikelShow();
                }
            }
        }

        // Button Zurück zu Lieferanten-Fenster
        private void btnZurueck_Click(object sender, RoutedEventArgs e)
        {
            //Lieferanten lieferanten = new Lieferanten();
            //lieferanten.Show();
            this.Close();
        }

    }
}

AnswerRe: WPF C# Selected Row from a Listbox in the Database Pin
Gerry Schmitz27-Apr-22 6:17
mveGerry Schmitz27-Apr-22 6:17 
GeneralRe: WPF C# Selected Row from a Listbox in the Database Pin
Diane Dolinski27-Apr-22 8:42
Diane Dolinski27-Apr-22 8:42 
AnswerRe: WPF C# Selected Row from a Listbox in the Database Pin
Mycroft Holmes27-Apr-22 12:43
professionalMycroft Holmes27-Apr-22 12:43 
GeneralRe: WPF C# Selected Row from a Listbox in the Database Pin
Diane Dolinski27-Apr-22 19:47
Diane Dolinski27-Apr-22 19:47 
GeneralRe: WPF C# Selected Row from a Listbox in the Database Pin
Diane Dolinski28-Apr-22 1:24
Diane Dolinski28-Apr-22 1:24 
QuestionHow do I avoid ORM tools 'polluting' the model? Pin
Patrick Skelton26-Apr-22 21:47
Patrick Skelton26-Apr-22 21:47 
AnswerRe: How do I avoid ORM tools 'polluting' the model? Pin
Richard Deeming26-Apr-22 22:16
mveRichard Deeming26-Apr-22 22:16 
GeneralRe: How do I avoid ORM tools 'polluting' the model? Pin
Patrick Skelton27-Apr-22 21:18
Patrick Skelton27-Apr-22 21:18 
AnswerRe: How do I avoid ORM tools 'polluting' the model? Pin
Gerry Schmitz27-Apr-22 6:11
mveGerry Schmitz27-Apr-22 6:11 
GeneralRe: How do I avoid ORM tools 'polluting' the model? Pin
Patrick Skelton27-Apr-22 21:20
Patrick Skelton27-Apr-22 21:20 
AnswerRe: How do I avoid ORM tools 'polluting' the model? Pin
jschell8-May-22 7:54
jschell8-May-22 7:54 
QuestionTLS 1.3 on Windows Vista Pin
moxol26-Apr-22 4:27
moxol26-Apr-22 4:27 
AnswerRe: TLS 1.3 on Windows Vista Pin
RedDk26-Apr-22 9:38
RedDk26-Apr-22 9:38 
AnswerRe: TLS 1.3 on Windows Vista Pin
Randor 26-Apr-22 17:22
professional Randor 26-Apr-22 17:22 
AnswerRe: TLS 1.3 on Windows Vista Pin
Richard Deeming26-Apr-22 21:22
mveRichard Deeming26-Apr-22 21:22 
PraiseRe: TLS 1.3 on Windows Vista Pin
Randor 26-Apr-22 21:54
professional Randor 26-Apr-22 21:54 
AnswerRe: TLS 1.3 on Windows Vista Pin
moxol27-Apr-22 1:01
moxol27-Apr-22 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.