NullReferenceException when connection to ASP.NET website
Posted by:
John Lace
Date: January 03, 2010 02:08PM
Hello everyone. Firstly can I say I am very new to MySQL, in fact this is the first project I have ever used it in. I am getting an infuriating error when trying to get data from a MySQL table. Here is my code:
string strConnectionString = "Driver={MySQL ODBC 5.1 Driver}; Server=localhost;Database=myDB; Uid=root; Pwd=pw;";
using (OdbcConnection conn = new OdbcConnection(strConnectionString))
{
try
{
conn.Open();
string strSQL = "SELECT ID, Name, ParentID, Active FROM Category WHERE (ParentID = -1);";
using (OdbcCommand cmd = new OdbcCommand(strSQL, conn))
{
using (OdbcDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
int intID = Convert.ToInt32(dr["ID"]);
string strName = Convert.ToString(dr["Name"]);
int intParentID = Convert.ToInt32(dr["ParentID"]);
bool blnActive = Convert.ToBoolean(dr["Active"]);
}
}
}
}
finally
{
conn.Close();
}
}
I get a NullReferenceException on the line "int intID = Convert.ToInt32(dr["ID"]);". The same thing happens if I try to get data from any other table. I can create a command and execute it and instantiate an OdbcDataReader which will contain the correct number of columns and will have "dr.HadRows" set to true but as soon as I try to access a data item from it I get a NullReferenceException. The stack trace is:
at System.Data.Odbc.OdbcConnection.get_ProviderInfo()
at System.Data.Odbc.OdbcConnection.get_IsV3Driver()
at System.Data.Odbc.OdbcDataReader.GetColAttributeStr(Int32 i, SQL_DESC v3FieldId, SQL_COLUMN v2FieldId, HANDLER handler)
at System.Data.Odbc.OdbcDataReader.GetName(Int32 i)
at System.Data.ProviderBase.FieldNameLookup..ctor(IDataReader reader, Int32 defaultLocaleID)
at System.Data.Odbc.OdbcDataReader.GetOrdinal(String value)
at System.Data.Odbc.OdbcDataReader.get_Item(String value)
I've tried googling "System.Data.Odbc.OdbcConnection.get_ProviderInfo()" and it returns no results. I know that there is nothing wrong with the connection string because if I use the same one and try to insert data it works fine. Can anyone help?
Subject
Written By
Posted
NullReferenceException when connection to ASP.NET website
January 03, 2010 02:08PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.