?
Solved

How to call a stored procedure?

Posted on 2011-09-26
6
Medium Priority
?
538 Views
Last Modified: 2013-12-17

Hello,

I have a stored procedure and I call it from C# code. However, I get an error as:

Could not find stored procedure 'xxx'

Below is the code and also run it from sql server itself using

exec dbo.proc_LogIn  @Username = 'user1', @Password = 'password1', @OutRes=1;

Can be permission issue from C# side?

Any help is appreciated.

Thanks.




public int Validate_Login(String strUserName, String strPassword)
        {
            SqlConnection con = new SqlConnection(@"server=myMachine\SQLEXPRESS;integrated security=true");
            SqlCommand cmdselect = new SqlCommand();
            cmdselect.CommandType = CommandType.StoredProcedure;
            cmdselect.CommandText =  "[dbo].[proc_LogIn]";
            cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = strUserName;
            cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = strPassword;
            cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
            cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
            cmdselect.Connection = con;
            int Results = 0;
            try
            {
                con.Open();
                cmdselect.ExecuteNonQuery();  //THIS LINE THROWS ERROR
                Results = (int)cmdselect.Parameters["@OutRes"].Value;
            }
           catch (SqlException ex)
            {
                lblMessage.Text = ex.Message;
            }
            finally
            {
                cmdselect.Dispose();
                if (con != null)
                {
                    con.Close();
                }
            }
            return Results;
        }

Open in new window

0
Comment
Question by:akohan
6 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 36597917
>            SqlConnection con = new SqlConnection(@"server=myMachine\SQLEXPRESS;integrated security=true");

I presume rather that you don't connect to the db where the stored procedure exists.
like the username connects to it's default db, which might be "master" by default ...

of course, it could also be permissions missing ...
0
 
LVL 3

Expert Comment

by:anjos
ID: 36597928
add "Initial Catalog=FAQ" to the connection string (SqlConnection con)

and in the cmdselect.CommandText write the name of the procedure, ie:
cmdselect.CommandText = "proc_LogIn"
0
 
LVL 3

Assisted Solution

by:anjos
anjos earned 400 total points
ID: 36597931
heh correction:
"Initial Catalog=_THE_NAME_OF_DB_"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Accepted Solution

by:
eridanix earned 1200 total points
ID: 36598293
The whole connection string should have this format:

Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)

Replace name of database Northwind with name of your database.

In place of you use server=myMachine\SQLEXPRESS you can use server=(local) if your SQL SERVER runs on the same machine as your program runs,
In other case you have to exactly specify your server path and name. For example with ip of server, where SQL Server runs and name of server.
Example: 195.47.58.15\SQLEXPRESS
0
 

Author Comment

by:akohan
ID: 36601464

Thank you to all for the help.

Regards.
0
 

Author Closing Comment

by:akohan
ID: 36601484
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question