0

In my project I am trying to have the user register, log in then insert their semester data as in when it starts and how many weeks it has but I am struggling with the code can someone help me? I will be posting the semester window. Please tell me what I am missing I know it's somewhere where I need to insert. In the SQL I have first in the table id,username,password,semester,number of weeks,start date.

This is my Semester window

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace Task1
{
    
    public partial class MainWindow : Window
    {
        SqlCommand cmd;
        SqlConnection cn;
        SqlDataReader dr;
        readonly Semester Semester = new Semester();
        
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void MainWindow_Load(object sender, EventArgs e)
        {
        cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename="+Environment.CurrentDirectory+"\\Database1.mdf;Integrated Security=True");
        cn.Open();
        }

        
        void AddModule_Click(object sender, RoutedEventArgs e)
        {
            var sem = semester.Text;
            
            dr = cmd.ExecuteReader();
            Semester.Name = sem;
            cmd = new SqlCommand("INSERT INTO LoginTable (semester,numberOfWeeks,startdate) values(@semester,@number of weeks,startdate)", cn);
            cmd.Parameters.AddWithValue("semester", semester.Text);
            cmd.Parameters.AddWithValue("number of weeks", numberOfWeeks.Text);
            cmd.Parameters.AddWithValue("startdate", startDate.Text);
            cmd.ExecuteNonQuery();

            // Check whether is empty
            if (string.IsNullOrWhiteSpace(sem) || string.IsNullOrEmpty(sem)) {
                MessageBox.Show("Semester Name cant be empty!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return; // Name cant be empty so returning with an error.
            }

            // Save into Memory
            

            // Try parsing user input and Save into Memory
            if (!int.TryParse(numberOfWeeks.Text, out Semester.NumberOfWeeks)) {
                MessageBox.Show("Number of weeks invalid!!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return; // NumOfWeek cant be other than numbers so returning with an error.
            }
            
            // Check if start date is set
            if(!startDate.SelectedDate.HasValue){
                MessageBox.Show("Choose Start Date!!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return; // invalid returning with an error.
            }
            
            // Get selected date and Save into Memory
            Semester.StartDate = startDate.SelectedDate.Value;
            
            // Show Module UI
            new DisplayWindow(Semester, DisplayWindow.DisplayMode.REMAINING)
                .Show();
            
            // Close This UI
            Close();
        }
    }
}


Ramona
  • 1
  • 2
  • Not sure exactly what your question is, but you have some issues with your code: `dr = cmd.ExecuteReader();` is superfluous and doesn't make sense in context. `SqlConnection` and `SqlCommand` need to be in `using` blocks, do *not* cache them, just create when you need them, and dispose immediately. `AttachDbFilename` is [a bad idea](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename). [`AddWithValue` is evile](https://www.dbdelta.com/addwithvalue-is-evil/), specify types and lengths exactly – Charlieface Oct 25 '21 at 19:15

0 Answers0