Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data after login to other form C#

I just started coding with C# and SQL this week to create a desktop application; after login I want to put the user data who logged in other form # dashboard, but I couldn't find a way to do this. I found a way to create a class and put that user data in it so you can grab; but I am really stuck here.

public void bunifuFlatButton1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-R3ILNJ7;Initial Catalog=Project2;Integrated Security=True");
    String query = "SELECT * FROM USERDB WHERE PRENOM='" + alphaBlendTextBox1.Text + "'AND PASS='" + alphaBlendTextBox2.Text + "'";

    SqlDataAdapter sda = new SqlDataAdapter(query, con);
    DataTable dtbl = new DataTable();
    sda.Fill(dtbl);

    if(dtbl.Rows.Count == 1) 
    {
        string name = dtbl.Rows[0]["Nom"].ToString();
        this.Hide(); 

        Form1 f1 = new Form1();
        f1.ShowDialog();
    }
    else
        MessageBox.Show("mauvais password try again"); 
}
like image 270
Yassine Fatnassi Avatar asked Dec 19 '25 07:12

Yassine Fatnassi


2 Answers

Modify the constructor of Form1 and when creating object of Form1 also pass the value which you can use in your Form1. Below is sample code of your Form1:

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        DataTable MyDataTable = new DataTable();

        public Form1(DataTable _MyDataTable)
        {
            InitializeComponent();

            MyDataTable = _MyDataTable;           
        }
    }
}

Then change your code to pass your values to this form like below:

if(dtbl.Rows.Count == 1) 
{
    string name = dtbl.Rows[0]["Nom"].ToString();        
    this.Hide();
    Form1 f1 = new Form1(dtbl);
    f1.ShowDialog();
}
else
    MessageBox.Show("mauvais password try again");
like image 84
Brijesh Kumar Tripathi Avatar answered Dec 20 '25 22:12

Brijesh Kumar Tripathi


One way of doing this is to create an Object which contains the data you read from your DB and then pass this into the constructor of your new form.

//This class will store the data from the DB
public class MyClass
{
    Public string Name { get; set; }
    //Repeat for all fields retrieved from the DB that you require.

    public MyClass() 
    {

    }
}


  //I changed below to have Using clauses.  The way you had it you were not correctly disposing your objects and disconnecting from the DB, 
//and you would have memory leaks and other problems later
DataTable dtbl = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-R3ILNJ7;Initial Catalog=Project2;Integrated Security=True"))
{       
    //I Changed this to use Parameters!
    //See https://www.dreamincode.net/forums/topic/268104-the-right-way-to-query-a-database-parameterizing-your-sql-queries/

    String query = "SELECT * FROM USERDB WHERE PRENOM= @PRENOM AND PASS= @PASS";      
    using (SqlCommand command = new SqlCommand(query, con)) 
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(command))
        {               
            //Check the SQLDbType below is correct for you DB schema!  
            sda.SelectCommand.Parameters.Add("@PRENOM", SqlDbType.NVarChar).Value = alphaBlendTextBox1.Text; 
            sda.SelectCommand.Parameters.Add("@PASS", SqlDbType.NVarChar).Value = alphaBlendTextBox2.Text; 
            sda.Fill(dtbl);             
        }
    }
}


//Declare your class here
MyClass mc = new MyClass();

if(dtbl.Rows.Count == 1) 
{
    mc.Name = dtbl.Rows[0]["Nom"].ToString();

    Form1 f1 = new Form1(mc);
    this.Hide(); 
    f1.ShowDialog();
}
else
    MessageBox.Show("mauvais password try again");

dtbl = null;


//Now update your Form code and create a new constructor
public partial class Form1 : Form
{
     //This is where you will store the incoming data
     private MyClass IncomingMyClass { get; set; }

     //Change the existing constructor to Private
     private Form1()
     {
         InitializeComponent();
     }

     //Create a new constructor, which calls the empty (now private) constructor above
     public Form1(MyClass myclass): this()
     {
        this.IncomingMyClass = myclass;
     }
     ....
like image 27
jason.kaisersmith Avatar answered Dec 20 '25 20:12

jason.kaisersmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!