Dear sir/Madam,
This is vishnu vardhan,i got some error while update an field of a table which is connected by using oledb connection.
Table name is totallogins, contents are id(autonumber),token_no(text),user_id(number),from_date(date/time),to_date(date/time). my require output is at the time of login id,token_no,user_id,from_date are filled and to_date column is fill when exit button clicks.In the attached query i got error msg as Data type mismatch.Could you please check and suggest us how to resolve my problem.
THANKS IN ADVANCE.
Loading
Amit ChoudharyPosted Feb 16, 2011, 6:07 AM
Hope it make sense.
vishnuvardhan diyyalaPosted Feb 15, 2011, 2:49 AM
By your example i got my mystake. But im unable to resolve that. y because i m a beginer(student) in .net. so unable to catch u r solution.
SqlParameter param1 = new SqlParameter();
param.ParameterName = "@NewDate";
and
SqlParameter param2 = new SqlParameter();
param.ParameterName = "@OldDate";
what is param1,parameter name in second line of above two sentences given by u.My database table name is totallogins.
on that table
id--------Auto no
tokenno----text
userid-----number
fromdate----date/time and
todate----date time.
Could you pleae explain me about parameter method
Thanks in advance.
Amit ChoudharyPosted Feb 14, 2011, 6:47 AM
Suthish NairPosted Feb 14, 2011, 3:20 AM
2. Due to that Datatype mismatch happening.
3. SQLParameter will take care of all the formats between SQL DB table and your commands.
4. Best way if your DB table datatype column is DateTime then,
SqlParameter param = cmd.Parameters.Add("@datetime", System.Data.SqlDbType.DateTime).Value = DateTime.Now;5. Do not convert to string value, can use Convert.ToDateTime() method.
Amit ChoudharyPosted Feb 14, 2011, 1:05 AM
Suthish is right by using the Parametrized query your problem will be solve.
Here is the sample:
string st6 = "update totallogins set To_date =@NewDate
where from_date=@OldDate;
cmd.CommandText = st6;
SqlParameter param1 = new SqlParameter();
param.ParameterName = "@NewDate";
param.Value = DateTime.Now;
SqlParameter param2 = new SqlParameter();
param.ParameterName = "@OldDate";
param.Value = b;
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Now Suthish i have question here and need your input.. why DateTime Could'nt save directly using the command object ?? Why do we need to use parameter to save the Date using DateTime class.
Thanks.
Suthish NairPosted Feb 12, 2011, 1:48 PM
This will solve your problem, give same datatype during creating parameters.
Parameterized Query and SQL Injection Attacks