2017-06-25 4 views
-1

J'ai un programme avec datetimepicker dedans. Voici la capture d'écran. enter image description hereErreur datetimepicker

Le problème est: Si le jour d'arrivée est égale à la date d'aujourd'hui - commande SQL indique l'erreur. Je devrais sélectionner la date d'aujourd'hui manuellement pour éviter l'erreur. Le format du datetimepicker est "Personnalisé" et il est similaire à "jj.MM.aaaa".

Voici mon code:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Text.RegularExpressions; 

namespace OldCityInn_v0._3 
{ 
    public partial class mainform : Form 
    { 
     private OleDbConnection connection = new OleDbConnection(); 
     public mainform(string user) 

     { 
      InitializeComponent(); 
      connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Hp\Desktop\DataBases\oldcityinn.accdb; 
      Persist Security Info=False;"; 
      label2.Text = user; 

     } 

     private void button3_Click(object sender, EventArgs e) 
     { 

     } 

     private void mainform_Load(object sender, EventArgs e) 
     { 
      editbtn.Visible = false; 
      /*************************************/ 
      tabPage1.Text = @"Arrival"; 
      tabPage2.Text = @"In House"; 
      tabPage3.Text = @"Departure"; 
      /* -------------------------------- */ 
      reservebtn.Text = "Reservation"; 
      managementbtn.Text = "Hotel Management"; 
      availabilitybtn.Text = "Availability"; 
      roomingbtn.Text = "Rooming List"; 
      infobtn.Text = "Information About Software"; 
      logoutbtn.Text = "Logout"; 
      closebtn.Text = "Close"; 
      /**************************************/ 

      availabilitybtn.Enabled = false; 

      /* ---------------------------------- */ 
      reservationpanel.Visible = false; 
      mngmntpanel.Visible = false; 
      try 
      { 
       connection.Open(); 
       toolStripStatusLabel1.Text = "Connected"; 
       connection.Close(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Failed to connect the system." + ex); 
        toolStripStatusLabel1.Text = "Not Ready"; 
        button1.Enabled = false; 
      } 
     } 

     private void button7_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void button6_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
      Form1 f1 = new Form1(); 
      f1.Show(); 
     } 

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
      string g_status = "Arrival"; 
      string gname = textBox1.Text; 
      string gsurname = textBox2.Text; 
      string gcompany = textBox3.Text; 
      string gna = comboBox1.SelectedItem.ToString(); 
      string groom = comboBox2.SelectedItem.ToString(); 
      string grate = textBox4.Text.ToString(); 
      string ginfo = richTextBox1.Text; 
      string gpaidstatus = "Not Paid"; 
      /*--------------*/ 


       TimeSpan difference = this.dateTimePicker2.Value - this.dateTimePicker1.Value; 
       var days = difference.TotalDays; 
       label12.Text = days.ToString(); 
       int total = int.Parse(textBox4.Text) * int.Parse(label12.Text); 


      /*--------------*/ 

       connection.Open(); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connection; 
       command.CommandText = "INSERT INTO guestreg(g_name,g_surname,g_company,g_ad,g_dd,g_amountofdays,g_na,g_room,g_rate,g_totalrate,g_info,u_added,u_timeadded,g_status,g_paidstatus) VALUES('" + gname + "','" + gsurname + "','" + gcompany + "','" + this.dateTimePicker1.Text + "','" + this.dateTimePicker2.Text + "','" + days + "','" + gna + "','" + groom + "','" + grate + "','"+total+"','" + ginfo + "','" + label2.Text + "','" + dateTimePicker3.Value + "','"+g_status+"','"+gpaidstatus+"')"; 
       command.ExecuteNonQuery(); 
       command.CommandText = "INSERT INTO billing(g_name,g_surname,g_company,g_ad,g_dd,g_amountofdays,g_totalrate) VALUES('" + gname + "','" + gsurname + "','" + gcompany + "'," + this.dateTimePicker1.Value.ToString("dd'.'MM'.'yyyy") + "','" + this.dateTimePicker2.Value.ToString("dd'.'MM'.'yyyy") + "','" + days + "','" + total + "')"; 
       command.ExecuteNonQuery(); 
       MessageBox.Show("Reservation completed! Thank you, "+label2.Text+"!"); 
       connection.Close(); 
       /******************************/ 

