2016-08-22 18 views
0

工作我的提交按鈕動作:更新用但不能從UI

protected void submit_Click(object sender, EventArgs e) 

    { 
     SqlConnection con = new SqlConnection("Data Source=172.25.192.80;Initial Catalog=DB01HMS001;User ID=pj01hms001;Password=tcshyd"); 
     con.Open(); 


     SqlCommand cmd = new SqlCommand("usp_team5_customerupdate", con); 
     cmd.CommandType = CommandType.StoredProcedure; 

     cmd.Parameters.AddWithValue("@Title", Convert.ToString(DropDownList1.SelectedValue)); 

     cmd.Parameters.AddWithValue("@Lastname",lstname.Text); 

     cmd.Parameters.AddWithValue("@StreetAddress", streetadd.Text); 

     cmd.Parameters.AddWithValue("@EmailAddress", Eaddress.Text); 
     cmd.Parameters.AddWithValue("@State", txtstate.Text); 
     cmd.Parameters.AddWithValue("@TownorCity",towncity.Text); 
     cmd.Parameters.AddWithValue("@Zipcode", Convert.ToInt32(txtzipcode.Text)); 
     cmd.Parameters.AddWithValue("@MobileNumber",Convert.ToInt64(mblenum.Text)); 

     cmd.Parameters.AddWithValue("@PhoneNumber", Convert.ToInt64(phnenum.Text)); 
     cmd.Parameters.AddWithValue("@CompanyName", cname.Text); 
     cmd.Parameters.AddWithValue("@OfficeAddress", Oaddress.Text); 
     cmd.Parameters.AddWithValue("@custid", Convert.ToInt64(Session["cusid"])); 
     Response.Write(Convert.ToString(Session["cusid"])); 
     int a = cmd.ExecuteNonQuery(); 
     if (a == 1) { 

      Response.Write("Success"); 
     } 
     else 
     { 
      Response.Write("Failure"); 
     } 

     con.Close(); 
    } 

我的表架構:

CREATE TABLE [dbo].[team5_customerprofile] (
[CustomerProfileID]  INT   IDENTITY (1001, 1) NOT NULL, 
[CustomerID]    AS   (CONVERT([bigint],CONVERT([varchar],[CustomerProfileID],0)+CONVERT([varchar](15),getdate(),(112)),0)), 
[Password]    VARCHAR (20) NULL, 
[Title]     VARCHAR (20) NULL, 
[Firstname]    VARCHAR (20) NULL, 
[Lastname]    VARCHAR (20) NULL, 
[DateOfBirth]    DATE   NULL, 
[Gender]     BIT   NULL, 
[StreetAddress]   VARCHAR (100) NULL, 
[Nationality]    VARCHAR (20) NULL, 
[State]     VARCHAR (30) NULL, 
[TownorCity]    VARCHAR (50) NULL, 
[Zipcode]     INT   NULL, 
[MobileNumber]   BIGINT  NULL, 
[AlternatePhone]   BIGINT  NULL, 
[PhoneNumber]    BIGINT  NULL, 
[EmailAddress]   VARCHAR (30) NULL, 
[CompanyName]    VARCHAR (50) NULL, 
[OfficeAddress]   VARCHAR (100) NULL, 
[TotalBonusMilesAcheived] INT   DEFAULT ((0)) NULL, 
CHECK ([Title]='Mrs.' OR [Title]='Miss.' OR [Title]='Mr.'), 
CHECK ([Nationality]='Indian'), 
CHECK (len([Zipcode])=(6)) 
); 

我寫了一個存儲過程,如下圖所示,該更新表在外部執行它,但它不會更新表,當我嘗試通過用戶界面輸入值,然後按提交。

