1

I am trying to insert a new row into a table in my SQL database through a webform.

The problem is that I need to place the DateTime value in the Description of when the assignment of deliveryman was done.

But because the value is a string and the requirement is to display something like this "Received parcel by StationMgrSG on 19 July 2016 10:34am" but my codes is displaying something like this "Received parcel by StationMgrSG on @TimeNow"

How do i change the value so that it displays the date and time?

This is my code:

public int updateDeliveryHistory() { string strConn = ConfigurationManager.ConnectionStrings["NPCSConnectionString"].ToString(); SqlConnection conn = new SqlConnection(strConn); SqlCommand cmad = new SqlCommand("INSERT INTO DELIVERYHISTORY(ParcelID, Description) VALUES (@ParcelId,'Received parcel by StationMgrSG on @TimeNow')",conn); cmad.Parameters.AddWithValue("@ParcelId", parcelID); cmad.Parameters.AddWithValue("@TimeNow", DHCreateTime); conn.Open(); cmad.ExecuteNonQuery(); conn.Close(); return 0; } 

3 Answers 3

2

To achieve this you have to modify the code like this:

SqlCommand cmad = new SqlCommand("INSERT INTO DELIVERYHISTORY(ParcelID, Description) VALUES (@ParcelId, @TimeNow)",conn); cmad.Parameters.AddWithValue("@ParcelId", parcelID); cmad.Parameters.AddWithValue("@TimeNow","Received parcel by StationMgrSG on " + DHCreateTime.ToString()); 

Additional note, You can specify the DateFormat inside the .ToString() method if needed. then the @TimeNow will looks like the following:

string formatString="dd-MMMM-yyyy HH:mm" cmad.Parameters.AddWithValue("@TimeNow","Received parcel by StationMgrSG on " + DHCreateTime.ToString(formatString)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Even better if you have a DateTime column in the DB for this data.
0

You just need to change this line

SqlCommand cmad = new SqlCommand("INSERT INTO DELIVERYHISTORY(ParcelID, Description) VALUES (@ParcelId,'Received parcel by StationMgrSG on @TimeNow')",conn); 

to

SqlCommand cmad = new SqlCommand("INSERT INTO DELIVERYHISTORY(ParcelID, Description) VALUES (@ParcelId,'Received parcel by StationMgrSG on ' + @TimeNow)",conn); 

The problem is you're passing '@TimeNow' as part of a string literal instead of a parameter.

Comments

0

Do it like this:

 string getDate = DateTime.Now.ToString("MMMM dd yyyy hh:mmtt"); SqlCommand cmad = new SqlCommand("INSERT INTO DELIVERYHISTORY(ParcelID, Description) VALUES (@ParcelId,'Received parcel by StationMgrSG on ' + getDate)",conn); cmad.Parameters.AddWithValue("@ParcelId", parcelID); cmad.Parameters.AddWithValue("@TimeNow", getDate); 

Hope it helps.

3 Comments

That is Why, I told you to format your code, instead for down voting the post. This will help you to post good answers
dont know yet how to format my code,, im just answering,, dont care if they down voted me.. maybe someday i know how to format it here.
Need to share my thought and give to other who needed it.. thanks @un-lucky

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.