1

hello i build a webservice in visual studio 2010. i get some id's which are saved in a string looks like this:

string room_ids="5,11,99,42";

they are separated by comma. i created a foreach loop to split the ids and from the comma and use them in my sql query until the ids are finished. but it doesn't work. i get an error it says:

Error converting data type nvarchar to numeric

here is my code, thanks in advance for your help!

internal static List<RAUM> Raum(string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
{       
    List<RAUM> strasseObject = new List<RAUM>();
    string[] allegebaude = GEBAEUDE_ID.Split(new char[] { ',' });

    foreach (string gebaudeid in allegebaude)
    {
        Trace.WriteLine("SIND JETZT DRINNE");
        Trace.WriteLine(gebaudeid);

        using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
        using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) AND STADT_ID = ISNULL(@Stadt_ID, STADT_ID) AND GEBAEUDE_ID = ISNULL(@gebaudeid,GEBAEUDE_ID ) AND REGION_ID = ISNULL(@Region_ID, REGION_ID)", con))
        {
            con.Open();
            if (!StringExtensions.IsNullOrWhiteSpace(RAUMKLASSE_ID))
                cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
            else
                cmd.Parameters.AddWithValue("@Raumklasse_ID", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(STADT_ID))
                cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
            else
                cmd.Parameters.AddWithValue("@Stadt_ID", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(GEBAEUDE_ID))
                cmd.Parameters.AddWithValue("@gebaudeid", GEBAEUDE_ID);
            else
                cmd.Parameters.AddWithValue("@gebaudeid", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(REGION_ID))
                cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);
            else
                cmd.Parameters.AddWithValue("@Region_ID", DBNull.Value);



            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["ID"] != DBNull.Value)
                    {
                        strasseObject.Add(new RAUM()
                        {
                            RaumName = rdr["BEZEICHNUNG"].ToString(),
                            RaumID = rdr["ID"].ToString()

                        });
                    }
                }
            }

        }
    }
    return strasseObject;
}

4 Answers 4

2

If you already have the IDs in a comma-separated string (called IDstring) then you can just do something like this:

sqlQuery = "SELECT Columns FROM table WHERE ID IN (" + IDstring + ")";

In your specific case, don't split the original string (GEBAEUDE_ID) but use it as it is:

   // Don't use a foreach loop any more
   string gebaudeIdSection = " AND GEBAEUDE_ID IN (" + GEBAEUDE_ID + ") ";
   if (string.IsNullOrEmpty(GEBAUDE_ID)) { gebaudeIdSection = ""; } // if there are no ids, let's remove that part of the query completely.
   using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
   using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) AND STADT_ID = ISNULL(@Stadt_ID, STADT_ID)" + gebaudeIdSection + " AND REGION_ID = ISNULL(@Region_ID, REGION_ID)", con))
   { // The rest of the code is the same as before...
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your help, i have a little problem, i can't set the gebaudeid to empty, because my string is whith []. how can i solve this?
I'm not sure I understand. You don't do anything with gebaudeid. You need to take the original comma-separated string GEBAUDE_ID that you passed into the function and just use that without splitting it. Remove the bit of code that splits the string into an array.
it works but when i let the gebaude id empty i get an error: Incorrect syntax near ')'.
I have tweaked the code to completely omit that part of the query if there are no ids.
thank you it works, i tried your solution for the other ids,too. but it seems like difficult. because of the "AND" in your string. when i just give 1 parameter the "AND" in the string creates an error. i don't know how to fix that
1

First of all I think you have to correct:

cmd.Parameters.AddWithValue("@gebaudeid", GEBAEUDE_ID);

with:

cmd.Parameters.AddWithValue("@gebaudeid", gebaudeid);

Then, try to convert the ids into integers ( for example, using Convert.ToInt32(gebaudeid) ) and not to pass them as strings in AddWithValue method.

Comments

1

try this:

if (!StringExtensions.IsNullOrWhiteSpace(gebaudeid))
            cmd.Parameters.AddWithValue("@gebaudeid", Convert.ToInt32(gebaudeid));

you should also pass the right parameter to your AddWithValue statement. You are iterating over the list of your ID's, that list being allegebaude. So you have to pass the gebaudeid parameter to your statement, instead of what you're doing now.

Comments

0

You can't implicitly treat a comma separated list of values; instead you'll need to create a table values function to split the list of values into a table structure and then use this structure as you would normally.

Have a look at this question: INSERT INTO TABLE from comma separated varchar-list for a sample function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.