I have an asp.net page, I have a text box where people can enter an
integer (age). I wish to store this in a SQLServer Database. If the
person enters no age then I would store a NULL.
However if the textbox is left blank, I get an error
"[InvalidCastException: Cast from string "" to type 'Integer' is not
valid.]"
How can I work around this, and get the null into the db?
Cheers
Fragg 4 9620
You have to use Convert.DBNul
For example (using a stored procedure)
int intValueFromTextBox = Convert.ToInt32(txtTextBox.Text)
if(intValueFromTextBox ==null
cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = Convert.DBNull
els
cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = intValueFromTextBox
}
how are you accessing db? with stored procedures? when setting the parameter
value check if the text box value is "" and set the value to null.
Av.
"Fraggle" <Fr************@yahoo.com> wrote in message
news:ce**************************@posting.google.c om... I have an asp.net page, I have a text box where people can enter an integer (age). I wish to store this in a SQLServer Database. If the person enters no age then I would store a NULL.
However if the textbox is left blank, I get an error "[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]"
How can I work around this, and get the null into the db?
Cheers
Fragg
If you're using a SQL Statment, you can simply omit the column from the
INSERT SQL statement.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Fraggle" <Fr************@yahoo.com> wrote in message
news:ce**************************@posting.google.c om... I have an asp.net page, I have a text box where people can enter an integer (age). I wish to store this in a SQLServer Database. If the person enters no age then I would store a NULL.
However if the textbox is left blank, I get an error "[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]"
How can I work around this, and get the null into the db?
Cheers
Fragg
What DB are you using? if you're using SQL, What type did you give to your
Age Column (int, string, etc?)make sure it matches the cast type you are
getting back from the DB.
Go to the Age COlumn in you table and select Allow Nulls.
let me know
Patrick
"avnrao" <av*@newsgroups.com> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl... how are you accessing db? with stored procedures? when setting the
parameter value check if the text box value is "" and set the value to null. Av.
"Fraggle" <Fr************@yahoo.com> wrote in message news:ce**************************@posting.google.c om...I have an asp.net page, I have a text box where people can enter an integer (age). I wish to store this in a SQLServer Database. If the person enters no age then I would store a NULL.
However if the textbox is left blank, I get an error "[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]"
How can I work around this, and get the null into the db?
Cheers
Fragg
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Martin |
last post by:
Hello,
I'm new with triggers and I can not find any good example on how to
do the following:
I have two tables WO and PM with the following fields:
WO.WONUM, VARCHAR(10)
WO.PMNUM,...
|
by: Abareblue |
last post by:
I have no clue on how to insert a record into access. here is the whole
thing
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
|
by: Sean |
last post by:
HI There,
I am making the transition from asp to asp .net, I am currenty writing an
application that requires a bulk insert from a webform into SQL server,
normally I would just create rows of...
|
by: Arek |
last post by:
Hey,
I am inserting values in the table:
Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)...
|
by: Pablo S |
last post by:
Hi all, I have search high and low on this -
Take for instance the statement :
insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
On Pg 7.2.x, the db would happily insert the...
|
by: 4partee |
last post by:
I'm trying to import a csv file with a PHP procedure. However, some of the
lines in the csv file have missing values.
When this command is given to mysql:
insert tablex values...
|
by: NuB |
last post by:
I have a sql query that is doing an update of records, how can I add NULL to
the field in the database if the field on my screen is blank?
example:
I have 5 textboxes, and a user can leave some...
|
by: Lorenzino |
last post by:
Hi, I have a problem with bindings in a formview.
I have a formview; in the insert template i've created a wizard control and
inside
it i have an HTML table with some textboxes bound to the...
|
by: Davor |
last post by:
I'm trying to import data from flat file in table and have few
problems.
1. Field Delimiter is ',' (comma). If ',' occurs in quoted
string it is still treated as field delimiter. This is BUG or...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |