如何在调用存储过程时清除错误?

时间:2011-06-11 05:17:33

标签: asp.net sql-server

我设计了我的网页但是我收到了以下错误。如何清除错误?

我的代码

 protected void btnSave_Click(object sender, EventArgs e)
 {
        Int32 st;
        int len = browseFilepath.PostedFile.ContentLength;
        byte[] pic = new byte[len];
        browseFilepath.PostedFile.InputStream.Read(pic, 0, len);

        SqlCommand Cmd = new SqlCommand();
        SqlConnection Cnn = new SqlConnection();
        string ConnectionString;
        ConnectionString = ConfigurationManager.ConnectionStrings["PhotostudioConnectionString"].ConnectionString;

        Cnn.ConnectionString = ConnectionString;
        if (Cnn.State != ConnectionState.Open)
            Cnn.Open();
        Cmd.Connection = Cnn;
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.CommandText = "sproc_Ins_PhotoSettingsDetails";
        Cmd.Parameters.Clear();
        // Cmd.Parameters.AddWithValue("@Id", txtBillNo.Text);
        Cmd.Parameters.AddWithValue("@Name", txtCName.Text);
        Cmd.Parameters.AddWithValue("@Phoneno", txtPhone.Text);
        Cmd.Parameters.AddWithValue("@Startdate", rdStartDate.SelectedDate);
        Cmd.Parameters.AddWithValue("@Enddate", rdEndDate.SelectedDate);
        Cmd.Parameters.Add("@Systemurl", SqlDbType.Image).Value = pic;
        SqlParameter Src = new SqlParameter("@FilePath", SqlDbType.NVarChar, 450);
        Src.Value = browseFilepath.PostedFile.FileName;
        Cmd.Parameters.Add(Src);
        Cmd.Parameters.AddWithValue("@Size", cmbSize.SelectedItem.Text  );
        Cmd.Parameters.AddWithValue("@Amount",txtAmt.Text);
        Cmd.Parameters.AddWithValue("@Extracopies", txtNoofcopies.Text);
        Cmd.Parameters.AddWithValue("@Examount",TextBox1.Text );
        Cmd.Parameters.AddWithValue("@Lamination", Ddl.SelectedItem.Text  );
        Cmd.Parameters.AddWithValue("@Laamount", txtlami.Text );
        Cmd.Parameters.AddWithValue("@Total", txtTot.Text );
        Cmd.Parameters.AddWithValue("@Grandtotal", txtgrandtot.Text );
        Cmd.Parameters.AddWithValue("@Paidamount", txtPaid.Text);
        Cmd.Parameters.AddWithValue("@Balance", txtbalance.Text );

        try
        {
            st = Convert.ToInt32(Cmd.ExecuteScalar());
        }
        catch (Exception ex)
        {
            throw new ApplicationException(
                "!!! An Error Occured While getting Data From Hdr_AccountType." + ex.Message);
            lblError.Visible = true;
            lblError.Text = "!!! An Error Occured While " + ex.Message.ToString();
            return;
        }

        Cmd.Dispose();

错误

  

从Hdr_AccountType获取数据时发生错误。将数据类型nvarchar转换为数字时出错。

我的存储过程:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[sproc_Ins_PhotoSettingsDetails]
    (
     --@Id as Numeric(18,0),
     @Name as Varchar(100),
     @Phoneno as Numeric(18,0),
     @Startdate as DateTime,
     @Enddate as  DateTime,
     @Systemurl as  Image,
     @FilePath as NVarchar(450),
     @Size as nvarchar(50),
     @Amount as numeric(18,0),
     @Extracopies as numeric(18,0),
     @Examount as numeric (18,0),
     @Lamination as numeric(18,0),
     @Laamount as numeric(18,0),
     @Total as numeric(18,0),
     @Grandtotal as numeric (18,0),
     @Paidamount as Numeric(18,0),
     @Balance as numeric (18,0)
    )     
AS
BEGIN
    INSERT INTO tblPhotosettingsMaster (Name, Phoneno, Startdate, Enddate, 
                                        Systemurl, FilePath, Size, Amount, 
                                        Extracopies, Examount, Lamination, Laamount,
                                        Total, Grandtotal, Paidamount, Balance)
    VALUES (@Name, @Phoneno, @Startdate, @Enddate,
            @Systemurl, @FilePath, @Size, @Amount,
            @Extracopies, @Examount, @Lamination, @Laamount,
            @Total, @Grandtotal, @Paidamount, @Balance)

    SELECT @@Identity
END

2 个答案:

答案 0 :(得分:3)

我认为您的问题是您将发送到的存储过程中的数据以文本形式发送(.Text = nvarchar),但您的SP假定数字输入参数。在大多数情况下,我认为SQL会将一串数字转换为数字,但我们假设您的txtgrandtot.Text实际上是“$ 123.23”,那么您将收到此错误。您的C#代码不会引发异常,它是由SQL Server引发的。

答案 1 :(得分:0)

您是否尝试过调试:

var ret = Cmd.ExecuteScalar();

控制存储的返回值?这是一个数字吗?

<强>更新

我同意豪克曼!检查输入参数,查找应该只是数字的字符值!