       /*****************************/ 
       textBox1.Text = ""; 
       textBox2.Text = ""; 
       textBox3.Text = ""; 
       textBox4.Text = ""; 
       richTextBox1.Text = ""; 
       comboBox1.SelectedIndex = -1; 
       comboBox2.SelectedIndex = -1; 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error: " + ex); 
      } 
     } 

     private void reservebtn_Click(object sender, EventArgs e) 
     { 
      reservationpanel.Visible = true; 
      mngmntpanel.Visible = false; 
     } 

     private void groupBox3_Enter(object sender, EventArgs e) 
     { 

     } 

     private void billingbtn_Click(object sender, EventArgs e) 
     { 

     } 

     private void searchbd_Click(object sender, EventArgs e) 
     { 
      if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage1"]) 
      { 
       checkinbtn.Enabled = true; 
       cancelbtn.Enabled = true; 

       try 
       { 

        string dtparrival = this.dateTimePicker4.Text.ToString(); 
        string statusarrival = "Arrival"; 

        string strSql = "SELECT * FROM guestreg WHERE g_status = '" + statusarrival + "' AND g_ad = '" + dtparrival + "'"; 


        OleDbCommand cmd = new OleDbCommand(strSql, connection); 
        connection.Open(); 
        cmd.CommandType = CommandType.Text; 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        DataTable scores = new DataTable(); 
        da.Fill(scores); 
        arrivaldgv.DataSource = scores; 
        /**************************************************************************/ 
        this.arrivaldgv.Columns[0].Visible = false; 
        this.arrivaldgv.Columns["g_totalrate"].Visible = false; 
        this.arrivaldgv.Columns["u_added"].Visible = false; 
        this.arrivaldgv.Columns["u_timeadded"].Visible = false; 
        /**************************************************************************/ 
        this.arrivaldgv.Columns["g_name"].HeaderText = "Name"; //1 
        this.arrivaldgv.Columns["g_surname"].HeaderText = "Surname"; //2 
        this.arrivaldgv.Columns["g_company"].HeaderText = "Company"; //3 
        this.arrivaldgv.Columns["g_ad"].HeaderText = "Arrival Day"; //4 
        this.arrivaldgv.Columns["g_dd"].HeaderText = "Departure Day"; //5 
        this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //6 
        this.arrivaldgv.Columns["g_na"].HeaderText = "Number of Adults"; //7 
        this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //8 
        this.arrivaldgv.Columns["g_room"].HeaderText = "Room Number"; //9 
        this.arrivaldgv.Columns["g_rate"].HeaderText = "Rate"; //10 
        this.arrivaldgv.Columns["g_totalrate"].HeaderText = "Total Rate"; //11 
        this.arrivaldgv.Columns["g_info"].HeaderText = "Information"; //12 
        this.arrivaldgv.Columns["u_added"].HeaderText = "User"; //13 
        this.arrivaldgv.Columns["u_timeadded"].HeaderText = "Time"; //14 
        this.arrivaldgv.Columns["g_status"].HeaderText = "Status"; //15 
        this.arrivaldgv.Columns["g_paidstatus"].HeaderText = "Payment"; 
        /**************************************************************************/ 
        connection.Close(); 
       } 
       catch 
       { 
        MessageBox.Show("Please, try again."); 
       } 
      } 
      else if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"]) 
      { 

       try 
       { 
        string statusinhouse = "In House"; 

        string strSql = "SELECT * FROM guestreg WHERE g_status = '" + statusinhouse + "'"; 


        OleDbCommand cmd = new OleDbCommand(strSql, connection); 
        connection.Open(); 
        cmd.CommandType = CommandType.Text; 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        DataTable scores = new DataTable(); 
        da.Fill(scores); 
        inhousedgv.DataSource = scores; 
        /**************************************************************************/ 
        this.inhousedgv.Columns[0].Visible = false; 
        this.inhousedgv.Columns["g_totalrate"].Visible = false; 
        this.inhousedgv.Columns["u_added"].Visible = false; 
        this.inhousedgv.Columns["u_timeadded"].Visible = false; 
        /**************************************************************************/ 
        this.inhousedgv.Columns["g_name"].HeaderText = "Name"; //1 
        this.inhousedgv.Columns["g_surname"].HeaderText = "Surname"; //2 
        this.inhousedgv.Columns["g_company"].HeaderText = "Company"; //3 
        this.inhousedgv.Columns["g_ad"].HeaderText = "Arrival Day"; //4 
        this.inhousedgv.Columns["g_dd"].HeaderText = "Departure Day"; //5 
        this.inhousedgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //6 
        this.inhousedgv.Columns["g_na"].HeaderText = "Number of Adults"; //7 
        this.inhousedgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //8 
        this.inhousedgv.Columns["g_room"].HeaderText = "Room Number"; //9 
        this.inhousedgv.Columns["g_rate"].HeaderText = "Rate"; //10 
        this.inhousedgv.Columns["g_totalrate"].HeaderText = "Total Rate"; //11 
        this.inhousedgv.Columns["g_info"].HeaderText = "Information"; //12 
        this.inhousedgv.Columns["u_added"].HeaderText = "User"; //13 
        this.inhousedgv.Columns["u_timeadded"].HeaderText = "Time"; //14 
        this.inhousedgv.Columns["g_status"].HeaderText = "Status"; //15 
        this.inhousedgv.Columns["g_paidstatus"].HeaderText = "Payment"; 
        /**************************************************************************/ 
        connection.Close(); 
       } 
       catch 
       { 
        MessageBox.Show("Please, try again."); 
       } 


      } 
      else if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage3"]) 
      { 


       string dtparrival = this.dateTimePicker4.Text.ToString(); 



       string strSql = "SELECT * FROM guestreg WHERE g_dd = '" + dtparrival + "'"; 


       OleDbCommand cmd = new OleDbCommand(strSql, connection); 
       connection.Open(); 
       cmd.CommandType = CommandType.Text; 
       OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
       DataTable scores = new DataTable(); 
       da.Fill(scores); 
       departuredgv.DataSource = scores; 
       /**************************************************************************/ 
       this.departuredgv.Columns[0].Visible = false; 
       this.departuredgv.Columns["g_totalrate"].Visible = false; 
       this.departuredgv.Columns["u_added"].Visible = false; 
       this.departuredgv.Columns["u_timeadded"].Visible = false; 
       /**************************************************************************/ 
       this.departuredgv.Columns["g_name"].HeaderText = "Name"; //1 
       this.departuredgv.Columns["g_surname"].HeaderText = "Surname"; //2 
       this.departuredgv.Columns["g_company"].HeaderText = "Company"; //3 
       this.departuredgv.Columns["g_ad"].HeaderText = "Arrival Day"; //4 
       this.departuredgv.Columns["g_dd"].HeaderText = "Departure Day"; //5 
       this.departuredgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //6 
       this.departuredgv.Columns["g_na"].HeaderText = "Number of Adults"; //7 
       this.departuredgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //8 
       this.departuredgv.Columns["g_room"].HeaderText = "Room Number"; //9 
       this.departuredgv.Columns["g_rate"].HeaderText = "Rate"; //10 
       this.departuredgv.Columns["g_totalrate"].HeaderText = "Total Rate"; //11 
       this.departuredgv.Columns["g_info"].HeaderText = "Information"; //12 
       this.departuredgv.Columns["u_added"].HeaderText = "User"; //13 
       this.departuredgv.Columns["u_timeadded"].HeaderText = "Time"; //14 
       this.departuredgv.Columns["g_status"].HeaderText = "Status"; //15 
       this.departuredgv.Columns["g_paidstatus"].HeaderText = "Payment"; 
       /**************************************************************************/ 
       connection.Close(); 
      } 
     } 

     private void checkinbtn_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       int selectedrowindex = arrivaldgv.SelectedCells[0].RowIndex; 
       DataGridViewRow selectedRow = arrivaldgv.Rows[selectedrowindex]; 
       string value = Convert.ToString(selectedRow.Cells["ID"].Value); 

       try 
       { 
        var cmd = new OleDbCommand(); 
        cmd.CommandType = CommandType.Text; 
        cmd.CommandText = "UPDATE guestreg SET g_status='In House' where ID [email protected]"; 
        cmd.Parameters.AddWithValue("@id", value); 
        cmd.Connection = connection; 
        connection.Open(); 
        cmd.ExecuteNonQuery(); 
        { 
         MessageBox.Show("The guest was checked in. Thank you " + label2.Text); 

         connection.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Error:  " + ex); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("You can't check in the guest that was already cheked in."); 
      } 
     } 

     private void managementbtn_Click(object sender, EventArgs e) 
     { 
      mngmntpanel.Visible = true; 
      reservationpanel.Visible = false; 
     } 

     private void cancelbtn_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       int selectedrowindex = arrivaldgv.SelectedCells[0].RowIndex; 
       DataGridViewRow selectedRow = arrivaldgv.Rows[selectedrowindex]; 
       string value = Convert.ToString(selectedRow.Cells["ID"].Value); 

       try 
       { 
        var cmd = new OleDbCommand(); 
        cmd.CommandType = CommandType.Text; 
        cmd.CommandText = "UPDATE guestreg SET g_status='Canceled' where ID [email protected]"; 
        cmd.Parameters.AddWithValue("@id", value); 
        cmd.Connection = connection; 
        connection.Open(); 
        cmd.ExecuteNonQuery(); 
        { 
         MessageBox.Show("Reservation was canceled. Thank you " + label2.Text); 
         arrivaldgv.Update(); 
         arrivaldgv.Refresh(); 

         connection.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Error:  " + ex); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("You can't cancel checked in guest."); 
      } 
     } 

     private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      var regex = new Regex(@"[^a-zA-Z0-9\s\b]"); 
      if (regex.IsMatch(e.KeyChar.ToString())) 
      { 
       e.Handled = true; 
      } 
     } 

     private void textBox2_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      var regex = new Regex(@"[^a-zA-Z0-9\s\b]"); 
      if (regex.IsMatch(e.KeyChar.ToString())) 
      { 
       e.Handled = true; 
      } 
     } 

     private void textBox3_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      var regex = new Regex(@"[^a-zA-Z0-9\s\b]"); 
      if (regex.IsMatch(e.KeyChar.ToString())) 
      { 
       e.Handled = true; 
      } 
     } 

     private void textBox4_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      var regex = new Regex(@"[^0-9\s\b]"); 
      if (regex.IsMatch(e.KeyChar.ToString())) 
      { 
       e.Handled = true; 
      } 
     } 

     private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      var regex = new Regex(@"[^a-zA-Z0-9.,$\s\b]"); 
      if (regex.IsMatch(e.KeyChar.ToString())) 
      { 
       e.Handled = true; 
      } 
     } 

     private void departuredgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      billing blng = new billing(); 
      blng.label12.Text = this.label2.Text; 
      blng.label13.Text = this.departuredgv.CurrentRow.Cells[0].Value.ToString(); 
      blng.textBox1.Text = this.departuredgv.CurrentRow.Cells[1].Value.ToString(); 
      blng.textBox2.Text = this.departuredgv.CurrentRow.Cells[2].Value.ToString(); 
      blng.textBox3.Text = this.departuredgv.CurrentRow.Cells[3].Value.ToString(); 
      blng.textBox4.Text = this.departuredgv.CurrentRow.Cells[4].Value.ToString(); 
      blng.textBox5.Text = this.departuredgv.CurrentRow.Cells[5].Value.ToString(); 
      blng.textBox6.Text = this.departuredgv.CurrentRow.Cells[6].Value.ToString(); 
      blng.textBox7.Text = this.departuredgv.CurrentRow.Cells[9].Value.ToString(); 
      blng.textBox8.Text = this.departuredgv.CurrentRow.Cells[10].Value.ToString(); 
      blng.ShowDialog(); 
     } 

     private void inhousedgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      billing blng = new billing(); 
      blng.label12.Text = this.label2.Text; 
      blng.label13.Text = this.inhousedgv.CurrentRow.Cells[0].Value.ToString(); 
      blng.textBox1.Text = this.inhousedgv.CurrentRow.Cells[1].Value.ToString(); 
      blng.textBox2.Text = this.inhousedgv.CurrentRow.Cells[2].Value.ToString(); 
      blng.textBox3.Text = this.inhousedgv.CurrentRow.Cells[3].Value.ToString(); 
      blng.textBox4.Text = this.inhousedgv.CurrentRow.Cells[4].Value.ToString(); 
      blng.textBox5.Text = this.inhousedgv.CurrentRow.Cells[5].Value.ToString(); 
      blng.textBox6.Text = this.inhousedgv.CurrentRow.Cells[6].Value.ToString(); 
      blng.textBox7.Text = this.inhousedgv.CurrentRow.Cells[9].Value.ToString(); 
      blng.textBox8.Text = this.inhousedgv.CurrentRow.Cells[10].Value.ToString(); 
      blng.ShowDialog(); 

     } 

     private void editbtn_Click(object sender, EventArgs e) 
     { 


     } 

     private void arrivaldgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      edit edt = new edit(); 
      edt.label12.Text = this.label2.Text; 
      edt.label13.Text = this.arrivaldgv.CurrentRow.Cells[0].Value.ToString(); 
      edt.textBox1.Text = this.arrivaldgv.CurrentRow.Cells[1].Value.ToString(); 
      edt.textBox2.Text = this.arrivaldgv.CurrentRow.Cells[2].Value.ToString(); 
      edt.textBox3.Text = this.arrivaldgv.CurrentRow.Cells[3].Value.ToString(); 
      edt.dateTimePicker1.Value = Convert.ToDateTime(this.arrivaldgv.CurrentRow.Cells[4].Value); 
      edt.dateTimePicker2.Value = Convert.ToDateTime(this.arrivaldgv.CurrentRow.Cells[5].Value); 
      edt.comboBox2.Text = this.arrivaldgv.CurrentRow.Cells[7].Value.ToString(); 
      edt.comboBox1.SelectedItem = this.arrivaldgv.CurrentRow.Cells[8].Value.ToString(); 
      edt.textBox9.Text = this.arrivaldgv.CurrentRow.Cells[9].Value.ToString(); 
      edt.richTextBox1.Text = this.arrivaldgv.CurrentRow.Cells[11].Value.ToString(); 
      edt.ShowDialog(); 
     } 

     private void infobtn_Click(object sender, EventArgs e) 
     { 
      info inf = new info(); 
      inf.ShowDialog(); 
     } 
    } 

} 

