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 8503727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:50:10+00:00 2026-06-11T01:50:10+00:00

I am trying to insert multiple records into SQL database. SQL table contains smalldatetime

  • 0

I am trying to insert multiple records into SQL database. SQL table contains smalldatetime column. Every record is for one hour period, so when you want to insert timespan for, eg. 5 hours, you fill in 5 records into database.

Code looks something like this:

int flag = 0;
try{
   if (String.Empty.Equals(tboxFromTime.Text)){
      MessageBox.Show("Error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
      return;
   }
   DateTime n, m;
   if (!DateTime.TryParse(tboxFromTime.Text, out n)){
      MessageBox.Show("Error!");
      return;
   }
   FormMain.connection.Open();
   command = new SqlCommand();
   command.Connection = FormMain.connection;
   string classParam, personParam, startParam;
   classParam = "class";
   personParam = "person";
   startParam = "start";

   command.CommandText = "INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start)";
   command.Parameters.AddWithValue("@class", int.Parse(cboxRoomID.Text.Substring(4)));
   command.Parameters.AddWithValue("@person", int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));
   command.Parameters.AddWithValue("@start", DateTime.Parse(cboxWeekDate.Text + " " + n.ToShortTimeString() + ":00"));
   if (!String.Empty.Equals(tboxToTime.Text)){
      if (!DateTime.TryParse(tboxToTime.Text, out m)){
         MessageBox.Show("Error!");
         return;
      }
      else if(DateTime.Compare(m.AddHours(-1), n) > 0){
             TimeSpan timeDiff = m - n;
             int intDiff = timeDiff.Hours;
             for (int i = 1; i < intDiff; i++){
                 classParam = "class" + i.ToString();
                 personParam = "person" + i.ToString();
                 startParam = "start" + i.ToString();
                 command.CommandText += ", " + String.Format("(@{0}, @{1}, @{2})", classParam, personParam, startParam);

                 command.Parameters.AddWithValue(String.Format("@{0}", classParam), int.Parse(cboxRoomID.Text.Substring(4)));
                 command.Parameters.AddWithValue(String.Format("@{0}", personParam), int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));
                 command.Parameters.AddWithValue(String.Format("@{0}", startParam), DateTime.Parse(cboxWeekDate.Text + " " + n.AddHours(i).ToShortTimeString() + ":00"));
             }
         }
     }

   flag = command.ExecuteNonQuery();
   if (flag == 1) { MessageBox.Show("Uspelo!"); }
   }

When executing for one hour period it all goes well, but for any timespan it cracks. Command Text string for a few hours is like this:

INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start), (@class1, @person1, @start1), (@class2, @person2, @start2), (@class3, @person3, @start3)

but, again I get this type of error message:

Incorrect syntax near ‘,’.

Can anyone figure out what is the problem?

  • 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-06-11T01:50:11+00:00Added an answer on June 11, 2026 at 1:50 am

    The created sql syntax is not correct. You have to insert each value for its own.

    INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start)
    INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class1, @person1, @start1)
    INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class2, @person2, @start2)
    INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class3, @person3, @start3)
    

    Or

    INSERT INTO Reservations
    SELECT @class, @person, @start
    UNION
    SELECT @class1, @person1, @start1
    UNION
    SELECT @class2, @person2, @start2
    UNION
    SELECT @class3, @person3, @start3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert multiple records into a table where each record has
I'm trying to insert multiple rows into MySql with only one INSERT INTO statement
i am trying to insert some info into an sql server table but instead
I am trying to copy multiple records using one query using insert select from.
I got the following issue while trying to insert multiple entries into a MySQL
Trying to insert 315K Gif files into an Oracle 10g database. Everytime I get
Am trying to insert XML into XML Column.. getting following error: . Msg 6819,
I have a Microsoft SQL database, where i am trying to insert some data.
How with SQL server bulk insert can I insert into multiple tables when there
I am trying to insert multiple rows in a MySQL table from PHP arrays.

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.