Trey Copeland
Trey Copeland

Reputation: 3527

How can I make my variable used throughout my application?

How can I get the returned memberID variable inserted back in the other SQL stored proc?

  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");
    }
}

Upvotes: 1

Views: 470

Answers (3)

user596075
user596075

Reputation:

First off, I would recommend that you split up your code a little bit. You are doing quite a bit of logic (including data access and probably some domain logic) inside of your control event handler.

But the immediate solution to your problem is to delare memberID outside of your try/catch block.

 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

The above way you would have memberID within scope for that whole event handler, which in turn would allow you to use that variable throughout the scope of the handler.

Upvotes: 0

marc_s
marc_s

Reputation: 755481

Just define int memberID outside the first try....catch block so that it'll be visible inside the second try...catch block:

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");
    }
}

Upvotes: 3

Fredou
Fredou

Reputation: 20140

change it like this, move the declaration of the variable out of the { }, before it

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");
}

Upvotes: 1

Related Questions