Hi

I have an insert statement in my VB code that updates about 20 fields in sql. I pass through todays date and a set of values.

This occurs on a button click event. My problem is if the button is clicked more than once the data is duplicated in the sql table, ie there is two rows of data for the same date. Is there anyway for it to overwrite the current data if the date is the same but put a new row in if it is a different date?

Below is an example of the code

Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
            conn.Open() 
            Using selectCommand As SqlCommand = conn.CreateCommand 
                With selectCommand 
                    .CommandType = CommandType.Text 
                    .CommandText = "INSERT INTO dbo.MorningReport(--Fields--) " + "VALUES--@fields--)" 
                    .Parameters.Add("@field1", SqlDbType.DateTime).Value = Date.Now 
                    .Parameters.Add("@field2", SqlDbType.Int).Value = getavgNT()

You can do something like this (assuming MSSQL):

UPDATE dbo.MorningReport 
    SET (...) 
    WHERE field1 = ''

IF @@ROWCOUNT = 0
    INSERT INTO dbo.MorningReport 
    VALUES (...)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.