problem with table datasource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jany
    New Member
    • Sep 2007
    • 10

    #1

    problem with table datasource

    hi everybody,
    iam new to dotnet..
    iam trying to give table as the datasource to the datagrid ,but iam getting an error like object ref.is not set to instance of an object...at the line r=dt.NewRow();
    and i have no bound columns in my datagrid.my intension is to add the data entered in the textboxes to the grid though the datatable;
    what is the error in my code.........
    please help me..

    protected DataTable dt = new DataTable();
    DataRow r;
    r = dt.NewRow();
    r[0] = TextBox1.Text;
    r[1] = TextBox2.Text;
    r[2] = TextBox3.Text;
    int i = DataGrid1.Items .Count;
    dt.Rows.InsertA t (r,i );
    dt.AcceptChange s();
    dt.GetChanges() ;
    DataGrid1.DataS ource = dt;
    DataGrid1.DataB ind();
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    moved to .NET forum

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Code:
      protected DataTable dt = new DataTable();
      DataRow r;
      r = dt.NewRow();
      I think for .NewRow() to work, you have to have some concept of what the row would contain. And by that I mean you need to have columns defined before a .NewRow() can return a row.

      Comment

      • mikeyeli
        New Member
        • Oct 2007
        • 63

        #4
        Originally posted by Plater
        Code:
        protected DataTable dt = new DataTable();
        DataRow r;
        r = dt.NewRow();
        I think for .NewRow() to work, you have to have some concept of what the row would contain. And by that I mean you need to have columns defined before a .NewRow() can return a row.
        Yup, the row is created based on the table schema, that is the reason why the NewRow() function is in the DataTable class, so like plater said, just add the columns first.

        Besides its easy to index a column in a row according to a name

        C#:
        Code:
                          .
                          .
                          .
        DataColumn column = new DataColumn("A");
        DataTable table = new DataTable();
        table.Columns.Add(column);
        DataRow row = table.NewRow();
        row["A"] = "Something";
        table.Rows.Add(row);
                          .
                          .
        someGrid.DataSource = table;
                          .
                          .
                          .

        Comment

        • jany
          New Member
          • Sep 2007
          • 10

          #5
          Originally posted by mikeyeli
          Yup, the row is created based on the table schema, that is the reason why the NewRow() function is in the DataTable class, so like plater said, just add the columns first.

          Besides its easy to index a column in a row according to a name

          C#:
          Code:
                            .
                            .
                            .
          DataColumn column = new DataColumn("A");
          DataTable table = new DataTable();
          table.Columns.Add(column);
          DataRow row = table.NewRow();
          row["A"] = "Something";
          table.Rows.Add(row);
                            .
                            .
          someGrid.DataSource = table;
                            .
                            .
                            .


          thanks for your reply mikeyeli .....
          iam using the code this way....




          public partial class tbgd : System.Web.UI.P age
          {
          protected DataTable dt = new DataTable();
          DataRow dr;
          protected void Page_Load(objec t sender, EventArgs e)
          {
          if (!Page.IsPostBa ck)
          {
          dt.Columns.Add( "Name", typeof(string)) ;
          dt.Columns.Add( "Id", typeof(int));
          }
          }

          protected void submit_Click(ob ject sender, EventArgs e)
          {
          dr = dt.NewRow();
          dr["Name"] = "control value";
          dr["Id"] = 12345;
          dt.Rows.Add(dr) ;

          DataGrid1.DataS ource = dt;
          DataGrid1.DataB ind();
          }
          }


          but iam getting column "Name" does not belongs to the table....
          please tell me what is the problem with this code

          Comment

          • mzmishra
            Recognized Expert Contributor
            • Aug 2007
            • 390

            #6
            Why you are adding the columns inside
            if (!Page.IsPostBa ck)

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #7
              Originally posted by jany
              thanks for your reply mikeyeli .....
              iam using the code this way....




              public partial class tbgd : System.Web.UI.P age
              {
              protected DataTable dt = new DataTable();
              DataRow dr;
              protected void Page_Load(objec t sender, EventArgs e)
              {
              if (!Page.IsPostBa ck)
              {
              dt.Columns.Add( "Name", typeof(string)) ;
              dt.Columns.Add( "Id", typeof(int));
              }
              }

              protected void submit_Click(ob ject sender, EventArgs e)
              {
              dr = dt.NewRow();
              dr["Name"] = "control value";
              dr["Id"] = 12345;
              dt.Rows.Add(dr) ;

              DataGrid1.DataS ource = dt;
              DataGrid1.DataB ind();
              }
              }


              but iam getting column "Name" does not belongs to the table....
              please tell me what is the problem with this code
              I think the field name will be case sensitive. Check if there is anything more to that field name (copy paste the field name)

              Comment

              • jany
                New Member
                • Sep 2007
                • 10

                #8
                Originally posted by mzmishra
                Why you are adding the columns inside
                if (!Page.IsPostBa ck)


                because each time when the page postedback a new table is being created ,but i want to add the rows to the previous table rows,and the new row should be displayed along with the prev. one.....

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  That will not happen if you are creating the DataTable over and over again when the page_load happens.

                  Comment

                  • mikeyeli
                    New Member
                    • Oct 2007
                    • 63

                    #10
                    Originally posted by jany
                    because each time when the page postedback a new table is being created ,but i want to add the rows to the previous table rows,and the new row should be displayed along with the prev. one.....
                    Programming web application is a little different than programming windows applications.

                    you should dedicate some time on reading about a webpage lifecycle. msdn has some good articles about this, that are very helpfull.

                    anyways, if what you want is, that that DataTable persists over postbacks, a not so clean way to do this, is simply making the data table static.

                    Comment

                    Working...