Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a windows application using C# .Net, I have DataGridView, a DateTimePicker and a textbox named as "days left" and a save button in that windows from. I have a database table named as "vehicle" and it includes two columns named as "expiry date" and "DaysLeft", I want to store data from windows form to the database table and it should store the DaysLeft either -ve or +ve value according to current date and I also want to update DaysLeft column automatically in DataGridView according to current date.

What I have tried:

I have tried the following code

private void dateTimePickerKhaas_ValueChanged(object sender, EventArgs e)
        {
            DateTime from = dateTimePickerKhaas.Value;
            DateTime to = DateTime.Now;
            TimeSpan Tspan = from - to;
            double days = Tspan.TotalDays;
            txtKDaysLeft.Text = days.ToString("0");
        }


private void btnKSave_Click(object sender, EventArgs e)
        {
                con.Open();
                String query = "INSERT INTO table_khaas ( ExpiryDate, DaysLeft) VALUES( '" + dateTimePickerKhaas.Value.ToString("MM/dd/yyyy") + "'  ,'" +txtKDaysLeft.Text + "')";
                SqlDataAdapter sda = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridViewKhaas.DataSource = dt;
                
                con.Close();
                grd_fillKhaas();


                MessageBox.Show("Inserted Successfully");
                dateTimePickerKhaas.Value = DateTime.Now;
                txtKDaysLeft.Text = "";
                        
        }

public void grd_fillKhaas()
        {
            //con.Open();
            String query = "SELECT * FROM table_khaas";
            SqlDataAdapter sda = new SqlDataAdapter(query, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridViewKhaas.DataSource = dt;
            //con.Close();
        }
Posted
Updated 12-Jun-17 11:35am
Comments

Remove the DaysLeft column, and calculate it when you query the table:
C#
public void grd_fillKhaas()
{
    using (var connection = new SqlConnection("..."))
    {
        var sda = new SqlDataAdapter("SELECT ExpiryDate, DateDiff(dd, GetDate(), ExpiryDate) As DaysLeft FROM table_khaas", connection);
        var dt = new DataTable();
        sda.Fill(dt);
        dataGridViewKhaas.DataSource = dt;
    }
}

C#
private void btnKSave_Click(object sender, EventArgs e)
{
    using (var connection = new SqlConnection("..."))
    using (var command = new SqlCommand("INSERT INTO table_khaas ( ExpiryDate ) VALUES ( @ExpiryDate )", connection))
    {
        command.Parameters.AddWithValue("@ExpiryDate", dateTimePickerKhaas.Value);
        
        connection.Open();
        command.ExecuteNonQuery();
    }
    
    grd_fillKhaas();
    

    MessageBox.Show("Inserted Successfully");
    dateTimePickerKhaas.Value = DateTime.Now;
    txtKDaysLeft.Text = "";
}

DATEDIFF (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 
v2
Quote:
I have a database table named as "vehicle" and it includes two columns named as "expiry date" and "DaysLeft",

Advice: Never put volatile data in database, it is a design flow.
Volatile data is data that change very often, like every day. Because in order to use that column "DaysLeft", you always have to update it unless you know that update have already been done. Believe me, it is a major pain in real situation.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, anf this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
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