Y
yfujiki
Hi all,
i'm getting a NullReferenceException when calling SqlCommand.ExecuteReader().
Weird thing is that i get it only when i run it as a device application. Same
sample
code runs perfectly in Windows Application Form.
------------------------------------------- below is the sample code. -------
---------------------------------------------
public void Form1_Load(Object sender, EventArgs e) {
string strconn = @"Data Source=XXX.XXX.XXX.XXX, YYYY;Initial
Catalog=dbname;Persist Security Info=True;User
ID=username;Password=password";
string strGetEmpls = " SELECT * from emp ";
SqlConnection conndb = new SqlConnection(strconn);
SqlCommand cmndb = new SqlCommand(strGetEmpls, conndb);
SqlDataReader rdr;
try
{
conndb.Open();
rdr = cmndb.ExecuteReader(); <-------------------------
-------- HERE, I GET THE EXCEPTION
while (rdr.Read())
{
MessageBox.Show((String)rdr["emp_name"]);
}
rdr.Close();
}
catch (SqlException ex)
{
foreach (SqlError err in ex.Errors)
{
MessageBox.Show(err.Message);
}
}
finally
{
conndb.Close();
}
}
}
------------------------------------------------------------------------------
-------------------------------------------------
When i checked cmndb value in debugger, only following 3 members of cmndb
were null.
1. _cachedMetaData
2. _parameters
3. _transaction
I appreciate if anyone can give me a hint about this...
I attach the stacktrace also.
---------------------------------- StackTrace --------------------------------
------------
at System.Data.SqlClient.TdsParser.SkipRow()
at System.Data.SqlClient.TdsParser.SkipRow()
at System.Data.SqlClient.TdsParser.Run()
at System.Data.SqlClient.SqlDataReader.InternalClose()
at System.Data.SqlClient.SqlDataReader.Close()
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at SQLServerSample1.Form1.Form1_Load()
at System.Windows.Forms.Form.OnLoad()
at System.Windows.Forms.Form._SetVisibleNotify()
at System.Windows.Forms.Control.set_Visible()
at System.Windows.Forms.Application.Run()
at SQLServerSample1.Program.Main()
------------------------------------------------------------------------------
i'm getting a NullReferenceException when calling SqlCommand.ExecuteReader().
Weird thing is that i get it only when i run it as a device application. Same
sample
code runs perfectly in Windows Application Form.
------------------------------------------- below is the sample code. -------
---------------------------------------------
public void Form1_Load(Object sender, EventArgs e) {
string strconn = @"Data Source=XXX.XXX.XXX.XXX, YYYY;Initial
Catalog=dbname;Persist Security Info=True;User
ID=username;Password=password";
string strGetEmpls = " SELECT * from emp ";
SqlConnection conndb = new SqlConnection(strconn);
SqlCommand cmndb = new SqlCommand(strGetEmpls, conndb);
SqlDataReader rdr;
try
{
conndb.Open();
rdr = cmndb.ExecuteReader(); <-------------------------
-------- HERE, I GET THE EXCEPTION
while (rdr.Read())
{
MessageBox.Show((String)rdr["emp_name"]);
}
rdr.Close();
}
catch (SqlException ex)
{
foreach (SqlError err in ex.Errors)
{
MessageBox.Show(err.Message);
}
}
finally
{
conndb.Close();
}
}
}
------------------------------------------------------------------------------
-------------------------------------------------
When i checked cmndb value in debugger, only following 3 members of cmndb
were null.
1. _cachedMetaData
2. _parameters
3. _transaction
I appreciate if anyone can give me a hint about this...
I attach the stacktrace also.
---------------------------------- StackTrace --------------------------------
------------
at System.Data.SqlClient.TdsParser.SkipRow()
at System.Data.SqlClient.TdsParser.SkipRow()
at System.Data.SqlClient.TdsParser.Run()
at System.Data.SqlClient.SqlDataReader.InternalClose()
at System.Data.SqlClient.SqlDataReader.Close()
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at SQLServerSample1.Form1.Form1_Load()
at System.Windows.Forms.Form.OnLoad()
at System.Windows.Forms.Form._SetVisibleNotify()
at System.Windows.Forms.Control.set_Visible()
at System.Windows.Forms.Application.Run()
at SQLServerSample1.Program.Main()
------------------------------------------------------------------------------