Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

when i build the below code . I got the error message dTable does not exist. So, where should i write the dTable for which it will not display the error.

when i build the below code i got the error message ::

Error      1      The name 'dTable' does not exist in the current context      D:\trial3-TierArchitecture\List.aspx.cs      56      16      D:\trial3-TierArchitecture\

How to solve the error.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
 
    }
    private DataTable BindGrid()
    {
 
        PersonBAL3 p = new PersonBAL3();
 
 
 
        try
        {
 
            DataTable dTable = p.Load();
 
            GridView1.DataSource = dTable;
 
            GridView1.DataBind();
 
        }
 
        catch (Exception ee)
        {
 
            lblMessage.Text = ee.Message.ToString();
 
        }
 
        finally
        {
 
            p = null;
 
        }
 
 
 
        return dTable;
 
    }
 
 
 
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
 
 
 
 
 
        // instantiate BAL
 
        PersonBAL3 pBAL = new PersonBAL3();
 
        try
        {
 
            pBAL.Delete(personID);
 
 
 
            lblMessage.Text = "Record Deleted Successfully.";
 
        }
 
        catch (Exception ee)
        {
 
            lblMessage.Text = ee.Message.ToString();
 
        }
 
        finally
        {
 
            pBAL = null;
 
        }
 
 
 
        GridView1.EditIndex = -1;
 
        // Refresh the list
 
        BindGrid();
 
    }
    protected void EditRecord(object sender, GridViewEditEventArgs e)
    {
 
    }
    protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
 
        int intResult = 0;
 
        GridViewRow row = GridView1.Rows[e.RowIndex];
 
 
 
        TextBox tFN = (TextBox)row.FindControl("txtFName");
 
        TextBox tLN = (TextBox)row.FindControl("txtLName");
 
        TextBox tAge = (TextBox)row.FindControl("txtAge");
 
 
 
        // instantiate BAL
 
        PersonBAL3 pBAL = new PersonBAL3();
 
 
 
        try
        {
 
            intResult = pBAL.Update(personID, tFN.Text, tLN.Text, int.Parse(tAge.Text));
 
            if (intResult > 0)
 
                lblMessage.Text = "Record Updated Successfully.";
 
            else
 
                lblMessage.Text = "Record couldn't updated";
 
        }
 
        catch (Exception ee)
        {
 
            lblMessage.Text = ee.Message.ToString();
 
        }
        finally
        {
 
            pBAL = null;
 
        }
 
 
 
        GridView1.EditIndex = -1;
 
        // Refresh the list
 
        BindGrid();
 
    }
}

Open in new window

0
Tapan Pattanaik
Asked:
Tapan Pattanaik
  • 2
1 Solution
 
p_davisCommented:
instantiate the datatable outside of the try block
0
 
p_davisCommented:

private DataTable BindGrid()
    {
 
        PersonBAL3 p = new PersonBAL3();
        DataTAble dTable = new DataTable(); //instantiate here.
 
 
        try
        {
 
            dTable = p.Load();
 
            GridView1.DataSource = dTable;
 
            GridView1.DataBind();
 
        }
 
        catch (Exception ee)
        {
 
            lblMessage.Text = ee.Message.ToString();
 
        }
 
        finally
        {
 
            p = null;
 
        }
 
 
 
        return dTable;
 
    }

Open in new window

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now