Click here to Skip to main content
15,902,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
aoa... in my app Pay roll module i want to add in sql server.... with the help of months data cab be added...
actually i want to add data in sql server for the employees... i want to avoid last three entries for the same month... which query can be made to insert data into table and on the same month same employee can not be add?

as my sample data is given below

Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  April	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  May	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  June	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  July	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	 August	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	 September	2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014

and my c# code is:

C#
private void button3_Click(object sender, EventArgs e)
       {
           SqlCommand cmd = sqlconn.CreateCommand();
           cmd.CommandText = "insert into payroll values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox8.Text + "','" + textBox7.Text + "','" + textBox6.Text + "','" + textBox10.Text + "','" + textBox9.Text + "','" + textBox12.Text + "','"+comboBox1.Text+"','"+comboBox2.Text+"')";
           try
           {
               cmd.ExecuteNonQuery();
           }
           catch (SqlException err)
           {
               MessageBox.Show(err.Message);
           }
           MessageBox.Show("Record Entered Successfull");


       }
Posted
Updated 16-Aug-14 22:35pm
v3
Comments
OriginalGriff 17-Aug-14 4:27am    
This is not a good question - we cannot work out from that little what you are trying to do.
I'm not sure what help you need, or what output you are trying to get.
Perhaps a sample of the output you want would help us understand?
Use the "Improve question" widget to edit your question and provide better information.
Member 10690757 17-Aug-14 4:30am    
actually i want to add data in sql server for the employees... i want to avoid last three entries for the same month... which query can be made to insert data into table and on the same month same employee can not be add?
Ashi0891 17-Aug-14 4:36am    
But why do you have repetition of data? Do you mean you Dnt want to insert last two entries from this example? In othe words you want to insert only one row with October month from above data?

1 solution

"actually i want to add data in sql server for the employees... i want to avoid last three entries for the same month... which query can be made to insert data into table and on the same month same employee can not be add?"

To do that, you need to check first:
SQL
SELECT COUNT(*) FROM MyTable
GROUP BY UserName, Year, Month 
WHERE Year = @CurrentYear AND Month = @CurrentMonth
(Obviously, you'll have to provide the year and month values
Then if the number of less than three you can insert

I'd do this in a stored procedure (and I'd store the month and year as a DateTime, not separate fields)
 
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