2017-06-25 24 views
-1

私にはdatetimepickerを持つプログラムがあります。 ここにスクリーンショットがあります。 enter image description hereDatetimepickerエラー

問題がある: 到着日が今日の日付に等しい場合 - SQLコマンドはエラーを示しています。私は間違いを防ぐために今日を手動で選択する必要があります。 datetimepickerのフォーマットは "Custom"で、 "dd.MM.yyyy"のようです。ここで

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(); 
     } 
    } 

} 

は間違いです:あなたはパラメータを使用するようにコードを展開すると

enter image description here

+0

これは型キャストの問題でなければなりません。あなたのデータベース内のarrivalDateのデータ型は何ですか?プラスは、到着日のピッカーであるdateTimePicker1です。これに答えると、おそらく私は助けることができるかもしれません。 – devTimmy

+1

パラメータ化されたコマンド、SQLインジェクションおよびストアドプロシージャ/ EntityFrameworkを検索する –

+0

@ user3499361タイプはカスタム(dd.MM.yyyy) –

答えて

2

、あなたの悩みのほとんどが消えます

は、ここに私のコードです。

ただし、連結するには、データ型とその有効な文字列式を尊重する必要があります。テキスト値は引用符で囲まれ、数字ではなく、日付はである必要があります。アクセスSQLは、オクトソープスにラップする必要があります。したがって:

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

残念ながら、まだ動作していません... :( –

+0

あなたのマイルのコードを歩かなければなりません。上記は単なる目を引くエラーです。 – Gustav

+0

確かに、私は間違いを見つけるだろう:) –