如何将下拉列表中的选定数据发送到mysql中的相应表?

时间:2016-01-20 19:43:05

标签: c# mysql asp.net

所以这就是我到目前为止所做的:

 //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();

如果例如用户选择了球杆,则会出现问题,桌面上不会显示正确的选项。 (请查看我插入的屏幕截图)

enter image description here

enter image description here

编辑:点击按钮后调用:

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();

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

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;