DataGridView get's filled twice in dont know why
Posted by: khaled Garbaya
Date: January 05, 2010 12:58PM

hi everyone this my first post so i hope you help me with my little problem:
i develope a little application that connects to a mysql database, everythings goes well as expected but my gridview get's fieled twice displaying the data twice
like: 1 2 3 4 5 1 2 3 4 5 and i don't know why i am sure that i am missing something so plz help me up this my code:

ConnectionManager.cs a class that i've create it to managethe connection and gets data:

*********************************************ConnectionManager.cs*******************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Data;

namespace MySqlDemo
{
class ConnectionManager
{
DataTable data = new DataTable();
MySqlDataAdapter da;
public MySqlConnection conn = null;
private string server;
private string userID;
private string password;
private string database;
private string table ;
public ConnectionManager(string serverName, string userID, string password, string database ,string table)
{
this.server = serverName;
this.userID = userID;
this.password = password;
this.database = database;
this.table = table;
//conn = this.openConnection();
}
public ConnectionManager()
{

}

public MySqlConnection openConnection()
{

if (conn != null)
conn.Close();

string connStr = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false",
this.server, this.userID, this.password, this.database);

try
{
conn = new MySqlConnection(connStr);
conn.Open();
}
catch (MySqlException ex)
{
MessageBox.Show("Error connecting to the server: " + ex.Message);
}
return conn;

}
public DataTable GetData()
{

da = null;
conn = openConnection();

da = new MySqlDataAdapter("SELECT * FROM " + this.table, conn);

MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
da.Fill(data);
da = null;
return data;
}
public void updateData()
{
DataTable changes = data.GetChanges();
da.Update(changes);
data.AcceptChanges();
}
}
}

*********************************************************************************

loginForm(Form1.cs)*************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySqlDemo
{
public partial class loginForm : Form
{
public static string serverName;
public static string userID;
public static string password;
public static string database;
public static string tableName;
public loginForm()
{
InitializeComponent();
}

private void cancelButton_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void connectButton_Click(object sender, EventArgs e)
{
if (loginTextBox.Text == "")
{
MessageBox.Show("veillez remplir tout les champs", "Error");
}
else
{
userID = loginTextBox.Text;
password = passTextBox.Text;
serverName = serverTextBox.Text;
database = DBTextBox.Text;
tableName = tableTextBox.Text;
this.Hide();
DataForm df = new DataForm();
df.Show();
}
}

}
}
*******************************************************************************
this form contains fivesTextFields
one for the user name for the database
one for the password
one for the server like localhost in my case
one for the database
one for the tableName

*******************************DataForm.cs***********************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MySqlDemo
{
public partial class DataForm : Form
{
DataTable dt;
ConnectionManager connManager;
public DataForm()
{
InitializeComponent();
}

private void DataForm_Load(object sender, EventArgs e)
{
connManager = new ConnectionManager(loginForm.serverName,loginForm.userID,loginForm.password,loginForm.database, loginForm.tableName);
if (connManager.GetData() != null)
{
dt = connManager.GetData();
tabledataGridView.DataSource = null;
tabledataGridView.DataSource = dt;

}

}

private void DataForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}

private void updateButton_Click(object sender, EventArgs e)
{
connManager.updateData();
}
}
}
****************************************************************************
this form contains a GridView and an update Button.

thank you for helping me,
regards,
khaled Garbaya

Options: ReplyQuote


Subject
Written By
Posted
DataGridView get's filled twice in dont know why
January 05, 2010 12:58PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.