Voici l'erreur:

enter image description here

+0

Cela doit être un problème de type coulée. Quel est le type de données de arrivalDate dans votre base de données? Plus est dateTimePicker1 le sélecteur pour la date d'arrivée. Répondez à cela et peut-être que je pourrais être en mesure d'aider. – devTimmy

+1

Rechercher les commandes paramétrées, l'injection SQL et les procédures stockées/EntityFramework –

+0

@ user3499361 type est personnalisé (dd.MM.yyyy) –

Répondre

2

Si vous développez votre code pour utiliser les paramètres, la plupart de vos problèmes vont disparaître. Cependant, pour la concaténation, vous devez respecter les types de données et leurs expressions de chaîne valides. Les valeurs de texte doivent être entre guillemets, les nombres non et les dates dans Access SQL doit être entouré de octothorpes. Ainsi:

... "','" + gcompany + "',#" + this.dateTimePicker1.Value.ToString("yyyy'/'MM'/'dd") + "#,#" + this.dateTimePicker2.Value.ToString("yyyy'/'MM'/'dd") + "#," + days + ",'" ... 
+0

malheureusement, toujours ne fonctionne pas ... :( –

+0

Vous devrez parcourir vos miles de code.Ce qui précède est juste une erreur accrocheur.Votre message d'erreur est assez clair et des points à d'autres parties – Gustav

+0

sûr, j'espère que je vais découvrir l'erreur :) –