我有一个 DropDownList ,我想在其中选择一个 ID 及其名称两次,将其放入 aDataSet
并绑定到 a DropDownList
。下面的代码是我的数据绑定。
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT [profileID], [profileName], [positionID], [positionName] from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn1);
cmd1.Parameters.AddWithValue("@memberID", memberID);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
conn1.Open();
da.Fill(ds);
conn1.Close();
chooseProfile.DataSource = ds;
chooseProfile.DataTextField = "profileName";
chooseProfile.DataValueField = "profileID";
chooseProfile.DataBind();
choosePosition.DataSource = ds;
choosePosition.DataTextField = "positionName";
choosePosition.DataValueField = "positionID";
choosePosition.DataBind();
}
在我的 ASP.NET 页面上,当用户点击提交时,它意味着获取值并插入到表中。
protected void submitReport_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery = "SELECT * FROM vw_profile";
SqlCommand cmd = new SqlCommand(selectQuery, conn);
conn.Open();
SqlDataReader viewReader;
viewReader = cmd.ExecuteReader();
while (viewReader.Read())
{
var memberID = (string)viewReader["memberID"].ToString();
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT * from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn);
cmd1.Parameters.AddWithValue("@memberID", memberID);
conn1.Open();
int submitProfileID = Convert.ToInt32(chooseProfile.SelectedItem.Value);
int submitPositionID = Convert.ToInt32(choosePosition.SelectedItem.Value);
DateTime dateTime = DateTime.Now;
string date = dateTime.GetDateTimeFormats('d')[0];
int reportNum;
reportNum = Convert.ToInt32(viewReader["reportNumber"]);
reportNum++;
string reportTitle = "#" + reportNum + " - " + date;
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString);
conn2.Open();
SqlCommand cmd2 = new SqlCommand("INSERT INTO sfgs_reports(profileID,memberID,positionID,reportTitle,reportContent,reportDateStamp,reportNumber) values (@profileID,@memberID,@positionID,@reportTitle,@content,@datestamp,@reportNumber)", conn2);
cmd2.Parameters.AddWithValue("@content", reportContent.Text);
cmd2.Parameters.AddWithValue("@memberID", memberID);
cmd2.Parameters.AddWithValue("@profileID", submitProfileID);
cmd2.Parameters.AddWithValue("@positionID", submitPositionID);
cmd2.Parameters.AddWithValue("@reportTitle", reportTitle);
cmd2.Parameters.AddWithValue("@datestamp", dateTime);
cmd2.Parameters.AddWithValue("@reportNumber", reportNum);
cmd2.ExecuteNonQuery();
conn2.Close();
}
}
conn.Close();
}
}
现在,当有人提交时会发生什么,而不是Value
从我选择的那个中抓取,而是抓取默认值,就像在DropDownList
默认情况下显示的那样。任何人都可以在此代码中看到可能导致此问题的任何错误吗?
编辑以显示更新的 submitReport_Click 方法:
protected void submitReport_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery = "SELECT * FROM vw_profile WHERE profileName=@currentUser";
SqlCommand cmd = new SqlCommand(selectQuery, conn);
cmd.Parameters.AddWithValue("@currentUser", User.Identity.Name);
conn.Open();
SqlDataReader viewReader;
viewReader = cmd.ExecuteReader();
while (viewReader.Read())
{
var memberID = (string)viewReader["memberID"].ToString();
if (!Page.IsPostBack)
{
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT [profileID], [profileName], [positionID], [positionName] from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn1);
cmd1.Parameters.AddWithValue("@memberID", memberID);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
conn1.Open();
da.Fill(ds);
conn1.Close();
chooseProfile.DataSource = ds;
chooseProfile.DataTextField = "profileName";
chooseProfile.DataValueField = "profileID";
chooseProfile.DataBind();
choosePosition.DataSource = ds;
choosePosition.DataTextField = "positionName";
choosePosition.DataValueField = "positionID";
choosePosition.DataBind();
}
}
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT * from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn);
cmd1.Parameters.AddWithValue("@memberID", memberID);
conn1.Open();
int submitProfileID = Convert.ToInt32(chooseProfile.SelectedItem.Value);
int submitPositionID = Convert.ToInt32(choosePosition.SelectedItem.Value);
DateTime dateTime = DateTime.Now;
string date = dateTime.GetDateTimeFormats('d')[0];
int reportNum = 1;
string reportTitle = "#" + reportNum + " - " + date;
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString);
conn2.Open();
SqlCommand cmd2 = new SqlCommand("INSERT INTO sfgs_reports(profileID,memberID,positionID,reportTitle,reportContent,reportDateStamp,reportNumber) values (@profileID,@memberID,@positionID,@reportTitle,@content,@datestamp,@reportNumber)", conn2);
cmd2.Parameters.AddWithValue("@content", reportContent.Text);
cmd2.Parameters.AddWithValue("@memberID", memberID);
cmd2.Parameters.AddWithValue("@profileID", submitProfileID);
cmd2.Parameters.AddWithValue("@positionID", submitPositionID);
cmd2.Parameters.AddWithValue("@reportTitle", reportTitle);
cmd2.Parameters.AddWithValue("@datestamp", dateTime);
cmd2.Parameters.AddWithValue("@reportNumber", reportNum);
cmd2.ExecuteNonQuery();
conn2.Close();
Page.Response.Redirect("default.aspx?ResponseCode=3", false);
}
}
conn.Close();
}
}