Question WPF Datagrid is erasing the SSMS Database and showing empty

Govind Sankar

Active member
Joined
May 15, 2020
Messages
35
Programming Experience
Beginner
Hello,

I am doing a wpf sample database project. I created a database called school details with different tables. One table is StudentDetails where I just entered the name of random five students and another table is five sets of marks for phyics, chemistry, biology and maths. Now the first part of the program is to run a wpf window which when I run show the database of the details of the five students in a datagrid. This part works fine. When I run the program I can see the database of the five students details. Next comes the second part. Here there is a button called science and when I press that button it should open a new wpf window that shows the five sets of marks. But when I press the science button, the datagrid comes up with one empty row and just the headings. Also when I press the science, it also is removing the science marks from the database server, because when I go back in SQL server management studio, I see the science marks database has become empty and all the five values are gone. Why is this so. Can anyone help me. I try to upload the whole project but the website says the file is too large even after I zip it. So i will just post the whole code here. Kindly help me. Thank you.

XML:
//This is the main WPF
<Window x:Class="SchoolDetails.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:SchoolDetails"
        mc:Ignorable="d"
        Title="SchoolDetails" Height="450" Width="800">
    <Border Margin="5,5,5,5">
        <Grid Margin="10,10,10,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" HorizontalAlignment="Left" Height="179" Margin="10,10,0,0" VerticalAlignment="Top" Width="723">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0" Margin="10,10,10,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="ID" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        <TextBox Grid.Column="1" x:Name="ID" Padding="2"></TextBox>
                    </Grid>
                    <Grid Grid.Row="1" Margin="10,10,10,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Name" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        <TextBox Grid.Column="1" x:Name="Name" Padding="2"></TextBox>
                    </Grid>
                    <Grid Grid.Row="2" Margin="10,10,10,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Age" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        <TextBox Grid.Column="1" x:Name="Age" Padding="2"></TextBox>
                    </Grid>
                    <Grid Grid.Row="3" Margin="10,10,10,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Class" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        <TextBox Grid.Column="1" x:Name="Class" Padding="2"></TextBox>
                    </Grid>
                </Grid>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Height="39" Margin="122,10,109,10" Width="130" x:Name="Science" Content="Science" Click="Science_Click"></Button>
                    <Button Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Height="39" Margin="122,10,109,10" Width="130" x:Name="Social" Content="Social" Click="Social_Click"></Button>
                    <Button Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Height="39" Margin="122,10,109,10" Width="130" x:Name="Language" Content="Language" Click="Language_Click"></Button>
                </Grid>
            </Grid>
            <Grid Grid.Row="1">
                <DataGrid x:Name="StudentDetailsDataGrid" VerticalAlignment="Top"  ItemsSource="{Binding ElementName=grid, Mode=OneWay}" HorizontalAlignment="Left"
                          HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" Grid.IsSharedSizeScope="True"
                          SelectionChanged="StudentDetailsDataGrid_SelectionChanged" Height="185" Width="752">                   
                </DataGrid>
            </Grid>
        </Grid>
    </Border>
</Window>
 
// This is the main wpf program

namespace SchoolDetails
{   
    public partial class MainWindow : Window
    {
        public SchoolDetails.School_DataDataSet schoolData = new School_DataDataSet();
        public StudentDetailsTableAdapter studentDetailsTableAdapter = new StudentDetailsTableAdapter();
        public void AdapterStudentDetails()
        {
            studentDetailsTableAdapter.Fill(schoolData.StudentDetails);
            /*
               DataGridAdapter_UNIO.Fill(ds.UNIO);
               UNIO_DG.ItemsSource = ds.UNIO;
               ds.UNIO.DefaultView.Sort = "ID asc";
             */
            StudentDetailsDataGrid.ItemsSource = schoolData.StudentDetails;
            schoolData.StudentDetails.DefaultView.Sort = "ID asc";
        }

        public MainWindow()
        {
            InitializeComponent();
            AdapterStudentDetails();
        }      

        private void Science_Click(object sender, RoutedEventArgs e)
        {
            var ScienceScore = new ScienceMarks();
            ScienceScore.Show();
        }       
    }
}

// This is the second wpf
<Window x:Class="SchoolDetails.ScienceMarks"
        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:SchoolDetails"
        mc:Ignorable="d"
        Title="ScienceMarks" Height="450" Width="800">
    <Grid HorizontalAlignment="Left" Height="385" Margin="17,15,0,0" VerticalAlignment="Top" Width="756">
        <DataGrid x:Name="GSDatagrid" HorizontalAlignment="Left" Height="349" Margin="10,10,0,0" VerticalAlignment="Top" Width="724">
           
        </DataGrid>

    </Grid>
</Window>

// and this is the program for that
namespace SchoolDetails
{
   
    public partial class ScienceMarks : Window
    {
        public SchoolDetails.School_DataDataSet schoolData2 = new School_DataDataSet();
        public GeneralScienceMarksTableAdapter GSTableAdapter = new GeneralScienceMarksTableAdapter();
        public void AdapterGSMarks()
        {
            GSTableAdapter.Fill(schoolData2.GeneralScienceMarks);
            GSDatagrid.ItemsSource = schoolData2.GeneralScienceMarks;

        }
        public ScienceMarks()
        {
            InitializeComponent();
            AdapterGSMarks();
        }

    }  
}
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Not at a PC right now to read all of the code fully, but based on the one way data binding in the XAML that I saw, I highly doubt that it is the DataGrid which is erasing your database since a one way binding goes from the data model to the view, but not the other direction.
 
Top Bottom