如何从下拉列表中执行SELECT查询。我有一个包含文凭学位的下拉菜单,在我的数据库中,我有一个名为校友(Aid,...,文凭)的表和一个名为Diploma的表,其中包含(Did,Diploma),我们手动插入了5个文凭(brevet, bacc2,本科,硕士,博士)。我想要的是从下拉列表中选择一个文凭,以便在我确认或提交后可以在校友表中结束。这是我的代码。
Form.aspx:
<form id="form" runat="server" method="post">
<div>
<table class="style1">
<tr>
<td>Diploma:</td>
<td>
<asp:DropDownList ID="Diploma" runat="server" AppendDataBoundItems="true" Width="160px">
<asp:ListItem Text="Select Degree" Value="Select Degree"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<center>
<asp:Button ID="Button1" runat="server" Text="Next" OnClick="Button1_Click" Width="85px" />
</center>
</form>
Form.aspx.cs:
protected void Button_Click(object sender, EventArgs e)
{
Session["diploma"] = Diploma.SelectedValue;
if (Status.Text == "1")
{
Response.Redirect("C2.aspx");
}
else
{
Response.Redirect("C1.aspx");
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["diploma"] != null &&)
{
Diploma.SelectedValue = Session["diploma"].ToString();
}
}
}
C1.aspx.cs:
if (!Page.IsPostBack)
{
if (Session["pname"] != null && Session["plastname"] != null && Session["pos"] != null &&
Session["children"] != null && Session["schoolofchildren"] != null &&
Session["schoolofchildren1"] != null && Session["schoolofchildren2"] != null)
{
PName.Text = Session["pname"].ToString();
PLastName.Text = Session["plastname"].ToString();
OldStudent.Text = Session["pos"].ToString();
Children.Text = Session["children"].ToString();
SchoolOfChildren.Text = Session["schoolofchildren"].ToString();
SchoolOfChildren1.Text = Session["schoolofchildren1"].ToString();
SchoolOfChildren2.Text = Session["schoolofchildren2"].ToString();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["pname"] = PName.Text;
Session["plastname"] = PLastName.Text;
Session["pos"] = OldStudent.Text;
Session["children"] = Children.Text;
Session["schoolofchildren"] = SchoolOfChildren.Text;
Session["schoolofchildren1"] = SchoolOfChildren1.Text;
Session["schoolofchildren2"] = SchoolOfChildren2.Text;
Response.Redirect("Form.aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Session["pname"] = PName.Text;
Session["plastname"] = PLastName.Text;
Session["pos"] = OldStudent.Text;
Session["children"] = Children.Text;
Session["schoolofchildren"] = SchoolOfChildren.Text;
Session["schoolofchildren1"] = SchoolOfChildren1.Text;
Session["schoolofchildren2"] = SchoolOfChildren2.Text;
Response.Redirect("C2.aspx");
}
protected void Button4_Click(object sender, EventArgs e)
{
try
{
ExecuteInsert1(Session["diploma"].ToString());
ExecuteInsert2(PName.Text, PLastName.Text, OldStudent.SelectedValue, Children.SelectedValue, SchoolOfChildren.Text, SchoolOfChildren1.Text,
SchoolOfChildren2.Text);
Response.Write("Successfully Added!");
ClearControls(Page);
Session.Clear();
}
catch{}
}
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["Champville1ConnectionString"].ConnectionString;
}
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls) ClearControls(c);
}
}
private void ExecuteInsert1(string diploma)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO Alumni (Diploma) VALUES " + " (@Diploma)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Diploma", SqlDbType.NVarChar, 50);
param[0].Value = diploma;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
private void ExecuteInsert2(...){}