This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
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 :)
Your code Executes the Command twice.
DeleteAllQuery.ExecuteNonQuery();
IsDeleted = (int)DeleteAllQuery.ExecuteScalar();
Remove one or the other.
Related
im new to C# and trying to connect a windows form to a mySQL database.
The problem is i get the error below and can't find a way to fix this.
Error CS0120 An object reference is required for the non-static field, method, or property 'Login.con()
This is the Form.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BtnConnect_Click(object sender, EventArgs e)
{
Login.con();
}
}
}
This is the Login.cs file where the connections should be made.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using MySql.Data;
using System.Data;
namespace Log
{
public class Login
{
public MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public Login()
{
server = "localhost";
database = "test";
user = "root";
password = "root";
port = "3306";
sslM = "none";
connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);
connection = new MySqlConnection(connectionString);
}
public void con()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
}
}
you need to instance Login class...
Login login = new Login();
login.con()
You have to create a new instance of class Login. Try like:
public void BtnConnect_Click(object sender, EventArgs e)
{
Login login = new Login();
login.con();
}
With this code I can prevent a duplicate application from running the name given in the code .
But after the application is created, the name of that application (exe) can be changed and run multiple times in the same machine.
How to prevent it from running?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Demo";
int ac = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count();
if (ac > 1)
{
MessageBox.Show("Application Already Running");
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
}
}```
I'm trying to create a Sharepoint Site using Sharepoint client namespaces, full code below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SPSite topLevelSite = new SPSite("http://localhost");
SPWeb spWebInstance = topLevelSite.OpenWeb();
String siteTemplate = spWebInstance.WebTemplate;
try
{
SharePointWebInstance.Webs.Add("the name", "name", "new site added", (UInt32)System.Globalization.CultureInfo.CurrentCulture.LCID, siteTemplate, false, false);
}
catch (Exception ex)
{
//...
}
finally
{
topLevelSite.Close();
SharePointWebInstance.Dispose();
}
}
}
}
And I'm getting a C# error at SharePointWebInstance.Webs.Add. Any thoughts?
You need to use the SPWeb object you have created, so it should be like this;
spWebInstance.Webs.Add("the name", "name", "new site added", (UInt32)System.Globalization.CultureInfo.CurrentCulture.LCID, siteTemplate, false, false);
And also make sure to update SPSite url address accordingly.
Here is my code: I am trying to find the roblox PlayerBeta but its not working
I am not the best in coding but I found out how to find it but cant implent it right into the code
( EDIT ) The program works if I use the PID but that changes everytime I open up a new RobloxPlayerBeta ! so I cannot use that
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Memory;
using System.Diagnostics;
namespace ForceField
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Mem MemLib = new Mem();
private void button1_Click(object sender, EventArgs e)
{
if (System.Diagnostics.Process.GetProcesses().Any((p) => p.ProcessName.Contains("RobloxPlayerBeta")))
{
int robloxPid = System.Diagnostics.Process.GetProcessesByName("RobloxPlayerBeta").FirstOrDefault().Id;
}
Console.WriteLine(robloxPid);
MemLib.writeMemory("0x184C3A98", "string", "PlsNoBan ");
Console.WriteLine(MemLib.readString("0x184C3A98"));
}
}
}
Omg I am stupid.. xD
I needed to do MemLib.OpenGameProcess(robloxPid)
This question already has answers here:
How to reset bindingsource filter to nothing
(4 answers)
Closed 6 years ago.
How can I automatically or by button clear the filter applied ?
I use the following code to filter a DataGridView from frPlanMain (Form 1) from Form 2.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Plan_de_lucru_1._0
{
public partial class SearchWindow : Form
{
public frPlanMain refTofrPlanMain;
public SearchWindow(frPlanMain f) //<<Edit made here
{
refTofrPlanMain = f;
InitializeComponent();
}
private void SearchButtonW_Click(object sender, EventArgs e)
{
{
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
if (item.Visible)
{
refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
break;
}
//
}
}
}
}
}
To clear a filter on a DataView, set its RowFilter property to an empty string
yourDataView.DefaultView.RowFilter = ""
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Plan_de_lucru_1._0
{
public partial class SearchWindow : Form
{
public frPlanMain refTofrPlanMain;
public SearchWindow(frPlanMain f) //<<Edit made here
{
refTofrPlanMain = f;
InitializeComponent();
}
private void SearchButtonW_Click(object sender, EventArgs e)
{
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
if (item.Visible)
{
refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
break;
}
}
}
private void clearFilter_onClick(object sender, EventArgs e){
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = null;
}
}
}
See here: How to reset bindingsource filter to nothing