Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:45:20+00:00 2026-06-05T15:45:20+00:00

I am passing 4 Parameters to an asp.net Webservice. This is my Code so

  • 0

I am passing 4 Parameters to an asp.net Webservice. This is my Code so far:

Webmethod:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public List<RaumHelper.RAUM> Raum(string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
    {
        return RaumHelper.Raum(RAUMKLASSE_ID, STADT_ID, GEBAEUDE_ID, REGION_ID);
    }

Helperclass:

   public class RaumHelper
            {
                public class RAUM
                {
                    public string RaumName { get; set; }
                    public string RaumID { get; set; }

                }

                internal static List<RAUM> Raum( string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
                {
                    List<RAUM> strasseObject = new List<RAUM>();

                    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 = @Raumklasse_ID OR STADT_ID = @Stadt_ID OR GEBAEUDE_ID = @Gebaeude_ID OR REGION_ID = @Region_ID", con)) 
                    {


                        con.Open();
                        cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
                        cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
                        cmd.Parameters.AddWithValue("@Gebaeude_ID", GEBAEUDE_ID);
                        cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);



                        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;
                }

            }

If i invoke that Webmethod with the 4 Parameters, the Method is working fine an i get a LIST of the RaumName’s and RaumID’s. But if i put only one Parameter iam getting an Error:

    System.Data.SqlClient.SqlException: Error converting data type nvarchar to numeric.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at System.Data.SqlClient.SqlDataReader.Read()

The ID’s in the Database are stored as Numeric and i passing string. I think that is the problem. But i dont know how to fix this.

I also want that my Query works also with only two or three entered Parameters.

Thank in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:45:22+00:00Added an answer on June 5, 2026 at 3:45 pm

    Usually it’s not a problem to pass a string to a parameter that’s numeric, as long as the SQL Server is able to convert the content of the string to a numeric value itself. If that doesn’t work, you get this error.

    For example: Passing "Hello" to a parameter that’s numeric, you get an error. Passing "1234" you don’t. Please note that an empty string or a string containing whitespace can not be converted to a numeric value!

    However, it should be said that it is not good style to do that. You should make sure that the types you use in your application match the types in the database to avoid problems. Maybe some further detail on why you need to have string types in your application could help.

    EDIT 1
    To make a parameter optional for the query, the way to go would be the following:

    1. Change your SQL statement to allow optional parameters like WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID).
    2. Do not add the @Raumklasse_ID parameter if it should be optional or add the value DBNull.Value

    You should really consider changing your string properties to nullable types like int?.

    EDIT 2
    This is how your code could look implementing the changes I suggested in Edit 1:

    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) OR STADT_ID = ISNULL(@Stadt_ID, STADT_ID) OR GEBAEUDE_ID = ISNULL(@Gebaeude_ID, GEBAEUDE_ID) OR REGION_ID = ISNULL(@Region_ID, REGION_ID)", con)) 
    {
        con.Open();
        if (!String.IsNullOrWhitespace(RAUMKLASSE_ID))
            cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
        else
            cmd.Parameters.AddWithValue("@Raumklasse_ID", DBNull.Value);
        if (!String.IsNullOrWhitespace(STADT_ID))
            cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
        else
            cmd.Parameters.AddWithValue("@Stadt_ID", DBNull.Value);
        if (!String.IsNullOrWhitespace(GEBAEUDE_ID))
            cmd.Parameters.AddWithValue("@Gebaeude_ID", GEBAEUDE_ID);
        else
            cmd.Parameters.AddWithValue("@Gebaeude_ID", DBNull.Value);
        if (!String.IsNullOrWhitespace(REGION_ID))
            cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);
        else
            cmd.Parameters.AddWithValue("@Region_ID", DBNull.Value);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm passing 2 hardcoded parameters to an ASP.Net MVC2 Controller Action with this code:
ColdFusion code calls an ASP.NET web service passing the following parameters: Web service operation
I used below code in ASp.NET web forms code behined. Its passing parameter in
This is a follow up to my previous question: Problem passing parameters via Iframe
I'm not quite understanding how this works. Passing parameters from my entity objects works
I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json
I'm passing parameters to a .NET ClickOnce-deployed application via the URL from a Flex
I am passing parameters to a stored proc. The parameters code block on the
For this question, I'm using ASP.NET Web Forms in C#, a web service and
I'm using a masterpage in my ASP.NET MVC project. This masterpage expects some ViewData

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.