Wednesday, March 28, 2012

if else statement problems

Hello to all...

I have an if-else statement that looks for a null array. I know the array is null, because I didn't put anything in it, but my Sql will still try to insert:

if (Line1Array ==null) Response.Write("Array 1 is empty");else for (int i = 0; i < Line1Array.Length; i++) SqlDataSource1.InsertParameters.Clear(); SqlDataSource1.InsertParameters.Add("Shift", ShiftDDL.Text); SqlDataSource1.InsertParameters.Add("Line", LinesDDL.Text); SqlDataSource1.InsertParameters.Add("Product", ProductsDDL.Text); SqlDataSource1.InsertParameters.Add("OPProfile", OPSLabel.Text); SqlDataSource1.InsertParameters.Add("OPAsstProfile", AsstLabel.Text); SqlDataSource1.InsertParameters.Add("OPActual", OPActualTextBox.Text); SqlDataSource1.InsertParameters.Add("OPAsstActual", OPAsstActualTextBox.Text); SqlDataSource1.InsertParameters.Add("Notes", NotesTextBox.Text); SqlDataSource1.InsertParameters.Add("Date", Calendar1.SelectedDate.ToString()); SqlDataSource1.InsertParameters.Add("Day", strCalDateDay); SqlDataSource1.InsertParameters.Add("Week", strCalDateWeek); SqlDataSource1.InsertParameters.Add("Month", strCalDateMonth); SqlDataSource1.InsertParameters.Add("Year", strCalDateYear); SqlDataSource1.Insert();

Does anyone see what is wrong with the statement?

Thanks for your help

Just because you didn't put any items into your array doesn't mean it will evaluate to null. If you created a new instance of the array, then the array will not evaluate to null. Instead, the Length will simply be zero.|||
if (Line1Array ==null)
{ Response.Write("Array 1 is empty");
}else
{ for (int i = 0; i < Line1Array.Length; i++)
{ SqlDataSource1.InsertParameters.Clear(); SqlDataSource1.InsertParameters.Add("Shift", ShiftDDL.Text); SqlDataSource1.InsertParameters.Add("Line", LinesDDL.Text); SqlDataSource1.InsertParameters.Add("Product", ProductsDDL.Text); SqlDataSource1.InsertParameters.Add("OPProfile", OPSLabel.Text); SqlDataSource1.InsertParameters.Add("OPAsstProfile", AsstLabel.Text); SqlDataSource1.InsertParameters.Add("OPActual", OPActualTextBox.Text); SqlDataSource1.InsertParameters.Add("OPAsstActual", OPAsstActualTextBox.Text); SqlDataSource1.InsertParameters.Add("Notes", NotesTextBox.Text); SqlDataSource1.InsertParameters.Add("Date", Calendar1.SelectedDate.ToString()); SqlDataSource1.InsertParameters.Add("Day", strCalDateDay); SqlDataSource1.InsertParameters.Add("Week", strCalDateWeek); SqlDataSource1.InsertParameters.Add("Month", strCalDateMonth); SqlDataSource1.InsertParameters.Add("Year", strCalDateYear); SqlDataSource1.Insert();
}
}
|||

Thanks for shedding light on that... I guess thats why you guys get paid the big bucks! I ended up doing this.. and it works nicely!

if (Line1Array.GetValue(1) ==null) Response.Write("Array 1 is empty");else for (int i = 0; i < Line1Array.Length; i++) SqlDataSource1.InsertParameters.Clear(); SqlDataSource1.InsertParameters.Add("Shift", ShiftDDL.Text); SqlDataSource1.InsertParameters.Add("Line", LinesDDL.Text); SqlDataSource1.InsertParameters.Add("Product", ProductsDDL.Text);if (OPSLabel !=null) { SqlDataSource1.InsertParameters.Add("OPProfile", OPSLabel.Text); SqlDataSource1.InsertParameters.Add("OPAsstProfile", AsstLabel.Text); } SqlDataSource1.InsertParameters.Add("OPActual", OPActualTextBox.Text); SqlDataSource1.InsertParameters.Add("OPAsstActual", OPAsstActualTextBox.Text); SqlDataSource1.InsertParameters.Add("Notes", NotesTextBox.Text); SqlDataSource1.InsertParameters.Add("Date", Calendar1.SelectedDate.ToString()); SqlDataSource1.InsertParameters.Add("Day", strCalDateDay); SqlDataSource1.InsertParameters.Add("Week", strCalDateWeek); SqlDataSource1.InsertParameters.Add("Month", strCalDateMonth); SqlDataSource1.InsertParameters.Add("Year", strCalDateYear);if (OPSLabel !=null) { SqlDataSource1.Insert(); }
|||

