I am facing problem updating my sql table. I am using materializecss and because of that i had to use
<div class="input-field col l6 s12 m6">
<input type="date" id="DOB" class="datepicker"/>
<label for="DOBLBL">DATE OF BIRTH</label>
</div>
jquery for it: $('.datepicker').pickadate({ selectMonths: true, // Creates a dropdown to control month selectYears: 15, // Creates a dropdown of 15 years to control year format: 'mm/dd/yyyy' });
C#
string dob = String.Format("{0}", Request.Form["DOB"]);
string jd = String.Format("{0}", Request.Form["JD"]);
String userName = Convert.ToString(Session["username"]);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand com = new MySqlCommand("userDetails", con);
com.CommandType = CommandType.StoredProcedure;
MySqlParameter p1 = new MySqlParameter("nickName", nickNameTB.Text);
MySqlParameter p2 = new MySqlParameter("dob", dob);
MySqlParameter p3 = new MySqlParameter("joinDt", jd);
MySqlParameter p4 = new MySqlParameter("memType", memTtype);
MySqlParameter p5 = new MySqlParameter("usrname", userName);
com.Parameters.Add(p1);
com.Parameters.Add(p2);
com.Parameters.Add(p3);
com.Parameters.Add(p4);
com.Parameters.Add(p5);
con.Open();
com.ExecuteNonQuery();
Routine which I created
CREATE DEFINER=`root`@`localhost` PROCEDURE `userDetails`(IN `nickName` VARCHAR(15), IN `dob` DATE, IN `joinDt` DATE, IN `memType` INT(3), IN `usrname` VARCHAR(15))
NO SQL
update ozi_members
SET memberNickName = nickName,
memberDob = dob,
memberJoinDate = joinDt,
memberType = memType
WHERE memberUsername = usrname
I tried procedure and it works fine. So I think the problem is with date format which I am taking from datepicker. please help.
If your fields are of type Date then you should pass a parameter of type Date.
If you pass a string then it is the database engine that needs to convert it to the date required by the field. A date is not a string and there is always a conversion at work here. This conversion could work or not, but why let this happen? Just create parameters specifying the type and pass a parameter value of the correct type
DateTime dob = Convert.ToDateTime(Request.Form["DOB"]);
DateTime jd = Convert.ToDateTime(Request.Form["JD"]);
....
using(MySqlConnection con = new MySqlConnection(constr))
using(MySqlCommand com = new MySqlCommand("userDetails", con))
{
com.CommandType = CommandType.StoredProcedure;
MySqlParameter p1 = new MySqlParameter("nickName", MySqlDbType.VarChar).Value = nickNameTB.Text;
MySqlParameter p2 = new MySqlParameter("dob", MySqlDbType.Date).Value = dob;
MySqlParameter p3 = new MySqlParameter("joinDt", MySqlDbType.Date).Value = jd;
....
con.Open();
com.ExecuteNonQuery();
}
Note also the a connection and a command should be enclosed in a using statement to have a proper disposing of these objects and avoid resource leaks.
(The initial Convert.ToDateTime assumes that your Request.Form variables are in a correct format for a Date probably validated at client side. If not then you should use DateTime.TryParse and give an appropriate message if the input cannot be converted to a DateTime variable)
In your stored procedure, DO NOT enclose the parameters in single quotes since it will not be treated like a parameter but as a value. Try this:
CREATE DEFINER=`root`@`localhost` PROCEDURE `userDetails`(IN @nickName VARCHAR(15),
IN @dob DATE, IN @joinDt DATE, IN @memType INT(3), IN @usrname VARCHAR(15))
# rest of code
Then from the application side make these changes to C# code:
MySqlParameter p1 = new MySqlParameter("@nickName", nickNameTB.Text);
// Other parameters
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.