I have a code for inserting data in database, which checks whether a row is inserted or not and returns th id of the row. Here is the code:
public bool vnesiDokument(Dokument dokument)
{
bool result = false;
SqlConnection connection = null;
try
{
// Open a database connection.
connection = GetConnection();
connection.Open(); //in this row I got error
"Object reference not set to an instance of an object."
// Create a command.
SqlCommand command = new SqlCommand("vnesiDok", connection);
command.CommandType = CommandType.StoredProcedure;
// Add parameters to command.
command.Parameters.AddWithValue("@vp", dokument.vp);
command.Parameters.AddWithValue("@valuta", dokument.valuta);
command.Parameters.AddWithValue("@valutakurs", dokument.valuta_kurs);
command.Parameters.AddWithValue("@imeprez", dokument.imeprezime);
command.Parameters.AddWithValue("@iznos", dokument.iznos);
command.Parameters.AddWithValue("@iznosden", dokument.iznosden);
command.Parameters.AddWithValue("@br_kas_izvod", dokument.br_kas_izvod);
command.Parameters.AddWithValue("@br_smetka", dokument.br_pat_smetka);
command.Parameters.AddWithValue("@tip", dokument.tip);
command.Parameters.AddWithValue("@datum", dokument.datum);
SqlParameter brdokParam = new SqlParameter(
"@brdok", SqlDbType.Int);
brdokParam.Direction = ParameterDirection.Output;
command.Parameters.Add(brdokParam);
// Execute the command.
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected == 1)
{
// Get the patient ID generated by the database for
// the new patient.
dokument.brdok = (int)command.Parameters["@brdok"].Value;
result = true;
}
}
finally
{
// Close the database connection.
if (connection != null)
{
connection.Close();
}
}
return result;
}
Can anybody help me what's wrong with my code please?
Thanks
Zoran HorvatPosted Aug 5, 2011, 4:23 AM
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ContosoMedicalDBConnectionString"];
I'm not sure what happens if ConnectionStrings doesn't contain element for ContosoMedicalDBConnectionString - should it break with exception or just return null. But I believe that it has returned null into settings variable and then you didn't get into the if statement. Hence, return value remained null. Try to debug that part of the GetConnection function.
Zoran
NelPosted Aug 5, 2011, 6:14 AM
But thank you very much anyway.
Zoran HorvatPosted Aug 5, 2011, 5:59 AM
If yes, then at which place does null pointer exception occur now?
Zoran
NelPosted Aug 5, 2011, 5:55 AM
NelPosted Aug 5, 2011, 4:20 AM
System.NullReferenceException: Object reference not set to an instance of an object., the same error.
here is the method
private SqlConnection GetConnection()
{
SqlConnection connection = null;
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ContosoMedicalDBConnectionString"];
if (settings != null)
{
string ConnectionString = settings.ConnectionString;
connection = new SqlConnection(ConnectionString);
}
return connection;
}
Zoran HorvatPosted Aug 5, 2011, 4:07 AM
Zoran