CREATE procedure usp_team5_customerupdate 
    (
    @Title varchar(20), 
    @Lastname varchar(20), 
    @StreetAddress varchar(100), 
    @State varchar(30), 
    @TownorCity varchar(50), 
    @Zipcode int, 
    @MobileNumber bigint, 
    @PhoneNumber bigint, 
    @EmailAddress varchar(30), 
    @CompanyName varchar(50), 
    @OfficeAddress varchar(100), 
    @custid nvarchar(20) 
    ) 
    as  
    begin 
    update team5_customerprofile 
    set 
    [email protected],[email protected],  
    [email protected], 
    [State][email protected], 
    [email protected],  
    [email protected], 
    [email protected], 
    [email protected],  
    [email protected], 
    [email protected],  
    [email protected] 
    where 
    [email protected] 
    end 
+0

你得到一個異常或錯誤? –

+0

沒有例外,沒有錯誤。沒有。即使「ExecuteNonQuery()」返回1也表示查詢成功執行。 「成功」也正在顯示。即使在那之後,也沒有數據庫更新完成。 :( –

+0

嘗試在您的更新語句周圍創建事務和提交事務。事務可能已關閉 –

回答

0

變化在存儲過程中的數據類型爲CustId相匹配的列數據類型是BigInt

@custid nvarchar(20) 

而且在你的代碼只是通過CustId無需任何轉換。

最後,您可以放置​​一個調試器並替換每個變量的代碼中的值,並在SQL查詢窗口中使用它並運行以下代碼以調試錯誤。

DECLARE @qry NVARCHAR(4000); 
SET @qry = ' 
     UPDATE team5_customerprofile 
     set 
     Title=''' + @Title + ''' 
     where 
     CustomerID=' + @custid + ' 
    end'; 
PRINT @qry; 

您可以包含其他列,以便在運行時可以檢查問題出在哪裏。

0
Create procedure usp_team5_customerupdate   
( 
@Title varchar(20), 
@Lastname varchar(20), 
@StreetAddress varchar(100), 
@State varchar(30), 
@TownorCity varchar(50), 
@Zipcode int =0, 
@MobileNumber bigint , 
@PhoneNumber bigint, 
@EmailAddress varchar(30), 
@CompanyName varchar(50), 
@OfficeAddress varchar(100), 
@custid nvarchar(20) 
) 
AS 
BEGIN 
IF Exists(SELECT * FROM team5_customerprofile WHERE [email protected]) 
BEGIN 
    UPDATE team5_customerprofile 
SET 
[email protected],[email protected], 
[email protected], 
[State][email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 
WHERE [email protected] 
END 
    ELSE 
    BEGIN 
      PRINT 'Invalid Customer Id '''[email protected]+'''' 
    END 
END 
0

你寫的代碼是正確的和存儲的過程也是正確的校驗連接字符串和運行的SQL事件探查器檢查所有的參數,如果有任何轉換問題檢查了所有......

嘗試一次改變這一程序,如果u得到任何錯誤,而執行該代碼讓我知道...

CREATE procedure usp_team5_customerupdate 

@Title varchar(20), 
@Lastname varchar(20), 
@StreetAddress varchar(100), 
@State varchar(30), 
@TownorCity varchar(50), 
@Zipcode int, 
@MobileNumber bigint, 
@PhoneNumber bigint, 
@EmailAddress varchar(30), 
@CompanyName varchar(50), 
@OfficeAddress varchar(100), 
@custid nvarchar(20) 

as  
begin 
IF Exists(SELECT * FROM team5_customerprofile WHERE [email protected]) 
BEGIN 
update team5_customerprofile 
set 
[email protected],[email protected],  
[email protected], 
[email protected], 
[email protected],  
[email protected], 
[email protected], 
[email protected],  
[email protected], 
[email protected],  
[email protected] 
where 
[email protected] 

end 
else 
print 'Error' 
end 

end 
0

我沒有看到與查詢字符串一起傳遞的參數,即

SqlCommand cmd = new SqlCommand("usp_team5_customerupdate", con); 

嘗試傳遞參數,如:

執行通過按鈕腳本時點擊
SqlCommand cmd = new SqlCommand("usp_team5_customerupdate @Title,@Lastname..(So on)", con);