Emad Ahmed

Emad Ahmed

  • NA
  • 62
  • 3k

Connect C# WinForms project to MySql through another WinFrom

Apr 21 2016 5:22 AM
I know my question have asked many times, but this time I want to do something more.

Simply, I make C# WinForms database using Telerik Winforms.

The scenario is like this:

1. I made a login form called (frmLogin) that includes a button
(btnDbConfig);
2. When I click (btnDbConfig) it open another form (frmConfig);
3. "frmConfig" will contain manually all the data for
connecting the application to MySql Database. The "frmConfig"
contains textboxes and buttons:
- Server textbox (txtServer)
- Port textbox (txtPort)
- Database textbox (txtDatabase)
- Database user textbox (TxtDbUser)
- Database password textbox (txtDbPassword)
- Save configuration button (btnSaveConfig)
- Cancel Button (txtDbCancel) `This.Close();` "frmConfig" will be connected to "MyCon.mdf" database and store the data entered by the
application user to be connected later to MySql database.

What I want is:

1. A C# code that inserts the data being entered in the textboxes in "frmConfig" into the "MyCom.mdf" when I Click "btnSaveConfig" button".

2. Another C# code to connect to MySql from textboxes in "frmConfig" through "frmLogin"'s login button "btnLogin".

3. The connection to MySql is to be applied to all the forms in the project.

Note : I work on Visual Studio 2015 and installed MySQl Connector.
Thank you for you help.

I adjusted this code to connect but not working:

  1. 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;
    using MySql.Data.MySqlClient;

    namespace MyCom  
  2. {  
  3. public partial class frmConfig : Form  
  4. {  
  5. public frmConfig()  
  6. {  
  7. InitializeComponent();  
  8. }  
  9.   
  10. private void btnDbCancel_Click(object sender, EventArgs e)  
  11. {  
  12. this.Close();  
  13. }  
  14.   
  15. private void btnSaveConfig_Click(object sender, EventArgs e)  
  16. {  
  17. string connetionString = null;  
  18. iCom.MySqlConnection cnn;  
  19. connetionString = "server=" + txtServer.Text + ";database=" + txtDatabase.Text + ";Port=3306;uid=" + txtDbUser.Text + ";pwd= " + txtDbPassword.Text + ";";  
  20. cnn = new MyCom.MySqlConnection(connetionString);  
  21. try  
  22. {  
  23. cnn.Open();  
  24. MessageBox.Show("Connection Open ! ");  
  25. cnn.Close();  
  26. }  
  27. catch (Exception ex)  
  28. {  
  29. MessageBox.Show("Can not open connection ! ");  
  30. }  
  31. }  
  32.   
  33. }  
  34.   


Answers (5)