Home > front end >  asp.net string with different format to datetime when save to database
asp.net string with different format to datetime when save to database

Time:02-10

I want to save the string value of textbox to database as datetime and for updating/edit I want to display it again in textbox as string. This is due to the datetime that will be inputted will be copied from outlook and be pasted in textbox to be use easily by the user. I have no idea how to do it. Please help me with this. Thank you

Sample string: Thu 1/27/2022 8:37 AM

.cshtml part

<input id="txtDateTime" maxlenght="25" type="text"  />
......

.js part (saving call cs)

SaveData: function(){
   var datetime = $("#txtDateTime").val();
   
   if(datetime == null || datetime == ""){
      alert("Datetime is required");
      return;
   }
   
   $.ajax({
      url: url,
      type: "POST",
      datatype: "JSON"
      data: {
         ......,
         DateTime: datetime
      }
   }).done(function(data)){
     ...........,
   }
}

.cs part (saving to db)

public int AddData(Model data, out string outMessage)
{
   using (SqlConnection cn = new SqlConnect(Common.DBConnection))
   {
     cn.Open();
     using (SqlCommand cmd = cn.CreateCommand())
     {
       cmd.CommandText = "sp_add_data";
       cmd.CommandType = System.Data.CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@DateTime", data.DateTime);
       .......,

       SqlParameter paramStatus = new SqlParameter();
       .......,

       SqlParameter paramMessage = new SqlParameter();
       .......,

       outMessage = paramMessage.Value.ToString();
       return Convert.ToInt32(paramStatus.Value.ToString());
     }
   }
}

sp_add_data

@DateTime datetime,
.....,
//insert script

Hope you can help me with this thank you.

CodePudding user response:

  •  Tags:  
  • Related