2011-06-11 64 views
0

我設計了我的網頁,但是我得到下面的錯誤。如何清除錯誤?如何在調用存儲過程時清除錯誤?

我的代碼

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] 
    (
    [email protected] 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 
+0

註釋try/catch並查看拋出的異常。 – iandayman 2011-06-11 05:52:28

回答

3

我認爲你的問題是你發送的數據爲你的存儲過程是以文本(.Text = nvarchar)的形式發送的,但是你的SP假定數字輸入參數。在大多數情況下,我認爲SQL會自動將一串數字轉換爲數字,但假設您的txtgrandtot.Text實際上是「$ 123.23」,那麼您會得到此錯誤。這個異常不是由你的C#代碼引發的,它是由SQL Server引發的。

0

你試圖調試:

var ret = Cmd.ExecuteScalar(); 

來控制存儲的返回值?這是一個數字嗎?

更新

我Haukman同意!檢查輸入參數尋找一個字符值,其中應該只有數字!

+0

我是初學者如何更改請修改 – Malliga 2011-06-11 05:29:57