在列表框选择上填充文本框 [英] Fill textboxes on listbox selection
问题描述
好吧,我想做的是在列表框中选择几个文本框.这是我的代码:
Well what I''m trying to do is fill a few textboxes on a list box selection. Here is the code I have:
if(!string.IsNullOrEmpty(this.UserList.SelectedItem.ToString()))
{
string value;
value = UserList.SelectedValue.ToString();
MySqlCommand sql1 = new MySqlCommand("SELECT * FROM `users` WHERE `name` = ''"+value+"''", myConnection.GetConnection());
MySqlDataAdapter DA = new MySqlDataAdapter(sql1);
DataSet DS = new DataSet();
DA.Fill(DS, "users");
if(DS.Tables["users"].Rows.Count > 0)
{
this.Usernametxtbox.Text = DS.Tables["users"].Rows[0] ["name"].ToString();
}
else
{
MessageBox.Show("No data to populate the feilds");
}
}
else
{
MessageBox.Show("Selected Value is null");
}
}
而且我似乎无法弄清楚为什么它基本上告诉我我使用了错误的Sql语法,我已经在phpmyadmin中检查了Sql查询,并且工作正常,因此我无法弄清楚自己在做什么错.
这是确切的错误,突出显示DA.Fill(DS, "users");
,然后说您的SQL语法有错误;在第1行的"System.Data.DataRowView"''''''附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法
And I can''t seem to figure out why it''s basically telling me I have the wrong Sql syntax, I have checked the Sql query in phpmyadmin and it works fine, so I cant figure out what I''m doing wrong.
Here is the exact error, it highlights DA.Fill(DS, "users");
and then it says You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''System.Data.DataRowView'''''' at line 1
推荐答案
尝试以下操作:
try this:
MySqlCommand sql1 = new MySqlCommand("SELECT * FROM users WHERE name = ''"+value+"''", myConnection.GetConnection());
我将首先尝试在您的代码中进行替换:
MySqlCommand sql1 = new MySqlCommand("SELECT * FROM users WHERE name = ''" + value + "''", myConnection.GetConnection());
还供您参考表:
I would start by trying this for replacement in your code:
MySqlCommand sql1 = new MySqlCommand("SELECT * FROM users WHERE name = ''" + value + "''", myConnection.GetConnection());
also for your table reference:
<br />
this.Usernametxtbox.Text = DS.Tables[0].Rows[0] ["name"].ToString();
只是测试的想法....
Just a thought for testing....
这篇关于在列表框选择上填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!