ASP.NET(C#) 在使用存储过程时捕获异常(sql 数据库)
ASP.NET(C#) catching exception while using stored procedure (sql database)
代码如下...我尝试使用调试点显示:
Procedure or function 'usp_select_legal1_data' expects parameter '@tower', which was not supplied.
C#代码:
try {
LegalView.ActiveViewIndex = 2;
String tw2 = TextBox3.Text;
SqlDataSource SqlDataSource2 = new SqlDataSource();
SqlDataSource2.ID = "SqlDataSource2";
this.Page.Controls.Add(SqlDataSource2);
SqlDataSource2.ConnectionString = System.Configuration.ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
SqlDataSource2.SelectParameters.Add("@tower", tw2);
SqlDataSource2.SelectCommand = "usp_select_legal1_data";
GVCaseTowerWise.DataSource = SqlDataSource2;
GVCaseTowerWise.DataBind();
if (GVCaseTowerWise.Rows.Count == 0) {
ScriptManager.RegisterClientScriptBlock(
this,
this.GetType(),
"alertMessage",
"alert('No cases for this tower exist in the database')",
true);
}
} catch (Exception ex) {
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"alertMessage", "alert('error while getting data')", true);
}
这是我的存储过程:
ALTER PROCEDURE [dbo].[usp_select_legal1_data]
@tower nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT distinct
[tower_to]
,[tower_from]
,[sy_no]
FROM [dbo].[legal1]
WHERE ((tower_to = @tower) or (tower_from = @tower))
END
改变
SqlDataSource2.SelectParameters.Add("@tower", tw2);
到
SqlDataSource2.SelectParameters.Add("tower", tw2);
问题出在@,因为参数名是tower。请参阅下面的 MSDN 文档
https://msdn.microsoft.com/en-us/library/f58z9c1a(v=vs.110).aspx
我找到了问题的答案。问题在于定义存储过程。它仍然将该过程视为文本命令。这是我用过的
SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
现在可以正常使用了。
代码如下...我尝试使用调试点显示:
Procedure or function 'usp_select_legal1_data' expects parameter '@tower', which was not supplied.
C#代码:
try {
LegalView.ActiveViewIndex = 2;
String tw2 = TextBox3.Text;
SqlDataSource SqlDataSource2 = new SqlDataSource();
SqlDataSource2.ID = "SqlDataSource2";
this.Page.Controls.Add(SqlDataSource2);
SqlDataSource2.ConnectionString = System.Configuration.ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
SqlDataSource2.SelectParameters.Add("@tower", tw2);
SqlDataSource2.SelectCommand = "usp_select_legal1_data";
GVCaseTowerWise.DataSource = SqlDataSource2;
GVCaseTowerWise.DataBind();
if (GVCaseTowerWise.Rows.Count == 0) {
ScriptManager.RegisterClientScriptBlock(
this,
this.GetType(),
"alertMessage",
"alert('No cases for this tower exist in the database')",
true);
}
} catch (Exception ex) {
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"alertMessage", "alert('error while getting data')", true);
}
这是我的存储过程:
ALTER PROCEDURE [dbo].[usp_select_legal1_data]
@tower nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT distinct
[tower_to]
,[tower_from]
,[sy_no]
FROM [dbo].[legal1]
WHERE ((tower_to = @tower) or (tower_from = @tower))
END
改变
SqlDataSource2.SelectParameters.Add("@tower", tw2);
到
SqlDataSource2.SelectParameters.Add("tower", tw2);
问题出在@,因为参数名是tower。请参阅下面的 MSDN 文档 https://msdn.microsoft.com/en-us/library/f58z9c1a(v=vs.110).aspx
我找到了问题的答案。问题在于定义存储过程。它仍然将该过程视为文本命令。这是我用过的
SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
现在可以正常使用了。