system.data.dll中发生了'system.data.sqlclient.sqlexception'类型的异常,但未在用户代码中处理其他信息:找不到存储过程'spconti'。
问题描述:
Create Procedure SpConti
as
Begin
Select ContinentID,ContitnentName From [SampleDemo].[dbo].tableContinents
End
--****************************
Create Procedure SpGetCountryByContitnentID 1
@ContinentID int
as
Begin
Select CountryId,CountryName From [SampleDemo].[dbo].[tableCountries]
Where [ContinentId]= @ContinentID
End
--****************************
Create Procedure SpGetCityByCountryID 2
@ContryID int
as
Begin
Select [CityId],[CityName] From [SampleDemo].[dbo].[tblCities]
Where [CountryId]= @ContryID
End
C#
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlContinent.DataSource = GetData("SpConti", null);
ddlContinent.DataBind();
}
}
private DataSet GetData(string SPname, SqlParameter SPParameter)
{
string CS = ConfigurationManager.ConnectionStrings["SampleDemoConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(CS);
SqlDataAdapter da = new SqlDataAdapter(SPname,conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
if (SPParameter!=null)
{
da.SelectCommand.Parameters.Add(SPParameter);
}
DataSet DS = new DataSet();
da.Fill(DS);
return DS;
}
我的尝试:
我在行da.fill(DS)上收到错误-----错误=** System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常但是未在用户代码中处理
附加信息:找不到存储过程'SpConti'。**我试图修改第一个存储过程SpConti
What I have tried:
I am getting error on line da.fill(DS)----- Error= "**An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Could not find stored procedure 'SpConti'.**" me trying to to pring first stored procedure SpConti
答
您好,
尝试更改以下代码。
Hi,
Try changing your code like the following.
private DataSet GetData(string SPname, SqlParameter SPParameter)
{
string CS = ConfigurationManager.ConnectionStrings["SampleDemoConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(CS);
SqlCommand cmd = new SqlCommand("SPname", conn);
if (SPParameter!=null)
{
cmd.Parameters.Add(SPParameter);
}
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet DS = new DataSet();
da.Fill(DS);
return DS;
}
在此之前,请确保您的sp存在于数据库中。
谢谢,
Sisir Patro
Prior to this make sure that your sp exists in the database.
Thanks,
Sisir Patro