Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

im creating one application in that application i set sales_count+1 if particular user enter detail then in database sales_count should be 1+1=2 like , this sales_count i set base on TODAY date , now i what i want if Database date and system date match then trigger UPDATE query but if not then throw insert query i written code for UPDATE but i unable to write how can i write sales_count+1 in insert query like if new Date system found then insert query should insert sales_count+1 and current date in sytem

Note: preoblem is i unable to write insert query which is increment sales_count for specific Unique_no with current date , im using MS-ACCESS as database

What I have tried:

C#
this.txtinput.MaxLength = 4;

string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;

string access = "select To_Date from Login";
             cmd = new OleDbCommand(access, con);
              con.Open();
              using (OleDbDataReader read = cmd.ExecuteReader())
              {
                  while (read.Read())
                  {
                      string date2 = read["To_Date"].ToString();
                      if (todaydate == date2)
                      {
                          cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1, [To_Date]=Date() where [Unique_No]=@Unique_No", con);
                          cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
                          con.Open();
                          int n = cmd.ExecuteNonQuery();

                          if (n == 0)
                          {
                              MessageBox.Show("Invalid Unique No.");

                          }
                          else
                          {
                              this.DialogResult = DialogResult.OK;
                          }

                          con.Close();

                       

                      }
                      else
                      {
                           cmd = new OleDbCommand("IF NOT EXISTS(SELECT 1 FROM Login WHERE Unique_No = @Unique_No) INSERT INTO(Sales_count,To_Date)values(?,Date()) ", con);
                          cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
                          con.Open();
                          int n = cmd.ExecuteNonQuery();

                          if (n == 0)
                          {
                              MessageBox.Show("Invalid Unique No.");

                          }
                          else
                          {
                              this.DialogResult = DialogResult.OK;
                          }
                          con.Close();


                      }
                  }
              }
               con.Close();
Posted
Updated 16-Aug-16 10:01am

 
Share this answer
 
Comments
Maciej Los 16-Aug-16 16:00pm    
5ed!
 
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