0

I want to make button enabled, depending on what data is in SQL table. I made table with boolean type column and now I want WPF to read it and make button enabled or disabled. I made class for connection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Windows;
using System.Data.SqlClient;
using System.Configuration;

namespace WpfCalendar.Classes
{
    class SQLconnection
    {
        public static string GetConnectionStrings()
        {
            string strConString = ConfigurationManager.ConnectionStrings["conString"].ToString();
            return strConString;
        }

        public static string sql;
        public static SqlConnection con = new SqlConnection();
        public static SqlCommand cmd = new SqlCommand("",con);
        public static SqlDataReader rd;
        public static DataTable dt;
        public static SqlDataAdapter da;

        public static void openConnection()
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.ConnectionString = GetConnectionStrings();
                    con.Open();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Brak połaczenia" + Environment.NewLine + ex.Message.ToString(), "C# WPF connect to SQL server", MessageBoxButton.OK, MessageBoxImage.Error);

            }
        }

        public static void closeConnection()
        {
            try
            {
                if(con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            catch(Exception)
            {
                
            }
        }

    }
}

And my window xaml.cs

public partial class Kalendarz : Window
    {
        public Kalendarz()
        {
            InitializeComponent();
           

        }



        private void Sprawdzanko1(object sender, RoutedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            bool klikniente;
            SQLconnection.openConnection();
            SQLconnection.cmd.CommandText = SQLconnection.sql;
            SQLconnection.sql = "Select [Otwarte] FROM Okienka WHERE Dzien LIKE '1';";
            SQLconnection.cmd.CommandType = CommandType.Text;
            SQLconnection.da = new SqlDataAdapter(SQLconnection.cmd);
            klikniente = (bool)SQLconnection.cmd.ExecuteScalar();
            

            if (klikniente == true && dt.Day == 1 && dt.Month == 12)
            {
                b1.IsEnabled = true;
            }
            else
            {
                b1.IsEnabled = false;
            }



            SQLconnection.closeConnection();
        }

I'm making a WPF advent calendar so the idea is to check if the button was clicked before and if date is correct. But the buttons don't react an I'm pretty sure the code isn't even executed...

Szopen
  • 9
  • 1

1 Answers1

0

Ok so I think I got it. It works so I'm fine with it :D

private void Sprawdzanko1()
        {
            bool klikniente;
            using (SQLconnection.con)
            {
                SQLconnection.openConnection();

                SqlCommand cmd = new SqlCommand("Select [Otwarte] FROM Okienka WHERE Dzien LIKE '1';", SQLconnection.con);

                klikniente = (bool)cmd.ExecuteScalar();
                SQLconnection.closeConnection();
            }

            if (klikniente == false && dt.Day == 1 && dt.Month == 12)
            {
                b1.IsEnabled = true;
            }
            else
            {
                b1.IsEnabled = false;
            }
        }

And then you call Sprawdzanko1();

Szopen
  • 9
  • 1