Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys, Need some expert advise and help. I am a NooB, trying to create a small Windows Form Application that would insert some data into MY SQL Database. On this form, I am using 3 Combo Box, 1 Text Box, 1 Datetime picker and one button for the save action. The Application works fine, the way it is intended to, but the only problem is time does not get updated after every record is inserted to the table. Time remains Constant. Below is the Code, kindly help.

Regards
Jagadish K

C#
private void Save_Click(object sender, EventArgs e)
{           
      string sDispatcher = DispatcherName.Text;
      string sDPSNum = DispatchNumber.Text;
      string sDPSType = DPSType.Text;
      DateTime dtDT = dateTimePickerDT.Value;
      string sQueue = QG.Text;

      string query = "INSERT INTO dps_data(DispatcherName, DispatchNumber, DPSType, DateTime, Queue)" +"VALUES( @Dispatcher, @DPSNum, @DPSType, @DT, @Queue)";

      SqlConnection conn = new SqlConnection(connection);
      SqlCommand cmd = new SqlCommand(query, conn);
      conn.Open();

      cmd.Parameters.Add(new SqlParameter("@Dispatcher", sDispatcher));
      cmd.Parameters.Add(new SqlParameter("@DPSNum", sDPSNum));
      cmd.Parameters.Add(new SqlParameter("@DPSType", sDPSType));
      cmd.Parameters.Add(new SqlParameter("@DT", dtDT));
      cmd.Parameters.Add(new SqlParameter("@Queue", sQueue));

      int result = cmd.ExecuteNonQuery();
      if (result > 0)
      {
         MessageBox.Show("Data Added Successfully");
      }
      else
      {
         MessageBox.Show("Error in Adding Data");
      }          

      conn.Close();

      DispatchNumber.Text = String.Empty;
      DPSType.Text = String.Empty;
      QG.Text = String.Empty;
   }
}

[Edit - Marcus Kramer Added pre tags and removed irrelevant code]
Posted
Updated 31-Jan-13 18:03pm
v2

1 solution

It won't insert because your dtDT variable is not included in your insert statement.
You need to add it to the string query = "INSERT INTO dps_data(DispatcherName, DispatchNumber, DPSType, DateTime, Queue)" +"VALUES( @Dispatcher, @DPSNum, @DPSType, @DT, @Queue)"; statement as another parameter.
 
Share this answer
 
Comments
JagzKrish 1-Feb-13 0:29am    
@Marcus Kramer - Thank you for your assistance, but I really don't understand what you are exactly talking about. Ok, let me make it very simple. When I build the Application, it launches fine and I have all the fields to be filled, I fill in all details and click Save, Data is inserted to the table and as per the button action, connection is closed and all fields are reset, ready for next entry. when the next entry is done, the time remains the same as first entry, no matter how many entries you do, unless you close and relaunch the application. Kindly excuse me, I am no big programmer, hence explaining in layman terms.

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