dbnull

SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value

て烟熏妆下的殇ゞ 提交于 2019-12-03 09:19:38
问题 I want to retrieve decimal values from the database and I would like to know which is the recommended way to check for null values. I have seen on MSDN - DBNull.Value Field that this check is rarely used. Thus, is the reader.IsDBNull the best/most efficient way to check for nulls? I have created 2 sample methods: public static decimal? GetNullableDecimal(SqlDataReader reader, string fieldName) { if (reader[fieldName] == DBNull.Value) { return null; } return (decimal)reader[fieldName]; }

How to check for NULL in MySqlDataReader by the column's name?

老子叫甜甜 提交于 2019-12-03 04:58:27
How can I check for a NULL value in an open MySqlDataReader ? The following doesn't work; it's always hitting the else : if (rdr.GetString("timeOut") == null) { queryResult.Egresstime = "Logged in"; } else { queryResult.Egresstime = rdr.GetString("timeOut"); } rdr.IsDbNull(int i) only accepts a column number, not name. var ordinal = rdr.GetOrdinal("timeOut"); if(rdr.IsDBNull(ordinal)) { queryResult.Egresstime = "Logged in"; } else { queryResult.Egresstime = rdr.GetString(ordinal); }//if or if(Convert.IsDBNull(rdr["timeOut"])) { queryResult.Egresstime = "Logged in"; } else { queryResult

Seeding method is inserting additional Entities with NULL values

时光怂恿深爱的人放手 提交于 2019-12-02 10:19:32
I am having this strange behavior all of a sudden (i have compared my files in version control (tfs) to be sure i did not change anything and i didn't found anything different). I am seeding my database with some metadata and i see that it has a very strange behavior i never saw before. I am inserting a Entity "Product" and it inserts this entity 2 times , first insert is correct and has everything it should have, the other one has NULL properties (string values) but some (like datetimes) have values. I have totally no clue why this is happening, it is occurring when i call the base.Seed(ctx);

Difference between DbNull.Value and DbNull.Value.ToString()

孤街醉人 提交于 2019-12-02 07:25:48
问题 I wanted to learn which usage is true? if(!string.IsNullOrEmpty(parentID)) cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID)); else cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value)); OR if(!string.IsNullOrEmpty(parentID)) cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID)); else cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString())); 回答1: cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID)); This is passing a parentID to

Crystal report not support null Datetime

时间秒杀一切 提交于 2019-12-02 05:56:21
I am using crystal report with ado.Net, when datetime is coming null from database then showing error. if we use Nullable (using ?) property then crystal report can't support. Like:- _report = ( from user in users select new userModel() { UserId = user.Field<string>("userid"), CheckInTime = <user.Field<DateTime>("intime"), CheckOutTime = user.Field<DateTime>("outime"), TotalWorks = user.Field<Int32>("TotalWork") }) .ToList(); throwing error here...... and when we use Nullabe..:- public DateTime? CheckInTime { get; set; } public DateTime? CheckOutTime { get; set; } CheckInTime = <user.Field

Parsing a DBNULL value into double

自闭症网瘾萝莉.ら 提交于 2019-12-02 05:41:02
I use the following line to convert the datarow value into double. double.parse(Convert.ToString(datarow)); If the datarow is DBNULL , I am getting the following exception: 'double.Parse(Convert.ToString(data))' threw an exception of type 'System.FormatException' How to handle this one without using tryparse. Another alternative would be to check if the datarow is DBNull : double d = datarow is DBNull ? 0 : double.Parse(Convert.ToString(datarow)); This way, you do not need to check for DBNull.Value DBNull can't be cast or parsed to double (or int , decimal , etc), so you have to check if

Passing Null/empty string to Oracle stored procedure from asp.net

半腔热情 提交于 2019-12-02 04:55:26
We have an ASP.NET web service that invokes a stored procedure on our DB (Oracle 11g). The problem is that the call to the procedure blows up every time an empty string is passed as one of the parameters. We're seeing an ORA-01084 error, which indicates to me that call is failing before the procedure is actually run. Here's the procedure, minus some logic that I believe is not relevant: PROCEDURE CreateReport ( from_dt IN NVARCHAR2, to_dt IN NVARCHAR2, p_table_id IN NVARCHAR2, p_column_id IN NVARCHAR2, device_name IN NVARCHAR2, ret_cursor OUT t_cursor ) AS v_cursor t_cursor; dateStr NVARCHAR2

avoid checking for DataRow.IsDBNull on each column?

可紊 提交于 2019-12-01 22:12:00
问题 My code is 2x longer than it would be if I could automatically set IsDBNull to "" or simply roll over it without an error. This is my code: Dim conn As New SqlConnection conn.ConnectionString = Module1.DBConn2 Dim sqlCommand = New SqlCommand("SELECT * FROM table", conn) conn.Open() Dim sqlDataset As DataSet = New DataSet() Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand) sqlDataAdapter.Fill(sqlDataset) conn.Close() For Each rs As DataRow In sqlDataset.Tables(0).Rows If

avoid checking for DataRow.IsDBNull on each column?

為{幸葍}努か 提交于 2019-12-01 21:43:04
My code is 2x longer than it would be if I could automatically set IsDBNull to "" or simply roll over it without an error. This is my code: Dim conn As New SqlConnection conn.ConnectionString = Module1.DBConn2 Dim sqlCommand = New SqlCommand("SELECT * FROM table", conn) conn.Open() Dim sqlDataset As DataSet = New DataSet() Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand) sqlDataAdapter.Fill(sqlDataset) conn.Close() For Each rs As DataRow In sqlDataset.Tables(0).Rows If Not IsDBNull(rs("column")) Then Response.Write(rs("column")) Else Response.Write("") End If Response

Operand type clash: nvarchar is incompatible with image

喜夏-厌秋 提交于 2019-12-01 16:37:33
I'm using a SQL Server 2008 stored procedure to create a new record with this syntax: cmd.Parameters.Add("@photo", DBNull.Value) cmd.ExecuteNonQuery() but the result is a: Operand type clash: nvarchar is incompatible with image Photo is not the only parameter but is the only image one, I am not passing a nvarchar but a null value, am I missing something? If you pass in DBNull.Value as the value, ADO.NET can't figure out what type the parameter should be. If you specify a string, or an integer value, the type of the SQL parameter can be derived from the value provided - but what type should