I still don't believe that is what you want it to do. Let me fix your indentation, so it reads how it is coded:

if (Line1Array.GetValue(1) ==null)Response.Write("Array 1 is empty");elsefor (int i = 0; i < Line1Array.Length; i++)SqlDataSource1.InsertParameters.Clear();SqlDataSource1.InsertParameters.Add("Shift", ShiftDDL.Text);SqlDataSource1.InsertParameters.Add("Line", LinesDDL.Text);SqlDataSource1.InsertParameters.Add("Product", ProductsDDL.Text);if (OPSLabel !=null){SqlDataSource1.InsertParameters.Add("OPProfile", OPSLabel.Text);SqlDataSource1.InsertParameters.Add("OPAsstProfile", AsstLabel.Text);}SqlDataSource1.InsertParameters.Add("OPActual", OPActualTextBox.Text);SqlDataSource1.InsertParameters.Add("OPAsstActual", OPAsstActualTextBox.Text);SqlDataSource1.InsertParameters.Add("Notes", NotesTextBox.Text);SqlDataSource1.InsertParameters.Add("Date", Calendar1.SelectedDate.ToString());SqlDataSource1.InsertParameters.Add("Day", strCalDateDay);SqlDataSource1.InsertParameters.Add("Week", strCalDateWeek);SqlDataSource1.InsertParameters.Add("Month", strCalDateMonth);SqlDataSource1.InsertParameters.Add("Year", strCalDateYear);if (OPSLabel !=null){SqlDataSource1.Insert();}
Because your for loop and else clause is not enclosed in braces {}, only the first statement is being done.
In this case if Line1Array is not null, you are clearing the parameters from the SqlDataSource1 once for each element.
Then you add a bunch of parameters (always, and always only once).
Then you do an insert if OPSLabel isn't null.
 
This begs the questions.. Why are you repeatedly clearing the SqlDataSource parameters in a loop? That's probably not what you meant.
Why are you going through the whole process of clearing, and setting a bunch of values, only to not do anything with them at the end?
(Why check if OPSLabel is null at the end instead of at the beginning?)
|||

You are right.. I have fixed that now. Another problem with the if's and else's -- I need to verify if another array block is empty, but just before the insert, not just skipping by the whole line:

if (Line1Array.GetValue(3) ==null) { Response.Write("<script language='javascript'>alert('null');</script>"); }else {if (Line1Array.GetValue(6) ==null) { Label1.Text = Line1Array.GetValue(3).ToString(); Response.Write("<script language='javascript'>alert('You are missing an entry for Actual Operators on ROW 1');</script>"); }else { SqlDataSource1.InsertParameters.Add("Shift", ShiftDDL.Text); SqlDataSource1.InsertParameters.Add("Line", LinesDDL.Text); SqlDataSource1.InsertParameters.Add("Product", ProductsDDL.Text); SqlDataSource1.InsertParameters.Add("OPProfile", OPSLabel.Text); SqlDataSource1.InsertParameters.Add("OPAsstProfile", AsstLabel.Text); SqlDataSource1.InsertParameters.Add("OPActual", OPActualTextBox.Text); SqlDataSource1.InsertParameters.Add("OPAsstActual", OPAsstActualTextBox.Text); SqlDataSource1.InsertParameters.Add("Notes", NotesTextBox.Text); SqlDataSource1.InsertParameters.Add("Date", Calendar1.SelectedDate.ToString()); SqlDataSource1.InsertParameters.Add("Day", strCalDateDay); SqlDataSource1.InsertParameters.Add("Week", strCalDateWeek); SqlDataSource1.InsertParameters.Add("Month", strCalDateMonth); SqlDataSource1.InsertParameters.Add("Year", strCalDateYear);//SqlDataSource1.Insert(); SqlDataSource1.InsertParameters.Clear(); } }

but this doesn't work! Value(6) is null, but is passes on and tries to insert. I know the javascript works, verified outside of the statement.

Any ideas?

Thanks

|||

I'm afraid I can't answer that one for you. I'm not well versed enough in C# to give you the answer you seek. However, in your response.write, the first less than is being sent out as <, and I'm pretty sure that isn't valid.

sql

No comments:

Post a Comment