Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to write code for dress rent shop and prevent booking in that booked date and the day after

What I have tried:

try
           {

               OleDbConnection con = new OleDbConnection();
               con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=marriage_dresses.accdb;";
               con.Open();
               OleDbCommand cmd = new OleDbCommand();
               string selectdata = "select dress_name from booking where take_date between @date1 AND @date2 AND dress_name=@name";
               cmd = new OleDbCommand(selectdata,con);
               cmd.Parameters.AddWithValue("@date1",dateTimePicker1.Value.Date);
               cmd.Parameters.AddWithValue("@date2",dateTimePicker2.Value.Date);
               cmd.Parameters.AddWithValue("@name",comboBox1.Text);
               OleDbDataAdapter da = new OleDbDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);
               con.Close();
               if (dt.Rows.Count != 0)
               {
                   MessageBox.Show("cannot book at this date");

               }
               else
               {
                   try
                   {

                       OleDbConnection con2 = new OleDbConnection();
                       con2.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=marriage_dresses.accdb;";
                       con2.Open();
                       OleDbCommand cmd2 = new OleDbCommand();
                       string insertdata = "insert into booking(dress_name,person_name,take_date,return_date,total_price,payed,rest,entry_name) values (@val1,@val2,@val3,@val4,@val5,@val6,@val7,@val8)";
                       cmd2 = new OleDbCommand(insertdata, con2);
                       cmd2.Parameters.AddWithValue("@val1", comboBox1.Text);
                       cmd2.Parameters.AddWithValue("@val2", textEdit1.Text);
                       cmd2.Parameters.AddWithValue("@val3", dateTimePicker1.Value);
                       cmd2.Parameters.AddWithValue("@val4", dateTimePicker2.Value);
                       cmd2.Parameters.AddWithValue("@val5", textEdit2.Text);
                       cmd2.Parameters.AddWithValue("@val6", textEdit3.Text);
                       double c = Convert.ToDouble(textEdit2.Text) - Convert.ToDouble(textEdit3.Text);
                       cmd2.Parameters.AddWithValue("@val7", c);
                       cmd2.Parameters.AddWithValue("@val8", "");
                       cmd2.ExecuteNonQuery();
                       MessageBox.Show("booking succesful");
                       string query = "select * from booking";
                       OleDbCommand cmd3 = new OleDbCommand(query, con2);
                       OleDbDataAdapter da2 = new OleDbDataAdapter(cmd3);
                       DataTable dt2 = new DataTable();
                       da2.Fill(dt2);
                       gridControl1.DataSource = dt2;
                       textEdit1.Text = "";
                       textEdit2.Text = "";
                       textEdit3.Text = "";


                       con.Close();

                   }
                   catch (Exception)
                   {
                       MessageBox.Show("error");
                   }
Posted
Updated 19-Feb-18 23:46pm

1 solution

That needs a little work: you don't need to use a DataAdapter, and you don't need to return the dress name - you already know that!
Instead, consider returning a COUNT, and use the ExecuteScalar method on the command to return a number directly.

But ... you also need to think about your conditions rather more carefully: just checking and existing "take date" isn't between the start or end of teh required hire period isn't going to work:
Existing hire: 1st to 10th March.
Required hire: 5th to 15th March.
Will immediately return no rows and you will say that that is ok, despite the dress needing to be in two places at the same time...

I'd suggest that you sit down with a pen and paper, and work out all the possible permutations and see if you can come up with conditions which fit all of them before you rush into code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900