Im fairly new to C# so please forgive me if my terminology isn't to par. In my winform application I want to grab a name from a MySQL database @ login and display the name on a second page(home.cs). Im currently getting an error which is "An Object Reference is required for the non static field. In login.cs im trying to add something like -> Home.bunifuCustomLabel2.Text = "test";
to set the label in home.cs basically... Login.cs is my 1st loading page and Home.cs is my 2nd page which loads after successful login. I would like to query the database while the connection is open and eventually set the label text for home.cs to display something like Welcome, Name
LOGIN.CS:
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 MySql.Data.MySqlClient;
namespace Firemax
{
public partial class Login : Form
{
MySqlConnection con = new MySqlConnection(@"Data Source=xyz.com;port=3333;Initial Catalog = test_db;User Id = fml;password=imscrewed");
int i;
public Login()
{
InitializeComponent();
this.CenterToScreen();
}
private void BunifuMaterialTextbox1_OnValueChanged(object sender, EventArgs e)
{
}
private void BunifuMaterialTextbox2_OnValueChanged(object sender, EventArgs e)
{
bunifuMaterialTextbox2.isPassword = true;
}
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from users where username='" + bunifuMaterialTextbox1.Text + "' and password='" + bunifuMaterialTextbox2.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i==0)
{
bunifuCustomLabel3.Text = "You have entered either a wrong Username and/or Password";
Home.bunifuCustomLabel2.Text = "test";
}
else
{
this.Hide();
Home fm = new Home();
fm.Show();
fm.Location = this.Location;
}
con.Close();
}
private void BunifuMaterialTextbox1_OnValueChanged_1(object sender, EventArgs e)
{
}
private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
HOME.CS:
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 MySql.Data.MySqlClient;
namespace Firemax
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
this.CenterToScreen();
}
private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Welcome to C# world! Changing code of logic.cs to following will eliminate the problem you are facing.
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 MySql.Data.MySqlClient;
namespace Firemax
{
public partial class Login : Form
{
MySqlConnection con = new MySqlConnection(@"Data Source=xyz.com;port=3333;Initial Catalog = test_db;User Id = fml;password=imscrewed");
int i;
public Login()
{
InitializeComponent();
this.CenterToScreen();
}
private void BunifuMaterialTextbox1_OnValueChanged(object sender, EventArgs e)
{
}
private void BunifuMaterialTextbox2_OnValueChanged(object sender, EventArgs e)
{
bunifuMaterialTextbox2.isPassword = true;
}
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from users where username='" + bunifuMaterialTextbox1.Text + "' and password='" + bunifuMaterialTextbox2.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
Home fm = new Home();
if (i==0)
{
bunifuCustomLabel3.Text = "You have entered either a wrong Username and/or Password";
fm.bunifuCustomLabel2.Text = "test";
}
else
{
this.Hide();
fm.Show();
fm.Location = this.Location;
}
con.Close();
}
private void BunifuMaterialTextbox1_OnValueChanged_1(object sender, EventArgs e)
{
}
private void BunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Your code needs improvements. I would highly recommend investing 70 minutes in C# video series by Mosh.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments