1.11M Members

Updating an sql table

 
0
 

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()
 
0
 

You can do something like this (assuming MSSQL):

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

IF @@ROWCOUNT = 0
    INSERT INTO dbo.MorningReport 
    VALUES (...)
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: