I am trying to set an condition when setting up parameter for a database field so that if the receiving value is null then it should set it to current date
cmd.Parameters.AddWithValue("#DatePlannedEnd", SqlDbType.NVarChar).Value = pm.DatePlannedEnd != null ? pm.DatePlannedEnd : DateTime.Now.Date;
I get error:
Type of conditional expression can not be determined because there is no implicit conversion between string and System.Date.Time
DatePlannedEnd is a Date field in the database. Any idea what will be the right way to achieve what I am trying to do?
You are mixing up Add and AddWithValue. The whole point of AddWithValue is that you provide the value, not the type, of the parameter. The type is inferred from the value. If you're going to set the Value property explicitly then call Add, not AddWithValue.
Apart from that, why are you specifying the type as NVarChar and then assigning a DateTime as the value? If the data type in the database is date then the SqlDbType should be Date, not NVarChar. If you use AddWithValue though, that will be inferred.
You can try checking the value first before submitting against the database :)
if (pm.DatePlannedEnd == null) {
pm.DatePlannedEnd = DateTime.Now.Date.ToString();
}
You can't return two different types from a ternary expression.
You're adding DateTime.Now as a date, but your parameter type is a string. Convert it to a string first.
Also, use one method or the other of adding parameters, not a combination of both.
var datePlannedEnd = pm.DatePlannedEnd ?? DateTime.Now.Date.ToString();
cmd.Parameters.Add("#DatePlannedEnd", SqlDbType.NVarChar);
cmd.Parameters["#DatePlannedEnd"].Value = datePlannedEnd;
Or:
cmd.Parameters.AddWithValue("#DatePlannedEnd", datePlannedEnd);
And if you have any control over the database, think about altering that column type to be a date instead of a string. That way, you won't have to parse a string back into a valid DateTime whenever you get the value back out of the database.
Related
I'm getting values from database to a aspx page. while getting values of one column, the column is throwing a value like...
null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390,
2391,2964,2965,2392,2393,2394,2395,2397
Now it throws a exception
Conversion failed when converting the varchar value 'null' to data
type int.
But I want all the other values of the column except the null...
Is there any way to convert this 'null' to '0', but still all the other values should exist.
Make your data type Nullable<int> instead of int to allow the converted int nullable values, or on the sql select statement you can do that: ISNULL(yourcolumn, 0) to convert null to 0.
I would just declare my type as int? variable; which is a nullable int type.
That would avoid you getting the exception, and you could easily convert your value to 0 if needed
if (value == null)
value = 0;
Since you say you want all of the values except the null value in your results, you could filter it in your select statement with a where clause.
SELECT * FROM COLUMN WHERE COLUMN_VALUE IS NOT NULL
Or, if you're using Linq to EF:
from x in yourObjectContext.yourColumns where x != null
I think you are trying to change from NULL to 0 in the database.
Then you should try this-
SELECT CASE WHEN columnName IS NULL THEN '0' ELSE columnName END FROM tableName
When I try to insert a string value using mdmysql.Parameters.AddWithValue it generates an input format string exception. Following is the code which produces the error:
cmdmysql.Parameters.AddWithValue("#p_mode", MySqlDbType.VarChar).Value = "ccc";
I tried with varchar, string, text but nothing is working. Also if I put null in place of "ccc" then the record gets inserted into the table. The variable type for p_mode in table is varchar. What is the reason for this exception?
The AddWithValue function takes only two inputs, 1. The parametername and 2. the value
cmdmysql.Parameters.AddWithValue("#p_mode", "ccc");
If you want to use the definition of types you must use the normal Add function:
cmdmysql.Parameters.Add("#p_mode", MySqlDbType.VarChar).Value = "ccc";
I would guess that the exception comes from your second input where you parse an MySqlDbType to the function and not the acutal value you want the returned mysqlparameter to have.
Try this
cmdmysql.Parameters.AddWithValue("#p_mode","ccc");
Since it's friday my brain must have malfunctioned.
I'm trying to update a column of the type DateTime with the good ol' OleDb, the update always run (changing other column values on the same row) but for some reason the DateTime column refuses to be set to null.
Ofc the column allows null.
I'm using the OleDbParameter constructor to add a new parameter to my command might it be the parameter that refuses to run if the value is null?
I've tried SqlDateTime.Null, DbNull.Value and null as the parameter but nothing sets the value to null, it leaves it untouched or sets it to our beloved 1900-00-01.
any tips and tricks are appreciated
you want to use DBNull.Value
just like the prior comments you could do something like DateTime? myDate = null;
The issue is that unless you take specific action, a DateTime is going to be represented as DateTime.MinValue. So if you want a null in the DB, you will need to test the value and, if MinValue, send DBNull instead.
We actually use a method to get the value as date time or null if date is minvalue:
function object DateToDB(DateTime testDate)
{
if (testDate == DateTime.MinValue)
{
return DBNull.Value;
} else
{
return testDate;
}
}
This method is then used to assign the parameter value.
I'm trying to insert a row in a PostgreSQL table with a date column. On the UI, I got a DateTimePicker where the user selectes the proper date. So far I got this:
On the UI:
objPresupuesto.date = this.dtpFechaPres.Value.ToString("yyyy-MM-dd");
On the method who inserts the row:
NpgsqlCommand query = new NpgsqlCommand("insert into presupuesto(presupuesto, codigo, descripcion, fecha, cliente, proyecto, total) values(nextval('presupuesto_presupuesto_seq'), #codigo, #descripcion, #fecha, #cliente, #proyecto, #total);Select lastval();", conn);
...
query.Parameters.Add(new NpgsqlParameter("fecha", NpgsqlDbType.Date, 0, "fecha"));
...
query.Parameters[2].Value = obj.date.toString;//without the toString it also fails
It throws this exception:
Specified cast is not valid.
The obj.date value is 2011-04-29. tryied putting single quotes around, but It also fails.
The database column type is date.
Anyone has done this before? Any ideas?
I checked this link searching for and aswer but it doesn't helped.
Thanks
The first thing I noticed is that when you are adding your parameter it is missing the "#" from the beginning where your command references the value with the "#".
Are you sure that the Parameters[2] is your "fecha" parameter?
try use:
npgsqlCommand.Parameters.Add("fecha", NpgsqlDbType.Date).Value = DateTime.Now;
For more readable code.
Type of obj.date is DateTime struct?
Add to connection string parameter Convert Infinity DateTime
For more info check: http://www.npgsql.org/doc/connection-string-parameters.html
query.Parameters[2].Value = CDate(obj.date)
I'm getting ora-01475 whenever I try to insert a null value in a column (of type DateTime) after some records have already been inserted that have real date values.
I'm using the OracleParameter constructor that takes the name and the value as an object (I assume the data type is then implied from the datatype of the object), but since sometimes the value of my parameter is null, it's being set as a String, therefore throwing this error.
I don't want to use the constructor that takes the datatype explicitly because I use reflection heavily to build the OracleCommand object and its parameters.
How can I reparse the cursor (as the error suggests) if I find this situation?
Has anyone else run into this and has a solution?
Have you tried to use nullable types?
DateTime? myDate;
//Code to set myDate value...
string sql = "[your SQL]"
using (OracleCommand command = new SqlCommand(sql, cn))
{
OracleParameter param = new OracleParameter(":Name",myDate);
command.Paerameters.add(param);
command.ExecuteNonQuery();
}