3

I want to use stored procedures in C# to get data from a SQL Server table. I want to print the city with the id that I'll pass as a parameter, but I don't know the correct syntax of using parameters like that for a stored procedure in C#.

This is the C# code that I'm using:

string connString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    SqlCommand command = new SqlCommand("SP_GetCityByID where id = 2", conn);
    command.CommandType = CommandType.StoredProcedure;

    SqlParameter param = command.Parameters.Add("@ID", SqlDbType.Int);
    param.Direction = ParameterDirection.Input;

    command.ExecuteNonQuery();

    Console.WriteLine(param.Value);
}
1

2 Answers 2

2
SqlCommand command = new SqlCommand("SP_GetCityByID ", conn);

You don't put a where condition when you call a stored procedure. where condition needs to be inside the body of stored procedure which should compare the id column of your city table with @ID parameter you are passing to stored procedure. Secondly, ExecuteNonQuery function which you have written at the end will not serve your purpose. Use ExecuteScalar function instead as given below:

String cityName= command.ExecuteScalar();

I am assuming your stored procedure accepts parameter @ID and returns matching city name in the form of table.

Sign up to request clarification or add additional context in comments.

6 Comments

ok i do remember hearing about ExecuteScalar but i dont remeber what it is used for. i kinda know not to put the parameter where i called the stored procedure but where DO i call the parameter? i need a place to write something like: "where id = 2"
The place to write the where condition is inside the stored procedure. And you should not write where id= 2 because that would always return same result. Instead write where id = @ID.
ExecuteScalar is used get single value results from database. Inside your stored procedure write a query to get city name on the basis of @ID parameter
yeah i know that, i have that part in the stored procedure. but can't i give the parameter to the stored procedure in the visual studio using c#? in the code i have up there?
For sending value add line param.Value= <your id value> before the statement executing the command. Also you should use AddWithValue instead of Add function for specifying parameters
|
0

provide parameter as below:

  string connString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        SqlCommand command = new SqlCommand("SP_GetCityByID", conn);
        command.CommandType = CommandType.StoredProcedure;
        SqlParameter param = command.Parameters.Add("@ID", SqlDbType.Int).Value = 2;
        //param.Direction = ParameterDirection.Input;
        command.ExecuteNonQuery();
        Console.WriteLine(param.Value);

    }

1 Comment

not working. visual studio throw me red error when i type the value like you suggested

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.