lodun
lodun

Reputation:

asp.net stored procedure problem

Why this code don't work,when i want run this code vwd 2008 express show me this error message:Invalid object name 'answers'.

this is my ascx.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
public partial class odgl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString =
  @"SANATIZEDSTRING!!!!";


        using (SqlConnection cn = new SqlConnection(connectionString))
        {


    using (SqlCommand dohvati = new SqlCommand("dbo.get_answers",cn)) {


        dohvati.CommandType = CommandType.StoredProcedure;
        SqlParameter izracun = new SqlParameter("@count", SqlDbType.Int);
        izracun.Direction = ParameterDirection.Output;

        dohvati.Parameters.Add(izracun);

        cn.Open();


        dohvati.ExecuteNonQuery();
        int count = Int32.Parse(dohvati.Parameters["@count"].Value.ToString());

        Response.Write(count.ToString());


        cn.Close();

    }

}

    }
}

and this is my stored procedure :

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[get_answers]
@ukupno int output


as
select @count= (SELECT COUNT(*) FROM answers)
go

Upvotes: 0

Views: 133

Answers (4)

James Alexander
James Alexander

Reputation: 6302

Make sure you've specified the correct DB to connect to in the connection string

Upvotes: 1

Oded
Oded

Reputation: 498904

Looks like you don't have an answers table in your dbo schema of the estudent_pioo database on the xxxxx\PADME server.

And the stored procedure you posted is would not even run. Perhaps you meant:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[get_answers]
  @count int output

as
  select @count= (SELECT COUNT(*) FROM answers)
go

Upvotes: 3

n8wrl
n8wrl

Reputation: 19765

Is 'answers' a table or view in the same DB as your stored procedure? Try running the stored procedure directly from SQL.

Looks like you are also going to have problems with the parameter. Should be @count?

Upvotes: 0

Daniel
Daniel

Reputation: 455

The problem seems to exist in your procedure... Are you sure that Answer table exist and belongs to dbo schema?

Upvotes: 1

Related Questions