2012-01-04 65 views
1

如何獲取返回的memberID變量插回到其他SQL存儲過程中?如何使我的應用程序中使用我的變量?

protected void imgBtnActivate_onClick(object sender, EventArgs e) 
{ 

    // Inserts Member record 
    try 
    { 
     Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]); 

     DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p"); 
     db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text); 
     db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text); 
     db.ExecuteNonQuery(dbCommand); 
     int memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString()); 

} 
    catch (Exception ex) 
    { 
     ExceptionPolicy.HandleException(ex, "Notify Policy"); 
    } 


    // Insert Member Email 
    try 
     { 

Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]); 

      DbCommand dbCommand = db.GetStoredProcCommand("[ins_MemberEmail_p]"); 
      dbCommand.Transaction = dbTran; 

      db.AddInParameter(dbCommand, "MemberID", DbType.Int32, memberID); 
      db.AddInParameter(dbCommand, "SourceID", DBType.In32, 33); 
      db.AddInParameter(dbCommand, "EmailAddress", DbType.String, EncryptedEmailAddress); 

    } 
    catch (Exception ex) 
    { 
     ExceptionPolicy.HandleException(ex, "Notify Policy"); 
    } 
} 

回答

3

只要定義int memberID第一try....catch外塊,以便它會在裏面可見第二個try...catch塊:

protected void imgBtnActivate_onClick(object sender, EventArgs e) 
{ 
    // define it here 
    int memberID = -1: 

    // Inserts Member record 
    try 
    { 
     ...... 
     db.ExecuteNonQuery(dbCommand); 
     memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString()); 
    } 
    catch (Exception ex) 
    { 
     ExceptionPolicy.HandleException(ex, "Notify Policy"); 
    } 

    // Insert Member Email 
    try 
    { 
     ...... 
     // then it'll be visible and useable here! 
     db.AddInParameter(dbCommand, "MemberID", DbType.Int32, memberID); 
     ..... 
    } 
    catch (Exception ex) 
    { 
     ExceptionPolicy.HandleException(ex, "Notify Policy"); 
    } 
} 
1

改變它像這樣,將變量的聲明出來的{},才

protected void imgBtnActivate_onClick(object sender, EventArgs e) 
{ 

int memberID; 

// Inserts Member record 
try 
{ 
    Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]); 

    DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p"); 
    db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text); 
    db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text); 
    db.ExecuteNonQuery(dbCommand); 
    memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString()); 

} 
catch (Exception ex) 
{ 
    ExceptionPolicy.HandleException(ex, "Notify Policy"); 
} 
0

首先,我會建議你分開你的代碼一點點。在控制事件處理程序中,您正在執行相當多的邏輯(包括數據訪問和可能的某些域邏輯)。

但是,您的問題的直接解決方案是在您的try/catch塊外部延遲memberID

protected void imgBtnActivate_onClick(object sender, EventArgs e) 
{ 
    int memberID; 
    // Inserts Member record 
    try 
    { 
     Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]); 

     DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p"); 
     db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text); 
     db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text); 
     db.ExecuteNonQuery(dbCommand); 
     memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString()); 

    } 
// .... so on and so forth 

你將不得不範圍爲整個事件處理程序,這反過來將允許您使用在整個處理程序的範圍變量內memberID上述方式。

相關問題