ID とその名前を 2 回選択し、それを に入れ、 にDataSet
バインドするDropDownList があります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();
}
}