Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:34:14+00:00 2026-05-31T17:34:14+00:00

I am trying to insert today’s date and try increment the date recursively. but

  • 0

I am trying to insert today’s date and try increment the date recursively. but I am getting conversion error message.

private void InsertTimesheetWeek(string timeSheetID)
{
    int row = GViewTimeSheet.Rows.Count;//get the row count
    int counter = 0;

    string[] txtDate = new string[row];// date column

    foreach (GridViewRow gRow in GViewTimeSheet.Rows)
    {

        txtDate[counter] = "GetDATE()+"+counter;

        counter++;
    }

    //Intializing sql statement
    string fields = "(TimeSheetID, Date)";
    string parm = "(@TimeSheetID, @Date)";
    string sqlStatement = "insert into TimeSheetWeeks" + fields + "Values" + parm;

    SqlCommand comm = new SqlCommand();
    comm.CommandText = sqlStatement;//assing sql statement as command
    SqlConnection connection = DataAccess.getConnection();
    comm.Connection = connection;

    try
    {
        connection.Open();
        for (int i = 0; i < row; i++)
        {
            comm.Parameters.AddWithValue("@TimeSheetID", timeSheetID);
            comm.Parameters.AddWithValue("@Date", txtDate[i]);

            comm.ExecuteNonQuery();
            comm.Parameters.Clear();
        }

    }
    catch (Exception ex)
    {
        Utilities.LogError(ex);
        throw ex;
    }
    finally
    {
        if (connection.State == ConnectionState.Open)// if the connection opened then 
        {
            connection.Close();//just close the connection in any way
        }
    }
}

Why it causes an error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:34:15+00:00Added an answer on May 31, 2026 at 5:34 pm

    The error is caused by passing “GetDATE()+1”, “GetDATE()+2”, etc. as a parameter and SQL can’t convert this to a date.

    Do the date calculations in code before sending to SQL:

    txtDate[counter] = DateTime.Now.AddDays(counter);
    

    The approach you’ve taken would work if you were building the SQL statement as a literal for each row.
    ex:*

    for (int i = 0; i < row; i++)
    {
        string fields = "(TimeSheetID, Date)";
        string parm = String.Format"({0}, GetDATE() + {1})", timeSheetID, i);
        string sqlStatement = "insert into TimeSheetWeeks" + fields + "Values" + parm;
    
        // ....
    

    *N.B. Don’t use the above code – I’ve provided it as an example only. Always use SQL parameters if possible. Parameters are type-safe and mitigate the risk of SQL injection. How To: Protect From SQL Injection in ASP.NET

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to insert XML into XML Column.. getting following error: . Msg 6819,
I am trying to insert to date,time fields using a php script but I
While trying to insert data to my SQl db i get the following error
I am trying to insert a record into a table using Linq but get
I am trying to insert a string in MySQL table. But the following is
I'm just getting my head around insert statements today after getting sick of cheating
Im trying to insert data to my database, and it gives me an error
I've been trying insert a MapView into an ActionBar Tab , but I wasn't
I'm trying to insert objects (NSNumbers) into an NSMutable array, but when I check
I got an error today while trying to do some formatting to existing code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.