Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to save a specific time using a datepicker to my database. I'm using nvarchar datatype for time.

What I have tried:

try
          {

              if (sqlCon.State == ConnectionState.Closed)
                  sqlCon.Open();
              if (btnEvSave.Text == "   Save")
              {
                  SqlCommand sqlCmd = new SqlCommand("EventAddorEdit", sqlCon);
                  sqlCmd.CommandType = CommandType.StoredProcedure;
                  sqlCmd.Parameters.AddWithValue("@mode", "Add");
                  sqlCmd.Parameters.AddWithValue("@EventID", 0);
                  sqlCmd.Parameters.AddWithValue("@EventTime", txtEvTime.Text.Trim());
                  sqlCmd.Parameters.AddWithValue("@EventVenue", txtEvVenue.Text.Trim());

                  sqlCmd.ExecuteNonQuery();
                  MessageBox.Show("Saved Successfully");


                  sqlCon.Close();

              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Must Fill all the Details");
          }
          finally
          {
               this.Close();
          }
Posted
Updated 16-Jan-19 20:13pm
Comments
F-ES Sitecore 17-Jan-19 4:47am    
Don't save time in varchar fields, always store data in field types appropriate to the data you are storing. Storing dates, times etc as text is only got to cause you problems down the line.

1 solution

Start with the debugger: put a breakpoint on the line
sqlCmd.ExecuteNonQuery();
and run your code in the debugger. When it stops at the breakpoint, look at the content of the variables you have just set as parameters, paying attention to <pre>txtEvTime.Text</pre>
What does it contain? Is it a valid time in text format? If not, why not?
If it is, then step over the ExecuteNonQuery instruction and look at what happens. Does it go to the catch block? If so, look at the exception in ex and find out why it went there - there is a lot =of info in there that can help you.

Does it even reach the breakpoint? Me, I'd be very suspicious of this line:
if (btnEvSave.Text == "   Save")
Which looks very dodgy...

We can't do any of this for you - we don't have any access to your code, or data, or SP, or ... so you will have to gather information for yourself!
 
Share this answer
 
v2
Comments
Maciej Los 17-Jan-19 4:02am    
Well... if (btnEvSave.Text.Trim() == "Save") looks better?
:laugh:
Cheers
Maciej
OriginalGriff 17-Jan-19 4:13am    
Eeeewwwww ... not a lot, no ... :laugh:
Might work better though, sadly.

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