Posts: 190
Threads: 13
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Feb 2013
Reputation:
0
MySqlConnectionStringBuilder mySqlConnectionStringBuilder = new MySqlConnectionStringBuilder();
mySqlConnectionStringBuilder.Server = this.textBox_host.Text;
mySqlConnectionStringBuilder.UserID = this.textBox_user.Text;
mySqlConnectionStringBuilder.Database = this.textBox_db.Text;
mySqlConnectionStringBuilder.Password = this.textBox_password.Text;
MySqlConnection mySqlConnection = new MySqlConnection(mySqlConnectionStringBuilder.ConnectionString);
MySqlConnection mySqlConnection2 = new MySqlConnection(mySqlConnectionStringBuilder.ConnectionString);
mySqlConnection2.Open();
MySqlCommand mySqlCommand = new MySqlCommand();
mySqlCommand.Connection = mySqlConnection;
{
try
{
mySqlConnection.Open();
MySqlCommand MySqlCommand = new MySqlCommand();
MySqlCommand.Connection = mySqlConnection;
string text = this.Shops.SelectedItem.ToString();
MySqlCommand.CommandText = string.Concat(new string[]
{
"select * from `",
textBox_db.Text,
"`.`t_shop` where a_keeper_idx=",
text,
";"
});
this.Reader = MySqlCommand.ExecuteReader();
this.Reader.Read();
this.textBox_name.Text = this.Reader["a_keeper_idx"].ToString();
this.Reader.Dispose();
}
catch (MySqlException ex)
{
MessageBox.Show("Error:\n" + ex.Message);
}
finally
{
if (this.Reader != null)
{
this.Reader.Dispose();
}
if (mySqlConnection != null)
{
mySqlConnection.Close();
mySqlConnection.Dispose();
}
}
}
My Code. I want if i click on this name that this displays on the textBox_name. How i can do that? Hope of help
Regards Jaiz
Posts: 313
Threads: 20
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Jul 2011
Reputation:
0
Problem here:
this.Reader = MySqlCommand.ExecuteReader();
this.Reader.Read();
this.textBox_name.Text = this.Reader["a_keeper_idx"].ToString();
if there are no rows from the reader it will return false, if it will try to read the a_keeprer_idx from the reader, it will throw an exception.
Try something like this
this.Reader = MySqlCommand.ExecuteReader();
if(this.Reader.Read()){
this.textBox_name.Text = this.Reader["a_keeper_idx"].ToString();
}
this.Reader.Close();
Try to use this.Reader.Close(); before this.Reader.Dispose();
Another problem is this:
string text = this.Shops.SelectedItem.ToString();
MySqlCommand.CommandText = string.Concat(new string[]
{
"select * from `",
textBox_db.Text,
"`.`t_shop` where a_keeper_idx=",
text,
";"
});
this.Shops.SelectedItem.ToString() is a string not a index where in your query you look for a index:
"select * from `t_shop` where a_keeper_idx=Master of Monster Combo;"
Which it should be something like this(123 is the ID of the NPC):
"select * from `t_shop` where a_keeper_idx=123;"
Why are you opening so many connections?
Posts: 190
Threads: 13
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Feb 2013
Reputation:
0
Well now there isnt any error but this dont display the keeper idx on the textBox_name.
And yes i have many connections i havent cleaned up it yet xD
Posts: 272
Threads: 21
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Jun 2012
Reputation:
0
Well now there isnt any error but this dont display the keeper idx on the textBox_name.
And yes i have many connections i havent cleaned up it yet xD
you have to double click on listbox and in the new event, you write what you want to see when you click on an element of the list.
i hope you understand me.
Posts: 190
Threads: 13
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Feb 2013
Reputation:
0
you have to double click on listbox and in the new event, you write what you want to see when you click on an element of the list.
i hope you understand me.
yes and there is my problem.
Error:
dt.Load(dbreader);
Posts: 190
Threads: 13
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Feb 2013
Reputation:
0
DataTable dt = listBox1.DataSource
This code shows me a error too
Posts: 313
Threads: 20
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Jul 2011
Reputation:
0
How hard is it:
private MySqlConnection dbCon = new MySqlConnection();
private void btnLoad_Click(object sender, EventArgs e)
{
try{
//get data from the table
MySqlCommand dbCmd = new MySqlCommand("SELECT * FROM test_table ", dbCon);
MySqlDataReader dbReader = dbCmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dbReader);
//add it to the listbox
listBox1.DataSource = dt;
listBox1.DisplayMember = "test_column2";
//close the reader
dbReader.Close();
dbCmd.Dispose();
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//check if the selected row is less then 0
int row = listBox1.SelectedIndex;
if (row < 0) return;
// get the data from the list
DataTable dt =(DataTable) listBox1.DataSource;
textBox1.Text = dt.Rows[row]["test_column3"].ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
//saving the data
//check if the selected row is less then 0
int row = listBox1.SelectedIndex;
if (row < 0) return;
// get the data from the list
DataTable dt = (DataTable)listBox1.DataSource;
dt.Rows[row]["test_column3"] = textBox1.Text;
MySqlCommand dbCmd = new MySqlCommand("Update test_table SET test_column3=" + textBox1.Text + "WHERE test_column1 =" + dt.Rows[row]["test_column1"] + ";", dbCon);
try{
dbCmd.ExecuteNonQuery();
dbCmd.Dispose();
}catch (Exception ex){
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//make a SQLConnection
string strConnection = @"server=127.0.0.1;database=testdb;uid=root;password=test";
dbCon.ConnectionString = strConnection;
try{
dbCon.Open();
}catch (Exception ex){
MessageBox.Show(ex.Message);
}
}