![]() |
|
Shops loading error - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196) +---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192) +---- Thread: Shops loading error (/showthread.php?tid=2281) Pages:
1
2
|
- Jaiz - 07-28-2013 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 - someone - 07-28-2013 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? - Jaiz - 07-28-2013 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 - soryjero - 07-28-2013 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. - someone - 07-28-2013 try this as references 2 DataTable dt = new DataTable(); dt.Load(dbreader); listBox1.DataSource =dt; listBox1.DisplayMember = "test_column"; On Click event: DataTable dt = listBox1.DataSource this.textBox_name = dt.rows[listBox1.selectedIndex]["Column_name"]; - Jaiz - 07-28-2013 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); - Jaiz - 07-28-2013 DataTable dt = listBox1.DataSource This code shows me a error too - someone - 07-28-2013 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); } } - Jaiz - 07-28-2013 Thank you very much someone. But 1 question i have. The dbCon i cant make it change able? /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); } here server=textBox_host.Text thats work or? I want make it change able per textBox ![]() - someone - 07-28-2013 You first need to close the connection to change it and then reopen it. More info read this thread: 2 |