I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined" , that I would like to set to Null. My first thought was to create an array and then use a foreach statement to run each string through the switch statement. The obvious problem here is that you cannot assign to the iteration variables. How can I get around this?
Multiple String Switch Statement
Collapse
X
-
Create a list/array but use a for loop instead of a foreach. Then you will be able to assign values to your strings.Originally posted by mcfly1204I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined" , that I would like to set to Null. My first thought was to create an array and then use a foreach statement to run each string through the switch statement. The obvious problem here is that you cannot assign to the iteration variables. How can I get around this?
[CODE=cpp]
for(int i=0; i<stringsArray. Length; i++)
{
switch (stringsArray[i])
{
case "None":
stringsArray[i] = null;
break;
}
}[/CODE]Comment
-
Thank you for that, but I now have a new issue. Now that I have eliminated unwanted strings and set them to null, I now need an if statement to not add the parameter if the value is null. Such as:
[code=cpp]
If (strValue1 != null)
{
OleDBCommand.Pa rameters.AddWit hValue("@value1 ", strValue1);
}
else
{
*code to move on to next parameter to add*
}[/code]
If the value is null, how would I proceed to the next parameter to add?Comment
-
Just use the same string array and iterate through it again. Also, set up an array with your parameter names.Originally posted by mcfly1204Thank you for that, but I now have a new issue. Now that I have eliminated unwanted strings and set them to null, I now need an if statement to not add the parameter if the value is null. Such as:
If (strValue1 != null)
{
OleDBCommand.Pa rameters.AddWit hValue("@value1 ", strValue1);
}
else
{
*code to move on to next parameter to add*
}
If the value is null, how would I proceed to the next parameter to add?
[CODE=cpp]
for (int i = 0; i < stringsArray.Le ngth; i++)
{
if (stringsArray[i] != null)
cmd.Parameters. AddWithValue(pa ramArray[i], stringsArray[i]);
}
[/CODE]
Where cmd is your OleDbCommand.Comment
-
So instead of adding the parameters, I would take all parameters in question, add them to an array, and then call something such as what you have above and dynamically add those parameters based of the string value? I just would not have thought to add parameters for a query in one method from another parameter, but I suppose if they're in the same class, it shouldn't matter.Originally posted by insertAliasJust use the same string array and iterate through it again. Also, set up an array with your parameter names.
[CODE=cpp]
for (int i = 0; i < stringsArray.Le ngth; i++)
{
if (stringsArray[i] != null)
cmd.Parameters. AddWithValue(pa ramArray[i], stringsArray[i]);
}
[/CODE]
Where cmd is your OleDbCommand.Comment
-
Well, that might not work, now that I think about it. I think that you have to add something for each parameter. I don't usually use parameters. I usually dynamically build my sql statement. So what you could do is write your sql statement up to the "where" clause. Then check to see if you have any valid parameters. If you do, add "where " and then iterate through your array of parameter values. If it isn't null, then use [CODE=cpp]sql += String.Format(" value1 = '{0}' and" ,stringsArray[i]);[/CODE] to the end of your sql statement (assuming you have your sql statement stored in a string called sql. Once you are done, remove the last 4 chars from the string (the last "and ") and you have a full sql statement.Originally posted by mcfly1204So instead of adding the parameters, I would take all parameters in question, add them to an array, and then call something such as what you have above and dynamically add those parameters based of the string value? I just would not have thought to add parameters for a query in one method from another parameter, but I suppose if they're in the same class, it shouldn't matter.
Also, instead of "value1" you could have another string[] paramArray. This could hold all the names of your parameters. So then the statement would be:
[CODE=cpp]
for(int i=0; i<stringsArray. Length; i++)
{
if(stringsArray[i] != null)
sql += String.Format(" {0} = '{1}' AND ",paramArra y[i], stringsArray[i]);
}
[/CODE]
Just remember to remove the last "AND ".
If you don't know what the String.Format() does, look it up. It is very useful.Comment
-
So here you are formatting the first item in the array to equal the next item and then adding the string AND to the end I am inexperience both with building sql statements dynamically as well as not using parameters.Originally posted by insertAliasAlso, instead of "value1" you could have another string[] paramArray. This could hold all the names of your parameters. So then the statement would be:
[CODE=cpp]
for(int i=0; i<stringsArray. Length; i++)
{
if(stringsArray[i] != null)
sql += String.Format(" {0} = '{1}' AND ",paramArra y[i], stringsArray[i]);
}
[/CODE]
Just remember to remove the last "AND ".
If you don't know what the String.Format() does, look it up. It is very useful.Comment
-
Not quite. The way string.Format works is that it evaluates "{#}" as one of the parameters following. So:Originally posted by mcfly1204So here you are formatting the first item in the array to equal the next item and then adding the string AND to the end I am inexperience both with building sql statements dynamically as well as not using parameters.
[code=cpp]
string temp = "Bob";
string out = String.Format(" {0} is his name",temp);
//out == "Bob is his name"
[/code]
What I did in that loop:
lets say I have a sql statement with 4 possible params: nameFirst, nameLast, nameMiddle, and userName. What I might do is:
[code=cpp]
string[] paramArray = new string[4];
paramArray[0] = "nameFirst" ;
paramArray[1] = "nameLast";
paramArray[2] = "nameMiddle ";
paramArray[3] = "userName";
[/code]
Then, I would get my value strings like you did, and assign them to stringsArray. Then run them through the switch statement and so forth.
Then you could build your sql statement.
[code=cpp]
string sql = "select * from tableName ";
bool hasParams = false;
for(int i=0; i<paramArray.Le ngth; i++)
{
if(stringsArray[i] != null)
{
hasParams = true;
break;
}
}
if(hasParams)
{
sql += " where ";
for(int i=0; i<stringsArray. Length; i++)
{
if(stringsArray[i] != null)
sql += String.Format(" {0} = '{1}' AND ",paramArra y[i], stringsArray[i]);
}
sql = sql.Substring(0 ,sql.Length - 4);
}
[/code]
What I did there is check to see if any of the strings aren't null (as per the way you were setting them null in the switch.) Then, if there are any "where" parameters, we finish building the sql statement. What that String.Format would evaluate to is "paramName = 'paramValue' AND "
So, in practice, if nameFirst should have a value of Bob, it will evaluate to:
"nameFirst = 'Bob' AND "
And at the end, we remove the extra "AND " (4 chars off the end.) You have a complete sql statement.
Maybe not the most simple way of doing it, but it works.Comment
-
So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:
INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**
cmd.Parameters. AddWithValue("@ parameter1", value1);
cmd.Parameters. AddWithValue("@ parameter2", value2);
***
Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.Comment
-
Ok, I was working on the incorrect assumption on a select statement. What you want to do is perfectly possible with System.Data.Sql Types.Originally posted by mcfly1204So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:
INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**
cmd.Parameters. AddWithValue("@ parameter1", value1);
cmd.Parameters. AddWithValue("@ parameter2", value2);
***
Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.
[code=cpp]
SqlCommand cmd = new SqlCommand(sqlI nsertText,conne ctionObject);
SqlParameter parameter = new SqlParameter("@ param1", System.Data.Sql Types.SqlString .Null);
cmd.Parameters. Add(parameter);
[/code]
That will insert a null value for that param. Sorry for all the confusion :(
Now if this isn't a varchar or nvarchar field you might have to use System.Data.Sql Types.whateverDbTypeY ouNeed, but this works for an nvarchar.Comment
Comment