DBNull.Value problem
-
Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.
dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.
dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
t4urean wrote:
DBNull.Value.ToString()
Why are you suppling a
ToString()
onDBNull.Value
? Don't do that just passDBNull.Value
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website
-
t4urean wrote:
DBNull.Value.ToString()
Why are you suppling a
ToString()
onDBNull.Value
? Don't do that just passDBNull.Value
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website
If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
I suspect that is because of the trinary operator that you are using. These are generally considered to be bad practice. Try this:
if (this.tbSpeed.Text == "")
dataRow["SPEED"] = DBNull.Value;
else
dataRow["SPEED"] = this.tbSpeed.Text;
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website
-
If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
And I'd guess that that the contents of the text box will need to be converted to a decimal also.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website
-
Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.
dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
IF you would use a simple "IF", it would be very simple.
-
IF you would use a simple "IF", it would be very simple.
I have used this even then i am getting the same error if (this.tbSpeed.Text == "") dataRow["SPEED"] = DBNull.Value;//"NULL"; else dataRow["SPEED"] = this.tbSpeed.Text;
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.
dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
Your manner of storing values in the database seems pretty unusual. I would create a command with an "INSERT" statement as the command text and call it's ExecuteNonQuery() method. For the decimal column I would use an SQL Parameter of decimal type to avoid format errors.
-
Your manner of storing values in the database seems pretty unusual. I would create a command with an "INSERT" statement as the command text and call it's ExecuteNonQuery() method. For the decimal column I would use an SQL Parameter of decimal type to avoid format errors.
I have found the solution... Just applied the validation when creating the query string and it works fine to specify NUll for an empty string. I am doing in the same way you are saying but i have a seperate class that deals with it when provided with a data row.
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
Try this:
dataRow\["Speed"\] = this.tbSpeed.Text ?? (object) DBNull.Value;