Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ADO.NET - Could Not Find Stored Procedure

Tags:

c#

ado.net

I am trying to execute a stored procedure through C#, ADO.NET and below is the code I am trying to execute:

using (SqlConnection conn = new SqlConnection(".;Initial Catalog=MyDB;User ID=sa;Password=***"))
            {
                try
                {
                    string cmdText = "dbo.sp_Create_FlaggedItemEntry @URI, @ID";
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@URI", value1);
                    cmd.Parameters.AddWithValue("@ID", value2);

                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }

Now when I try to debug it, I got an error at the line - cmd.ExecuteNonQuery(); - "Could Not Find Stored Procedure dbo.sp_Create_FlaggedItemEntry"

I verified that the Connection String is all correct and Stored Procedure exists. Further, If I change the line - cmd.CommandType = CommandType.StoredProcedure; to cmd.CommandType = CommandType.Text; it get executed successfully and as expected.

Can someone suggest what I am missing and doing wrong here - Please pardon me if it is something very basic as it is quite long since I last worked with ADO.NET

like image 679
Pankaj Gaur Avatar asked Sep 07 '14 13:09

Pankaj Gaur


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

CommandType.StoredProcedure means that the CommandText should only contain the name of the stored procedure.

Remove the parameter names from the string.

like image 133
SLaks Avatar answered Sep 24 '22 17:09

SLaks


Take the parameters out of the command text. Also, you don't need to specify dbo.

The reason it's working with CommandType.Text is because it's a legitimate SQL command like that - if you were to open up SSMS and type that in it'd work as long as you also create the variables @URI and @ID

Documentation here

like image 41
Ciaran Avatar answered Sep 21 '22 17:09

Ciaran