.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 797
Number of posts: 1359

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
DataTable Reference problem in c# Posted by larrycolvin on 5 Jun 2003 at 7:40 AM
I am having a problem with a DataTable object reference. I get the error message "System.NullReferenceException: Object reference not set to an instance of an object." on line 51. The Function1 works fine, the problem in only in Function2. Can anyone help the rookie out?

1 namespace Test
2 {
3 public class TestClass : System.Web.UI.Page
4 {
5 public DataSet MyDataSet;
6 public DataTable MyDataTable1;
7 public DataTable MyDataTable2;
8
9
10 public void Function1()
11 {
12 MyDataSet = new DataSet();
13 MyDataTable1 = MyDataSet.Tables.Add("DataTable1");
14
15 MyDataTable1.Columns.Add("Col1", typeof(Decimal));
16 MyDataTable1.Columns.Add("Col2", typeof(String));
17
18 MyDataTable2 = MyDataSet.Tables.Add("DataTable2");
19
20 MyDataTable2.Columns.Add("Col1", typeof(Decimal));
21 MyDataTable2.Columns.Add("Col1", typeof(String));
22
23 string CMDStr = "SQL Statement";
24
25 SQLConn = new SqlConnection("Connection String");
26
27 SqlCommand SQLCmd = new SqlCommand(CMDStr, SQLConn);
28
29 SQLConn.Open();
30
31 SqlDataReader SQLReader = SQLCmd.ExecuteReader();
32
33 DataRow MyRow1;
34
35 while (SQLReader.Read())
36 {
37 MyRow1 = dtAvailableElements.NewRow();
38 MyRow1["Col1"] = SQLReader.GetValue(0);
39 MyRow1["Col2"] = SQLReader.GetString(1);
40 dtAvailableElements.Rows.Add(MyRow1);
41 }
42
43 SQLReader.Close();
44 SQLConn.Close();
45 }
46
47 public void Function2()
48 {
49 DataRow MyRow2;
50
51 MyRow2 = MyDataTable2.NewRow();
52 MyRow2["ElementCode"] = 45;
53 MyRow2["ElementName"] = txtFieldName.Text;
54 MyDataTable2.Rows.Add(MyRow2);
55 }
56 }
57 }





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.