This is my Sql functions class created in c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace DatabaseTest
{
static class SQLFunctions
{
static private SqlConnection dbconnect = new SqlConnection(@"Data Source=sourcecode_guy-pc\sqlexpress;Initial Catalog=test;Integrated Security=True");
static public void Delete(object button) // This is the *Delete Function*
{
try
{
dbconnect.Open();
int IsDeleted;
SqlCommand DeleteAllQuery = new SqlCommand("DELETE FROM users", dbconnect);
DeleteAllQuery.ExecuteNonQuery();
IsDeleted = (int)DeleteAllQuery.ExecuteScalar();
if (IsDeleted == 0)
{
MessageBox.Show("Database has been truntcated.");
}
else if(IsDeleted == 1)
{
MessageBox.Show("An error has occured and your database table was not deleted.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
dbconnect.Close();
}
}
}
}
This is the delete button in my form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DatabaseTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDelete_Click(object sender, EventArgs e)
{
SQLFunctions.Delete(button: btnDelete);
}
}
}
When I run the program, it gives me a System.NullReferenceException: Object reference not set to an instance of an object. at DatabaseTest.SQLFunctions.Delete(Object button) in C:\my path :)