Solved

How to call a stored procedure?

Posted on 2011-09-26
6
532 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 100 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 100 total points
ID: 36597931
heh correction:
"Initial Catalog=_THE_NAME_OF_DB_"
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Accepted Solution

by:
eridanix earned 300 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 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