Set a Label text in 1st winform page to display on 2nd form page

demo7up

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();
        }
    }
}
Shahid Syed

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to display the selected datepicker value from the 1st page onto the 2nd page datepicker

If usertype is jobseeker then redirect to 1st page if usertype is employer then redirect to 2nd page

display 2nd form depending on 1st form result

Loading a 2nd DIV on submit button click on the 1st DIV leaving the 1st DIV visible on the same page

map and send list of data from 1st to 2nd page in flutter

How to get tableview cell text label from 2nd viewcontroller to a label from 1st viewcontroller?

How to tell if a Google App Engine documentation page applies to the 1st/2nd generation standard or the flexible environment

link will work only on 1st click and from 2nd click will not redirect to the page which is specified in href

While using a web scraper how can I make sure that once the 1st page has been scraped it will then scrape the 2nd page?

Joining 2 text files using pandas, 1st text file into header, the 2nd as the body

AWK Retrieve text after a certain pattern where the 1st and 2nd columns match the values in the 1st and 2nd columns in an input file

i want an additional column to be displayed which will display 1st occurence, 2nd occurence and so on

How to initialise 2nd view controller form 1st view iOS

How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form

Find the Last Cell Value of the 2nd Column Set for each 1st Column Cell in EXCEL

How to set 1st, 2nd and 3rd letters above the digit in swift

How to select the 1st third, 2nd third and 3rd third of a data set?

Set option of the 2nd Select based on the 1st Select

Best Practice to display text label on a .NetCore Razor page

How to get label text value form a html page?

AJAX returns whole web page after 2nd form submit - JQuery

Form reloads page 2nd time with preventDefault when using custom DOM function

How can i set the text for a label in razor page using "if" condition?

Pagination 1st page link in CodeIgniter

Javascript works on 1st page but not the second

How to Add 30 days to 1st Datepicker, display it on the 2nd Datepicker. And Add 3 days to 2nd , display it on 3rd?

Google Sheets - Label Value (Name) by Occurrence (1st, 2nd, 3rd, etc.), ordered by Corresponding Date

Apex - Display Text on Page

How do I compare every line of the 1st text file to every line of the 2nd text file in Python?