locked
C# using VS2015 - Having problem attempting to create a datatable RRS feed

  • Question

  • User-1999980185 posted

    Can anyone send me to a link that have a SIMPLE and COMPLETE code that create a DATATABLE  in a method, then create a DATAROW in the DATATABLE in another method and do a DATABIND in another method.   I am new in C# and I am having major problems with simple things.  Example I can not create a datatable in one method and create a datarow in another method.   Any examples would be appreciated.  Thanks in advance.

    Sunday, July 15, 2018 8:57 AM

Answers

  • User-1999980185 posted

    Vahid,  thank you very much.  That work.   I thank all who are getting me through the basic coding of C#.   Thanks for all your time and effort with a New Bee.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 15, 2018 8:26 PM

All replies

  • User-369506445 posted

    hi

    first, create a DataTable variable

     DataTable table=new DataTable();

    below method create columns for the DataTable, (in this sample create 4 columns)

    DataTable CreateColumn(DataTable table)
            {
                table.Columns.Add("column 1", typeof(int));
                table.Columns.Add("column 2", typeof(string));
                table.Columns.Add("column 3", typeof(string));
                table.Columns.Add("column 4", typeof(DateTime));
    
                return table;
            }

    below method create rows for the DataTable

    DataTable CreateRow(DataTable table)
            {
                //row 1
                table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                //row 2
                table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                //row 3
                table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                //row 4
                table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                //row 4
                table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    
                return table;
            }
    

    Bind to another method

    public void BindToAnotherMethod(DataTable table)
            {
    
                foreach (DataRow row in table.Rows)
                {
                    Response.Write(String.Format("column 1 : {0}  ,  column 2 : {1}   ,  column 3 : {2} <br />",
                        row[0], row[1], row[2]));
                }
            }

    and the complete code

    public ActionResult Index()
            {
    
                DataTable table = new DataTable();
                table = CreateColumn(table);
                table = CreateRow(table);
                BindToAnotherMethod(table);
    
                //DataTable table = GetTable();
    
                return View();
            }
    
    
    
            DataTable CreateColumn(DataTable table)
            {
                table.Columns.Add("column 1", typeof(int));
                table.Columns.Add("column 2", typeof(string));
                table.Columns.Add("column 3", typeof(string));
                table.Columns.Add("column 4", typeof(DateTime));
    
                return table;
            }
    
            DataTable CreateRow(DataTable table)
            {
                //row 1
                table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                //row 2
                table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                //row 3
                table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                //row 4
                table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                //row 4
                table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    
                return table;
            }
    
            public void BindToAnotherMethod(DataTable table)
            {
    
                foreach (DataRow row in table.Rows)
                {
                    Response.Write(String.Format("column 1 : {0}  ,  column 2 : {1}   ,  column 3 : {2} <br />",
                        row[0], row[1], row[2]));
                }
            }

    Sunday, July 15, 2018 9:23 AM
  • User753101303 posted

    Hi,

    It can help also to understand how it fails on your side. For now it looks like you have a "scope" issue: https://msdn.microsoft.com/en-us/library/ms973875.aspx (edit: ah it's for VB but this is the same principles for C#)

    Mybe you create a DataTable which is local to a method and then try to use that elsewhere ?

    Sunday, July 15, 2018 9:43 AM
  • User-1999980185 posted

    Thank you very much.   The only problem is, in my processing,  I  will not be able to create all the rows at the same time.   I have done this in VB many times.  My processing will look like this  Create the table, do some processing, at a later time create a ROW, do some processing, later create another ROW, do some more processing at a later time create another ROW, and I do not know how many ROWS I will need to create.  Sometime 5 rows, sometimes 10 rows, and later Bind.  What I mean is I do not know how many rows I will need to create, so I can not create them at the same time.  Some time 5 rows, some time 50 rows, and another time 25 rows then bind all together with one bind.  I will work on what you sent me and I appreciate it very much.  Thanks again.

    Sunday, July 15, 2018 1:07 PM
  • User-369506445 posted

    don't worry, you can change my code to below

    In this sample I have 3 separate methods, in Index1 I add two rows, in Index 2 I add 1 rows, and in Index3 I print the DataTable value

     

    static DataTable table=new DataTable();
    
            public ActionResult Index()
            {
                table = CreateColumn(table, "column 1", "column 2");
                table = CreateRow(table, "row 1 -col1", "row 1 -col2");
                table = CreateRow(table, "row 2 -col1", "row 2 -col2");
    
                return View();
            }
    
            public ActionResult Index2()
            {
                table = CreateRow(table, "row 3 -col1", "row 3 -col2");
                return View();
            }
            public ActionResult Index3()
            {
                BindToAnotherMethod(table);
                return View();
            }
    
    
    
    
    
    
    DataTable CreateColumn(DataTable table , string col1Name,string col2Name)
            {
                table.Columns.Add(col1Name, typeof(string));
                table.Columns.Add(col2Name, typeof(string));
    
                return table;
            }
    
            DataTable CreateRow(DataTable table , string col1value, string col2value)
            {
                table.Rows.Add(col1value, col2value);
    
                return table;
            }
    
            public void BindToAnotherMethod(DataTable table)
            { 
                foreach (DataRow row in table.Rows)
                {
                    Response.Write(String.Format("column 1 : {0}  ,  column 2 : {1}  <br />",
                        row[0], row[1]));
                }
            }

    now you can add your rows dynamically without you need to know about the rows

    Sunday, July 15, 2018 1:28 PM
  • User753101303 posted

    It's hard to figure out what is the problem. You can add rows as needed without any problem (at least during the same http query). IMO asking for samples is not always the better approach. Another option would be to be crystal clear about the problem you have so that we one could focus on your exact issue rather than providing a sample that might be unrelated to your issue.

    Or "some processing" are http queries ie you want to create a DataTable and then you have multiple http queries during which you'll possibly add other rows and you don't want to store them immediately in your db even with a "draft" flag  ?

    Sunday, July 15, 2018 1:40 PM
  • User-1999980185 posted

    What am I not understanding.  Below is my complete code.  I am getting two errors.  Please advise.   I know this is simple but this is my first C# coding.

    Errors below:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0103	The name 'Mytable' does not exist in the current context	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	45	Active
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0103	The name 'Mytable' does not exist in the current context	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	48	Active
    
    
    Code below:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    //
    using System.Data;
    using System.Data.Common;
    
    
    namespace FirstCWeb
    {
        public partial class FirstCWeb_PageOne : System.Web.UI.Page
        {
            string DDate;
            string DDesc;
    
            //DDate.Text = "2018/01/01";
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
                BuildTable();
            }
           
    
            public void BuildTable()        
            {
    
                DataTable Mytable = new DataTable();
                Mytable.Columns.Add("DDate", typeof(string));
                Mytable.Columns.Add("DDesc", typeof(string));
    
                Create_DateRow();       
    
            }
    
            public void Create_DateRow()
            {
    
                DataRow dr = Mytable.NewRow();        // Error here - it do not accept Mytable in this method
                dr["DDate"] = DDate;
                dr["DDesc"] = DDesc;
                Mytable.Rows.Add(dr);                 // Error here - it do not acce;t Mytable in this method
    
    
    
            }
    
    
        }
    }

    Sunday, July 15, 2018 3:22 PM
  • User-369506445 posted

    You have to define mytable as a global variable. Put below line outside the BuildTable method

    Try below code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    //
    using System.Data;
    using System.Data.Common;
    
    
    namespace FirstCWeb
    {
        public partial class FirstCWeb_PageOne : System.Web.UI.Page
        {
            string DDate;
            string DDesc;
    
            //DDate.Text = "2018/01/01";
    
    DataTable Mytable = new DataTable();
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
                BuildTable();
            }
           
    
            public void BuildTable()        
            {
    
                
                Mytable.Columns.Add("DDate", typeof(string));
                Mytable.Columns.Add("DDesc", typeof(string));
    
                Create_DateRow();       
    
            }
    
            public void Create_DateRow()
            {
    
                DataRow dr = Mytable.NewRow();        // Error here - it do not accept Mytable in this method
                dr["DDate"] = DDate;
                dr["DDesc"] = DDesc;
                Mytable.Rows.Add(dr);                 // Error here - it do not acce;t Mytable in this method
    
    
    
            }
    
    
        }
    }

    Sunday, July 15, 2018 3:44 PM
  • User-1999980185 posted

    Vahid,  thank you very much.  That work.   I thank all who are getting me through the basic coding of C#.   Thanks for all your time and effort with a New Bee.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 15, 2018 8:26 PM