Solved

How to call a stored procedure?

Posted on 2011-09-26
6
531 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 40
SQL Help 27 45
Adding bootstrap to only Content page and NOT master page 1 12
performance query 4 24
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how the fundamental information of how to create a table.
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.

777 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