L
labjac
Guest
Hallo
Starting at this for to long.
I want to delete the records from a Database before I put new data in, currently it insert correctly, but want to delete before write.
Code below, the error I get is System.NullReferenceException: 'Object reference not set to an instance of an object.'
I know I'm missing something but not sure what to reference.
namespace WCS_Shared.WallConfigurators.Data
{
public class PutWallDatabaseConfigurator
{
private SqlDbConnect conn;
public void WriteWallConfigurationToDatabase(DataTable dataTable)
{
int locationNr = 1;
string locationID = "010101010";
int record = 0;
DeleteConfigurationDataInDatabase();
foreach (DataRow dr in dataTable.Rows)
{
conn.SqlQuery("INSERT INTO tb_PutWallConfiguration (LocationNr,LocationID) VALUES (@LocNr, @LocID)");
conn.Cmd.Parameters.Add("LocNr", dataTable.Rows[record][0]);
conn.Cmd.Parameters.Add("LocID", dataTable.Rows[record][1]);
conn.NonQueryEx();
record++;
}
}
private void DeleteConfigurationDataInDatabase()
{
//conn.Cmd.CommandText = "DELETE * FROM tb_PutWallConfiguration";
conn.SqlQuery("DELETE FROM tb_PutWallConfiguration");
conn.Cmd.ExecuteNonQuery();// NonQueryEx();
}
}
}
Class for the Connection, this part works, also insert works.
namespace WCS_Shared.Databases.SQL
{
public class SqlDbConnect
{
private SqlConnection _con;
public SqlCommand Cmd;
private SqlDataAdapter _da;
private DataTable _dt;
public SqlDbConnect()
{
_con = new SqlConnection(WCS_Shared.Databases.SQL.ConnectionString.GetConnectionString());
try
{
_con.Open();
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
}
public void SqlQuery(string queryText)
{
Cmd = new SqlCommand(queryText, _con);
}
public DataTable QueryEx()
{
_da = new SqlDataAdapter(Cmd);
_dt = new DataTable();
try
{
_da.Fill(_dt);
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
return _dt;
}
public void NonQueryEx()
{
try
{
Cmd.ExecuteNonQuery();
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
}
}
}
labjac
Continue reading...
Starting at this for to long.
I want to delete the records from a Database before I put new data in, currently it insert correctly, but want to delete before write.
Code below, the error I get is System.NullReferenceException: 'Object reference not set to an instance of an object.'
I know I'm missing something but not sure what to reference.
namespace WCS_Shared.WallConfigurators.Data
{
public class PutWallDatabaseConfigurator
{
private SqlDbConnect conn;
public void WriteWallConfigurationToDatabase(DataTable dataTable)
{
int locationNr = 1;
string locationID = "010101010";
int record = 0;
DeleteConfigurationDataInDatabase();
foreach (DataRow dr in dataTable.Rows)
{
conn.SqlQuery("INSERT INTO tb_PutWallConfiguration (LocationNr,LocationID) VALUES (@LocNr, @LocID)");
conn.Cmd.Parameters.Add("LocNr", dataTable.Rows[record][0]);
conn.Cmd.Parameters.Add("LocID", dataTable.Rows[record][1]);
conn.NonQueryEx();
record++;
}
}
private void DeleteConfigurationDataInDatabase()
{
//conn.Cmd.CommandText = "DELETE * FROM tb_PutWallConfiguration";
conn.SqlQuery("DELETE FROM tb_PutWallConfiguration");
conn.Cmd.ExecuteNonQuery();// NonQueryEx();
}
}
}
Class for the Connection, this part works, also insert works.
namespace WCS_Shared.Databases.SQL
{
public class SqlDbConnect
{
private SqlConnection _con;
public SqlCommand Cmd;
private SqlDataAdapter _da;
private DataTable _dt;
public SqlDbConnect()
{
_con = new SqlConnection(WCS_Shared.Databases.SQL.ConnectionString.GetConnectionString());
try
{
_con.Open();
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
}
public void SqlQuery(string queryText)
{
Cmd = new SqlCommand(queryText, _con);
}
public DataTable QueryEx()
{
_da = new SqlDataAdapter(Cmd);
_dt = new DataTable();
try
{
_da.Fill(_dt);
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
return _dt;
}
public void NonQueryEx()
{
try
{
Cmd.ExecuteNonQuery();
}
catch (Exception e)
{
LogFiles.ExceptionLogger.WriteException(e);
}
}
}
}
labjac
Continue reading...