Solved

How to call a stored procedure?

Posted on 2011-09-26
6
529 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 142

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now