Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello

I need some help.
I want to update column between two dates.
Here is my code.

C#
public string UpdateValid(DateTime startDate, DateTime endDate)
 {
 OleDbConnection conn = new OleDbConnection("my own database setup");
 string status = null;
 
 conn.Open();
 OleDbCommand cmd = conn.CreateCommand();
 
 cmd.CommandText = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN startDate AND @endDate AND DriverIDFK = @DriverIDFK";
 
 cmd.Parameters.AddWithValue("@startDate", startDate);
 cmd.Parameters.AddWithValue("@endDate", endDate);
 cmd.Parameters.AddWithValue("@DriverIDFK", UserMapper.driverNumber);
 
 OleDbTransaction sqltrans = cmd.Connection.BeginTransaction();
 
 try
 { 
 OleDbCommand updateCmd = cmd.Connection.CreateCommand();
 updateCmd.CommandText = cmd.CommandText;
 updateCmd.Transaction = sqltrans;
 
 
 updateCmd.Parameters.Add("@valid", OleDbType.Boolean);
 updateCmd.Parameters["@valid"].Value = false;
 
 updateCmd.ExecuteNonQuery(); 
 sqltrans.Commit();
 
 
 status = "Ok";
 }
 catch (OleDbException odbe)
 {
 sqltrans.Rollback();
 System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
 }
 catch (Exception ex)
 {
 System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
 }
 finally
 {
 conn.Close();
 }
 
 return status;

 }

It says something like that, there is no value is specified for one or more parameters.

Please Help
Posted
Updated 27-Jul-12 2:16am
v2

This is the solution.

C#
public string UpdateValid(DateTime startDate, DateTime endDate)
        {
            OleDbConnection conn = new OleDbConnection("my db path"));
            string status = null;
            
            string updateSql = "UPDATE Watchreceipt SET valid = false WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";

            conn.Open();
            OleDbTransaction sqltrans = conn.BeginTransaction();
            OleDbCommand updateCmd = conn.CreateCommand();
            updateCmd.CommandText = updateSql;
            
            updateCmd.Parameters.AddWithValue("@startDate", startDate);
            updateCmd.Parameters.AddWithValue("@endDate", endDate);
            updateCmd.Parameters.AddWithValue("@DriverIDFK", UserMapper.driverNumber);

            updateCmd.Transaction = sqltrans;

            try
            {
                
                updateCmd.ExecuteNonQuery();
                sqltrans.Commit();
                
                status = "Ok";
            }
            catch (OleDbException odbe)
            {
                sqltrans.Rollback();
                System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return status;

        }


I changed the SQL statement and deleted some code inside the try block.
Thanks
 
Share this answer
 
v2
Add the '@' character to startDate
C#
cmd.CommandText = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN startDate AND @endDate AND DriverIDFK = @DriverIDFK";

Becomes
C#
cmd.CommandText = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";
 
Share this answer
 
Comments
Sego Turan 27-Jul-12 8:35am    
Thanks for the comment.
I have add the character '@' to startDate but it give me the same problem.

Here is my code

public string UpdateValid(DateTime startDate, DateTime endDate)
{
OleDbConnection conn = new OleDbConnection("my own database setup");
string status = null;

conn.Open();
OleDbCommand cmd = conn.CreateCommand();

cmd.CommandText = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";

cmd.Parameters.AddWithValue("@startDate", startDate);
cmd.Parameters.AddWithValue("@endDate", endDate);
cmd.Parameters.AddWithValue("@DriverIDFK", UserMapper.driverNumber);

OleDbTransaction sqltrans = cmd.Connection.BeginTransaction();

try
{
OleDbCommand updateCmd = cmd.Connection.CreateCommand();
updateCmd.CommandText = cmd.CommandText;
updateCmd.Transaction = sqltrans;


updateCmd.Parameters.Add("@valid", OleDbType.Boolean);
updateCmd.Parameters["@valid"].Value = false;

updateCmd.ExecuteNonQuery();
sqltrans.Commit();


status = "Ok";
}
catch (OleDbException odbe)
{
sqltrans.Rollback();
System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
}
finally
{
conn.Close();
}

return status;

}
OriginalGriff 27-Jul-12 9:38am    
Yes. That is what I would expect - you are creating a new command in the try block - use the one outside it instead, as the parameters do not "magically" transfer themselves! :laugh:
(I should have spotted that the first time, but I got caught up with teh more obvious missing '@' character).
You have not mentioned the values for startDate and endDate. Also startDate should be accompanied by @

UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN startDate AND @endDate AND DriverIDFK = @DriverIDFK
 
Share this answer
 
Comments
OriginalGriff 27-Jul-12 9:33am    
Yes he has - see the Parameters.AddWithValue below that statement.
Check this .. You used startdate instead of @startdate


SQL
cmd.CommandText = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";
 
Share this answer
 
v2

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