Hello if anyone can help me with this I will be very grateful.
I have a working version of this program as a windows application, when
I try to port it over to c# ASP.NET I can't make it work.
The program works in the following way.
Uploads a file to the server. (works)
Populates a dataset (works)
Passing the dataset to another class (doesn't work)
The problem comes when I try to access this dataset from another class.
I receive the error message
"NullReferenceException was unhandled by user code"
It occurs on the following line:
numberRows = Convert.ToInt32(dsBarcodesToCheck.Tables
Dataset is declared using the following code:
public DataSet dsBarcodesToCheck = new DataSet();
I have included the entire program below. It is worth noting that the
public string test does seem to pass okay, the dataset doesn't.
Thanks for any help you can offer
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Threading;
using System.Data.Odbc;
public partial class _Default : System.Web.UI.Page
{
public int[] valueArray;
public DataSet dsBarcodesToCheck = new DataSet();
public string Test;
public struct returndata
{
public string iclass;
public string ivendor;
public string istyle;
public string icolour;
public string isize;
public string ides;
public string ifound;
}
public void BarcodeLookup()
{
//Console.WriteLine("T" + Thread.CurrentThread.Name + "-S" +
DateTime.Now.ToLongTimeString());
returndata rd = new returndata();
DataSet ds = new DataSet();
string bar, upc, chk, strSQL, strConn;
int numberRows;
long RowsInQueryResult;
bar = "NotSet";
Test = "SETOKAY";
numberRows =
dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows.Count;
for (int looper = 1; looper < numberRows; looper++)
{
if
(dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows[looper]["ThreadAccesse
d"].ToString() == "N")
{
dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows[looper]["ThreadAccessed
"] = "Y";
bar =
dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows[looper]["Barcodes"].ToS
tring();
upc = bar.Substring(0, 12);
chk = "";
if (bar.Length == 13)
{
chk = bar.Substring(12, 1);
}
strConn = "dsn=SYSBLON;uid=GUEST;pwd=guest;";
strSQL = "SELECT * FROM IPUPCXF where UUPD = '" + upc +
"' AND UECK = '" + chk + "'";
System.Data.Odbc.OdbcDataAdapter da = new
OdbcDataAdapter(strSQL, strConn);
da.Fill(ds, "tblXref");
RowsInQueryResult = ds.Tables["tblXref"].Rows.Count;
rd.ifound = "false";
if (RowsInQueryResult > 0)
{
rd.iclass =
ds.Tables["tblXref"].Rows[0]["UCLS"].ToString();
rd.ivendor =
ds.Tables["tblXref"].Rows[0]["UVEN"].ToString();
rd.istyle =
ds.Tables["tblXref"].Rows[0]["USTY"].ToString();
rd.icolour =
ds.Tables["tblXref"].Rows[0]["UCLR"].ToString();
rd.isize =
ds.Tables["tblXref"].Rows[0]["USIZ"].ToString();
rd.ides =
ds.Tables["tblXref"].Rows[0]["UDES"].ToString();
rd.ifound = "true";
}
//Console.WriteLine("T" + Thread.CurrentThread.Name +
"-" + DateTime.Now.ToLongTimeString() + "-" + rd.iclass + "-" +
rd.ivendor + "-" + rd.istyle + "-" + rd.icolour + "-" + rd.isize + "-" +
rd.ides + "-" + rd.ifound);
ds.Tables["tblXref"].Clear();
}
}
}
public void Page_Load(object sender, EventArgs e)
{
}
public void UploadFile_Click(object sender, EventArgs e)
{
loadFile();
}
public void loadFile()
{
DataRow myRow;
string strFileName = FileUpload1.PostedFile.FileName;
strFileName = System.IO.Path.GetFileName(strFileName);
FileUpload1.PostedFile.SaveAs("C:/Inetpub/BarcodeChecker/" +
strFileName + "");
Label1.Text = "Your file: " + strFileName + " has been uploaded
successfully";
string XmlFile;
XmlFile = "C:/Inetpub/BarcodeChecker/" + strFileName + "";
FileStream fstr = new FileStream(XmlFile, FileMode.Open,
FileAccess.Read);
XmlTextReader reader = new XmlTextReader(fstr);
string name;
string value;
dsBarcodesToCheck.Tables.Add("BarcodesToCheck");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Barcodes");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Class");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Vendor");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Style");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Colour");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Size");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("Description");
dsBarcodesToCheck.Tables["BarcodesToCheck"].Columns.Add("ThreadAccessed"
);
while (reader.Read())
{
if (reader.Name.ToString() == "Data")
{
reader.Read();
name = reader.Name.ToString();
value = reader.Value.ToString();
if (value.Length > 0)
{
myRow =
dsBarcodesToCheck.Tables["BarcodesToCheck"].NewRow();
myRow["Barcodes"] = value;
myRow["ThreadAccessed"] = "N";
dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows.Add(myRow);
}
}
}
Label1.Text =
dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows.Count.ToString();
fstr.Close();
Test = "testing";
GridViewer.DataSource = dsBarcodesToCheck;
GridViewer.DataMember =
dsBarcodesToCheck.Tables["BarcodesToCheck"].TableName.ToString();
GridViewer.DataBind();
}
public void CheckBarcodes_Click(object sender, EventArgs e)
{
int numberRows;
string lclTesting;
lclTesting = Test;
numberRows =
Convert.ToInt32(dsBarcodesToCheck.Tables["BarcodesToCheck"].Rows.Count.T
oString());
for (int t = 0; t < 2; t++)
{
Thread thread = new Thread(new ThreadStart(BarcodeLookup));
thread.Name = Convert.ToString(t);
thread.Start();
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***