Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. DataTable and DataSet?

DataTable and DataSet?

Scheduled Pinned Locked Moved C#
helpdesignquestion
5 Posts 4 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    murali_utr
    wrote on last edited by
    #1

    $Exception {"Column 'Lab_SNo' does not belong to table LabBill."} System.Exception {System.ArgumentException} What’s wrong in this code? While clicking the button the above exception is raised. Any help me to this problem.

    public partial class Lab_frmLabBilling : System.Web.UI.Page
    {
    DataSet dsBill = new DataSet();
    DataTable dtBill = new DataTable("LabBill");

    protected void Page\_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {            
    
                InitializeDS();                 
            }          
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
        }
    }
    

    private void InitializeDS()
    {
    try
    {
    DataColumn dCol1 = new DataColumn("Lab_SNo",typeof(int));
    DataColumn dCol2 = new DataColumn("pk_lab_bill_id",typeof(int));
    DataColumn dCol3 = new DataColumn("GroupId",typeof(int));
    DataColumn dCol4 = new DataColumn("TestId",typeof(int));
    DataColumn dCol5 = new DataColumn("Lab_Test", typeof(string));
    DataColumn dCol6 = new DataColumn("Lab_Test_Cost", typeof(float));

            dtBill.Columns.Add(dCol1);
            dtBill.Columns.Add(dCol2);
            dtBill.Columns.Add(dCol3);
            dtBill.Columns.Add(dCol4);
            dtBill.Columns.Add(dCol5);
            dtBill.Columns.Add(dCol6);
            
            dsBill.Tables.Add(dtBill);
    
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
        }
    }
    

    protected void btnGInsert_Click(object sender, EventArgs e)
    {
    try
    {
    if (txtGroupName.Text == string.Empty)
    {
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", "Please Enter Group Name", false);
    return;
    }

            int iCount;
    
            iCount = dtBill.Columns.Count;
            iCount = dtBill.Rows.Count;            
    
            iCount++;
    
            DataRow drBill;
    
           
            drBill = dtBill.NewRow();
            drBill\["Lab\_SNo"\] = iCount;
            drBill\["pk\_lab\_bill\_id"\] = iCount;
            drBill\["GroupId"\] = int.Parse(Session\["LabGroupId"\].ToString());
            drBill\["TestId"\] = 0;
            drBill\["Lab\_T
    
    V S R 4 Replies Last reply
    0
    • M murali_utr

      $Exception {"Column 'Lab_SNo' does not belong to table LabBill."} System.Exception {System.ArgumentException} What’s wrong in this code? While clicking the button the above exception is raised. Any help me to this problem.

      public partial class Lab_frmLabBilling : System.Web.UI.Page
      {
      DataSet dsBill = new DataSet();
      DataTable dtBill = new DataTable("LabBill");

      protected void Page\_Load(object sender, EventArgs e)
      {
          try
          {
              if (!Page.IsPostBack)
              {            
      
                  InitializeDS();                 
              }          
          }
          catch (Exception ex)
          {
              Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
          }
      }
      

      private void InitializeDS()
      {
      try
      {
      DataColumn dCol1 = new DataColumn("Lab_SNo",typeof(int));
      DataColumn dCol2 = new DataColumn("pk_lab_bill_id",typeof(int));
      DataColumn dCol3 = new DataColumn("GroupId",typeof(int));
      DataColumn dCol4 = new DataColumn("TestId",typeof(int));
      DataColumn dCol5 = new DataColumn("Lab_Test", typeof(string));
      DataColumn dCol6 = new DataColumn("Lab_Test_Cost", typeof(float));

              dtBill.Columns.Add(dCol1);
              dtBill.Columns.Add(dCol2);
              dtBill.Columns.Add(dCol3);
              dtBill.Columns.Add(dCol4);
              dtBill.Columns.Add(dCol5);
              dtBill.Columns.Add(dCol6);
              
              dsBill.Tables.Add(dtBill);
      
          }
          catch (Exception ex)
          {
              Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
          }
      }
      

      protected void btnGInsert_Click(object sender, EventArgs e)
      {
      try
      {
      if (txtGroupName.Text == string.Empty)
      {
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", "Please Enter Group Name", false);
      return;
      }

              int iCount;
      
              iCount = dtBill.Columns.Count;
              iCount = dtBill.Rows.Count;            
      
              iCount++;
      
              DataRow drBill;
      
             
              drBill = dtBill.NewRow();
              drBill\["Lab\_SNo"\] = iCount;
              drBill\["pk\_lab\_bill\_id"\] = iCount;
              drBill\["GroupId"\] = int.Parse(Session\["LabGroupId"\].ToString());
              drBill\["TestId"\] = 0;
              drBill\["Lab\_T
      
      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      Did you debug? It might help if you indicate the faulty line. (PS: edit your post to highlight the line instead of replying to me. :) ) [EDIT]Please also put your code between code tags. (select code and press the code button)[/EDIT]

      V.

      1 Reply Last reply
      0
      • M murali_utr

        $Exception {"Column 'Lab_SNo' does not belong to table LabBill."} System.Exception {System.ArgumentException} What’s wrong in this code? While clicking the button the above exception is raised. Any help me to this problem.

        public partial class Lab_frmLabBilling : System.Web.UI.Page
        {
        DataSet dsBill = new DataSet();
        DataTable dtBill = new DataTable("LabBill");

        protected void Page\_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {            
        
                    InitializeDS();                 
                }          
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
            }
        }
        

        private void InitializeDS()
        {
        try
        {
        DataColumn dCol1 = new DataColumn("Lab_SNo",typeof(int));
        DataColumn dCol2 = new DataColumn("pk_lab_bill_id",typeof(int));
        DataColumn dCol3 = new DataColumn("GroupId",typeof(int));
        DataColumn dCol4 = new DataColumn("TestId",typeof(int));
        DataColumn dCol5 = new DataColumn("Lab_Test", typeof(string));
        DataColumn dCol6 = new DataColumn("Lab_Test_Cost", typeof(float));

                dtBill.Columns.Add(dCol1);
                dtBill.Columns.Add(dCol2);
                dtBill.Columns.Add(dCol3);
                dtBill.Columns.Add(dCol4);
                dtBill.Columns.Add(dCol5);
                dtBill.Columns.Add(dCol6);
                
                dsBill.Tables.Add(dtBill);
        
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
            }
        }
        

        protected void btnGInsert_Click(object sender, EventArgs e)
        {
        try
        {
        if (txtGroupName.Text == string.Empty)
        {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", "Please Enter Group Name", false);
        return;
        }

                int iCount;
        
                iCount = dtBill.Columns.Count;
                iCount = dtBill.Rows.Count;            
        
                iCount++;
        
                DataRow drBill;
        
               
                drBill = dtBill.NewRow();
                drBill\["Lab\_SNo"\] = iCount;
                drBill\["pk\_lab\_bill\_id"\] = iCount;
                drBill\["GroupId"\] = int.Parse(Session\["LabGroupId"\].ToString());
                drBill\["TestId"\] = 0;
                drBill\["Lab\_T
        
        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        I think the problem resides where you check the !Page.IsPostBack. When you press a button that runs on the server, the page will go into the Page_Load first and then in the event handler of the button. The state is NOT saved between postbacks so this means that the datatables do not exist yet. You could or recreate the table on each postback or save the information in a session object and take it out. Like this:

        if(!Page.IsPostBack){
        //create table here
        //in the end put in Session object.
        }
        else{
        //Reassign your table with the information from the Session object.
        }

        You might want to read up on the Asp.Net Page Life Cycle (see google). That will clarify things. hope this helps.

        V.

        1 Reply Last reply
        0
        • M murali_utr

          $Exception {"Column 'Lab_SNo' does not belong to table LabBill."} System.Exception {System.ArgumentException} What’s wrong in this code? While clicking the button the above exception is raised. Any help me to this problem.

          public partial class Lab_frmLabBilling : System.Web.UI.Page
          {
          DataSet dsBill = new DataSet();
          DataTable dtBill = new DataTable("LabBill");

          protected void Page\_Load(object sender, EventArgs e)
          {
              try
              {
                  if (!Page.IsPostBack)
                  {            
          
                      InitializeDS();                 
                  }          
              }
              catch (Exception ex)
              {
                  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
              }
          }
          

          private void InitializeDS()
          {
          try
          {
          DataColumn dCol1 = new DataColumn("Lab_SNo",typeof(int));
          DataColumn dCol2 = new DataColumn("pk_lab_bill_id",typeof(int));
          DataColumn dCol3 = new DataColumn("GroupId",typeof(int));
          DataColumn dCol4 = new DataColumn("TestId",typeof(int));
          DataColumn dCol5 = new DataColumn("Lab_Test", typeof(string));
          DataColumn dCol6 = new DataColumn("Lab_Test_Cost", typeof(float));

                  dtBill.Columns.Add(dCol1);
                  dtBill.Columns.Add(dCol2);
                  dtBill.Columns.Add(dCol3);
                  dtBill.Columns.Add(dCol4);
                  dtBill.Columns.Add(dCol5);
                  dtBill.Columns.Add(dCol6);
                  
                  dsBill.Tables.Add(dtBill);
          
              }
              catch (Exception ex)
              {
                  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
              }
          }
          

          protected void btnGInsert_Click(object sender, EventArgs e)
          {
          try
          {
          if (txtGroupName.Text == string.Empty)
          {
          Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", "Please Enter Group Name", false);
          return;
          }

                  int iCount;
          
                  iCount = dtBill.Columns.Count;
                  iCount = dtBill.Rows.Count;            
          
                  iCount++;
          
                  DataRow drBill;
          
                 
                  drBill = dtBill.NewRow();
                  drBill\["Lab\_SNo"\] = iCount;
                  drBill\["pk\_lab\_bill\_id"\] = iCount;
                  drBill\["GroupId"\] = int.Parse(Session\["LabGroupId"\].ToString());
                  drBill\["TestId"\] = 0;
                  drBill\["Lab\_T
          
          S Offline
          S Offline
          satalaj
          wrote on last edited by
          #4

          Hi, When you are calling the page for first time, the DataTable schema are getting initialized. When you click on button, you are skipping the datatable initialization by putting it into condition called !Page.IsPostBack. Better you keep the initialized table object in session and during post back access it from session object.

          1 Reply Last reply
          0
          • M murali_utr

            $Exception {"Column 'Lab_SNo' does not belong to table LabBill."} System.Exception {System.ArgumentException} What’s wrong in this code? While clicking the button the above exception is raised. Any help me to this problem.

            public partial class Lab_frmLabBilling : System.Web.UI.Page
            {
            DataSet dsBill = new DataSet();
            DataTable dtBill = new DataTable("LabBill");

            protected void Page\_Load(object sender, EventArgs e)
            {
                try
                {
                    if (!Page.IsPostBack)
                    {            
            
                        InitializeDS();                 
                    }          
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
                }
            }
            

            private void InitializeDS()
            {
            try
            {
            DataColumn dCol1 = new DataColumn("Lab_SNo",typeof(int));
            DataColumn dCol2 = new DataColumn("pk_lab_bill_id",typeof(int));
            DataColumn dCol3 = new DataColumn("GroupId",typeof(int));
            DataColumn dCol4 = new DataColumn("TestId",typeof(int));
            DataColumn dCol5 = new DataColumn("Lab_Test", typeof(string));
            DataColumn dCol6 = new DataColumn("Lab_Test_Cost", typeof(float));

                    dtBill.Columns.Add(dCol1);
                    dtBill.Columns.Add(dCol2);
                    dtBill.Columns.Add(dCol3);
                    dtBill.Columns.Add(dCol4);
                    dtBill.Columns.Add(dCol5);
                    dtBill.Columns.Add(dCol6);
                    
                    dsBill.Tables.Add(dtBill);
            
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", ex.Message, false);
                }
            }
            

            protected void btnGInsert_Click(object sender, EventArgs e)
            {
            try
            {
            if (txtGroupName.Text == string.Empty)
            {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "HMS", "Please Enter Group Name", false);
            return;
            }

                    int iCount;
            
                    iCount = dtBill.Columns.Count;
                    iCount = dtBill.Rows.Count;            
            
                    iCount++;
            
                    DataRow drBill;
            
                   
                    drBill = dtBill.NewRow();
                    drBill\["Lab\_SNo"\] = iCount;
                    drBill\["pk\_lab\_bill\_id"\] = iCount;
                    drBill\["GroupId"\] = int.Parse(Session\["LabGroupId"\].ToString());
                    drBill\["TestId"\] = 0;
                    drBill\["Lab\_T
            
            R Offline
            R Offline
            Rahul Rajat Singh
            wrote on last edited by
            #5

            Well, to start with. If you need datatable in as a member variable then on each postback it will be recreated(no state saved). So you will be needing a session or cache object to store the table's state. Have datatable created first time and then push the table in a session/cache object. on further postbacks first get the value from session/cache and then you are good to go. (do it in form_load as the other events will be fired after that)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups