FartStopper
FartStopper

Reputation: 121

Error running SQL Server USP from code

I have been trying to run a SQL Server query from my C# code, but each time I get an error of

dbo.123 Does not exist

If I log into SSMS and type in the query window exec dbo.123 the procedure runs. Why is my code unable to see it? I am connecting to the proper server and database.

public DataSet RunSQLStoredProc()
{
    ebdb = new DataSet(); 
    SqlQueryBuilder = new StringBuilder();
    SqlQueryBuilder.Append("exec dbo.123 ");
    ebdb = DoThis(SqlQueryBuilder.ToString());
    return ebdb;
}

public DataSet DoThis(string sqlQuery)
{
    try
    {
        System.Configuration.ConnectionStringSettings connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer1"];

        using (SqlConnection conn = new SqlConnection(connstring.ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = sqlQuery;
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;

                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);                    
                adapter.Fill(ebdb);
                conn.Close();
            }                
        }

        return ebdb;
    }
    catch (Exception exception) { throw exception; }
}

Upvotes: 0

Views: 85

Answers (1)

Mohan
Mohan

Reputation: 71

change the statement from SqlQueryBuilder.Append("exec dbo.123 "); to SqlQueryBuilder.Append("123"); Also there is a space after 123 which is can create a problem so remove that space too.

Upvotes: 2

Related Questions