这就是我到目前为止所做的:
//inserting enrolment details
string input_club = DropDownList1.SelectedItem.Text;
string enrol_mysql = "INSERT INTO enrolment_details(child_id,club_name) VALUES (@child_id, @club_name)";
long id = newchild.LastInsertedId;
MySqlCommand enrol = new MySqlCommand(enrol_mysql, connection);
enrol.CommandText = enrol_mysql;
enrol.Parameters.AddWithValue("@child_id", id);
enrol.Parameters.AddWithValue("@club_name", input_club);
enrol.ExecuteNonQuery();
当用户选择俱乐部时,如果正确的选项没有出现在表格中,就会出现问题。 (请看我插入的屏幕截图)
编辑:这是在点击按钮时调用的:
protected void registerEventMethod(object sender, EventArgs e)
{
registerUser();
Server.Transfer("~/Registration_Login.aspx");
}
private void registerUser()
{...}
此外,下拉列表中显示的选项是从另一个表中检索的。如果有任何相关性,这里是代码:
// Retriving club data from club_detail table from mysql
MySqlCommand clubdata = new MySqlCommand("SELECT club_id,club_name FROM club_details", connection);
DropDownList1.DataSource = clubdata.ExecuteReader();
DropDownList1.DataTextField = "club_name";
DropDownList1.DataValueField = "club_id";
DropDownList1.DataBind();
非常感谢您的帮助。
最佳答案
var sqlQry = "SELECT club_id,club_name FROM club_details";
var da = new MySqlDataAdapter(sqlQry, connection);
var ds = new DataSet();
da.Fill(ds, "ClubDetails");
DropDownList1.DataSource = ds.Tables["ClubDetails"];
DropDownList1.DataTextField = "club_name";
DropDownList1.DataValueField = "club_id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select Item", "0"));
您可能还想更改此 string input_club = DropDownList1.SelectedItem.Text;
测试
var input_club = DropDownList1.SelectedValue;
关于c# - 如何将下拉列表中的选定数据发送到mysql中的相应表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909085/