Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending new record

Status
Not open for further replies.
Mar 20, 2003
103
AU
I am builidng a web form in asp.net.
Theres one registration page and one thankyou page. When user submit a postback will occur. I have trouble capturing the data. Could someone please have a look at the script and tell me how to solve it? thanks.

Heres my script:

if (IsPostBack)
{
SqlConnection myDOTNET = new SqlConnection();
myDOTNET.ConnectionString = "data source=10.0.0.1;initial catalog=test;user id=ass;password=;";
myDOTNET.Open();
DataSet custDS = new DataSet("DOTNET_TEST");
DataRow anyRow = custDS.Tables["DOTNET_TEST"].NewRow();
anyRow["FNAME"] = "Jay";
custDS.Tables["DOTNET_TEST"].Rows.Add(anyRow);
myDOTNET.Close();
Response.Redirect("Thankyou.aspx");
}

After submitting the following error is shown.

error occurs here:
DataRow anyRow = custDS.Tables["DOTNET_TEST"].NewRow();

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Hi borgkling2003,


You should add table to the dataset but U r not doing that.
what u should do is create a DataTable object and assign the table to it.

Add this table to the dataset object.
ie custDS.Add.Tables(<tableName>);

This may solve ur problem.

I did not try this but I am sending another sample code which explains how to create a table and insert data into it. This may help U

if (IsPostBack)
{
SqlConnection myDOTNET = new SqlConnection();
myDOTNET.ConnectionString = "data source=10.0.0.1;initial catalog=test;user id=ass;password=;";
myDOTNET.Open();
DataSet custDS = new DataSet("DOTNET_TEST");

//custDS.Add.Tables(<tableName>);

DataRow anyRow = custDS.Tables["DOTNET_TEST"].NewRow();
anyRow["FNAME"] = "Jay";
custDS.Tables["DOTNET_TEST"].Rows.Add(anyRow);
myDOTNET.Close();
Response.Redirect("Thankyou.aspx");
}


//.............
//..Sample code
DataSet myDataSet = null;
// Create a new DataTable.
System.Data.DataTable myDataTable = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn myDataColumn;
DataRow myDataRow;

// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "id";
myDataColumn.ReadOnly = true;
myDataColumn.Unique = true;
// Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn);

// Create second column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "ParentItem";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ParentItem";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
// Add the column to the table.
myDataTable.Columns.Add(myDataColumn);

// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = myDataTable.Columns["id"];
myDataTable.PrimaryKey = PrimaryKeyColumns;

// Instantiate the DataSet variable.
myDataSet = new DataSet();
// Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable);

// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i<= 2; i++)
{
myDataRow = myDataTable.NewRow();
myDataRow["id"] = i;
myDataRow["ParentItem"] = "ParentItem " + i;
myDataTable.Rows.Add(myDataRow);
}

//..Add data to DataGrid
dataGrid1.SetDataBinding(myDataSet,"ParentTable");



Regards,
R.Venkatesh
MakeLogic
venkat@makelogicmldb.com
 
I don't know why you put that code when IsPostBack() but the error occurs at that line because there is no Datatable object with the "DOTNET_TEST" name in the DataSet;
As the code is presented , you should create a DataTable object with minimum a column named "FNAME" and add this table to the DataSet object.
Code:
DataSet custDS = new DataSet("DOTNET_TEST");
-->    DataTable dt = new DataTable("DOTNET_TEST");
-->    DataColumn col1 = new DataColumn("FNAME");
-->    dt.Columns.Add(col1);
--->   custDS.Tables.Add(dt);
DataRow anyRow = custDS.Tables["DOTNET_TEST"].NewRow();
anyRow["FNAME"] = "Jay";
custDS.Tables["DOTNET_TEST"].Rows.Add(anyRow);
But the above code will do nothing on the data source where you open a connection.
obislavu
 
On this code,

DataSet custDS = new DataSet("DOTNET_TEST");

"DOTNET_TEST" is just a namespace and has nothing to do with the table you will create following obislavu's correction:

--> DataTable dt = new DataTable("DOTNET_TEST");


However, explaining obislavu's last words, when you add or modify a row in "DOTNET_TEST" DataTable, you're only modifying what's still cached in the memory. To read more about DataSets, see
Nothing will be uploaded to your DB until you setup a proper SqlCommand / SqlDataAdapter object and use this object to upload the changes.

Here's a pseudocode on your code:


If IsPostBack=True
Connect to server DB
Create in-memory cache
Append form data (to in-memory)
Close connection
Redirect page

Nothing was sent to the server prior to closing the connection.

Since you're probably dealing with singleton record (registration form) only, its faster and cheaper to use an SqlCommand object instead.

Hope this helps [peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top