ISNULL question
Im a little confused about the proper way of using the ISNULL feature, or if it can be used at all for variables. Im having an issue relating to user input from a form stored in a session variable as well as other strings in the session. Its an address form and when the user doesnt enter anything in the input obviously the value stored is a null. Ocassionally I get a Sql server exception error saying I cant have a null value. I know which line of code causes this error but Im not sure how to fix it. I need a way to handle the null values but inserting some other value into the variable so it cant be null. So something like "IF textboxValue = NULL make textBox value 0". Thats not code obviously but thats the kind of thing Im trying to do. If anyone can lend some advice for me I'd seriously appreciate it because this is keeping people from ordering from me when the error happens. Thanks in advance!
Jan MontanoPosted Oct 9, 2007, 1:32 AM
//if (sqlRdr.GetString(8) != DBNull)
if (sqlRdr.GetString(8) != DBNull.Value) // try the commented if statement above if this does not work
{
sCollection = sqlRdr.GetString(8);
}
else
{
sCollection = "0";
}
You could also substitute the above if-else condition with
sCollection = sqlRdr.GetString(8) == DBNull.Value ? "0" : sqlRdr.GetString(8);
DanPosted Oct 8, 2007, 9:32 AM
Thank you for going in depth like that, it was very h elpful. My situation was a little different on handling the variable. Here is what the code looks like, I added the if/else at the end and I dont think works right but the code does execute at least so its not syntax.
while (sqlRdr.Read() == true) //this must match p8
{
double price = 0;
if (sqlRdr.GetValue(6).Equals("STD")) price = Convert.ToDouble(sqlRdr.GetDecimal(3)); else price = Convert.ToDouble(sqlRdr.GetDecimal(7));
subtot = Convert.ToDouble((sqlRdr.GetInt32(0) * price));
total += subtot;
sQty = sqlRdr.GetInt32(0).ToString();
sItem = sqlRdr.GetString(1).Trim();
if (sqlRdr.GetString(8) != null)
{
sCollection = sqlRdr.GetString(8);
}
else
{
sCollection = "0";
}
Jan MontanoPosted Oct 8, 2007, 3:01 AM
private void button1_Click(object sender, EventArgs e)
{
bool result;
collection.Text = null;
result = String.IsNullOrEmpty(collection.Text);
MessageBox.Show(result.ToString()); //returns true because collection.Text is null
collection.Text = "";
result = String.IsNullOrEmpty(collection.Text); //retunrs true because collection.Text contains an empty string
//MessageBox.Show(result.ToString());
if (result)
{
//do your processing here.
}
collection.Text = "Dan";
result = String.IsNullOrEmpty(collection.Text); //returns false because it has a value
MessageBox.Show(result.ToString());
}
DanPosted Oct 5, 2007, 10:23 AM
Let me make sure I understand....
If I had a textbox called sCollection I could do something like "String.IsNullOrEmpty(sCollection)". That forces the value of the textbox to be null, right?? Im sorry but Im not a C coder yet. Im still wet behind the ears. Im not looking to force a field to be null but to do a check before the variable "sCollection" is trimmed and passed on to the server. So I would need some kind of IF/ELSE setup to make the string a default value just in case it does have a null. Because the null value that this field contains causes the errors. Its an SqlNullException error to be specific.
Jan MontanoPosted Oct 5, 2007, 2:19 AM
What ISNULL feature in C# are you referring to exactly? Also consider using String.IsNullOrEmpty(textBoxvalue) method.