0

I am trying to do a little To-Do app for my school project. I have one problem: i don't know how to delete items both generated by data template in app and those in database.

I've tried accesing items by getting selected item and then deleting it but at some point the id's of those items in db are diffrent from those in the app. I am using SQL server and in my db i have one table with 4 columns: ID(int, auto incremented, primary key), Task(varchar), Descr(varchar), Active(bit). Now i am trying to bind checkbox attribute isChecked to Active of Task class in my app.

this is my xaml code

<Window x:Class="ToDoApp2.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:ToDoApp2"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="400" ResizeMode="NoResize">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="244*"/>
        <ColumnDefinition Width="149*"/>
    </Grid.ColumnDefinitions>
    <TreeView x:Name="TrvMenu" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:ToDoTask}" ItemsSource="{Binding Tasks}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Content="{Binding Title}" IsChecked="{Binding active}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
    <TextBox x:Name="TaskTb" HorizontalAlignment="Left" Height="30" Margin="0,400,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2"/>
    <TextBox x:Name="DescriptionTb" HorizontalAlignment="Left" Height="80" Margin="0,430,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2"/>
    <Button  x:Name="CreateBtn" Content="Create New Task" HorizontalAlignment="Left" Margin="0,510,0,0" VerticalAlignment="Top" Width="197" Height="59" Click="Button_Click"/>
    <Button  x:Name="DeleteBtn" Content="Delete Selected Task" HorizontalAlignment="Left" Margin="197,510,-1,0" VerticalAlignment="Top" Width="196" Height="59" Click="DeleteBtn_Click" Grid.ColumnSpan="2"/>

</Grid>
</Window>

this is the class that represents one task in app

public class ToDoTask
{

    public ToDoTask()
    {
        this.Tasks = new ObservableCollection<ToDoTask>();
    }
    public string Title { get; set; }
    public bool active=true;

    public ObservableCollection<ToDoTask> Tasks { get; set; }
    }

And this is how i add new tasks to db and app

public MainWindow()
    {

        InitializeComponent();
        SQLCnn init = new SQLCnn();
        ObservableCollection<ToDoTask> initList = init.readQuery();
        for(int i=0; i < initList.Count; i++)
        {
            TrvMenu.Items.Add(initList[i]);
        }
        SQLCnn.connection.Close();

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (!String.IsNullOrEmpty(TaskTb.Text))
        {
            string value = TaskTb.Text;
            string desc = DescriptionTb.Text;

            ToDoTask task = new ToDoTask() { Title = value };
            task.Tasks.Add(new ToDoTask() { Title = desc });
            SQLCnn SQLtask = new SQLCnn();
            SQLtask.insertQuery(value, desc);
            TrvMenu.Items.Add(task);
        }
    }
  }

1 Answers1

-1

As you mentioned in your question you have a problem with Id's generation in your code. You should create a method where you pass all necessary data to create your task and after that, your database should return you back Id of newly created task. So in your case method SQLTask.insertQuery(...) should return id (int value) which is auto-generated by your database. Now you can assign it to newly created object ToDoTask task = new ToDoTask(); task.Id = ... and after that, you can add it to list of tasks. If you do that you will have a valid id value to delete the task from the database. And one more thing, fields are not supported as a binding source so in the ToDoTask class you should change active field to the property if you want to bind it.

In this case, it's all but...

In your project, you can use framework and patterns that will learn you a lot more cool stuff and increase the quality of your code. So to improve working with the database you can use ORM e.g. Entity Framework (https://learn.microsoft.com/en-us/ef/). To separate GUI code from a business logic code, you can use the MVVM pattern. Here you have a lot of options e.g. you can use one of the following projects:

To learn more about MVVM please look at this question: MVVM: Tutorial from start to finish?

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • Thanks a lot, i really appreciate your answer. There is one more thing i would like to ask. Is there some sql command or that could reset auto incremented id? – Oskar Michalkiewicz May 31 '19 at 10:57