Wednesday, 28 August 2013

Object reference not set to an instance of an object in c# how to check for null value?

Object reference not set to an instance of an object in c# how to check
for null value?

I am getting nullrefernce exception after running c# code at this line
var data = info.details.Split('|');
c# code:
public studentinfo SaveData(studentinfo info)
{
bool returnBool=false;
SqlConnection con;
var data = info.details.Split('|');
var response = new studentinfo
{
ID = data[0],
Name = data[1],
Project = data[2],
Result = data[3]
};
con = new
SqlConnection(ConfigurationManager.ConnectionStrings["Myproject.Properties.Settings.MyConnection"].ConnectionString);
string sqlStr = "INSERT INTO Result (ID,Name,Project,Result)
values('" + data[0] + "', '" + data[1] + "', '" + data[2] +
"', '" + data[3] + "')";
SqlCommand dbCommand = new SqlCommand(sqlStr, con);
try
{
con.Open();
if (dbCommand.ExecuteNonQuery() != 0)
{
returnBool = true;
}
if (!data.Equals(null))
{
returnBool = true;
}
con.Close();
}
catch
{
returnBool= false;
}
con.Close();
return response;
}
I tried to implement:
if (!data.Equals(null))
{
returnBool = true;
}
but even then its giving the same object reference exception, Please help
me to resolve it. Thanks

No comments:

Post a Comment