色々探して失敗してやっとここにたどり着きました…
データベースからcheckboxListを動的に作成しています。次に、これらの選択したチェック済みアイテムを送信したいのですが、アクセスできません。コードを見つけてください....
protected void CreateCheckBoxListDynamically()
{
DataTable dt = new DataTable();
SqlConnection dBConnection = null;
try
{
dBConnection = new SqlConnection();
dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("SP_GetDomesticCountryList", dBConnection);
cmd.CommandType = CommandType.StoredProcedure;
if (ddlTournamentType.SelectedValue != "Select" && ddlTournamentType.SelectedValue != "")
cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
else
cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = DBNull.Value;
dBConnection.Open();
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
CheckBoxList cblCountry = new CheckBoxList();
cblCountry.ID = "cblCountryTeam";
cblCountry.DataTextField = dt.Columns["CT_CountryTeamName"].ToString();
cblCountry.DataValueField = dt.Columns["CT_CountryTeamId"].ToString();
cblCountry.DataSource = dt;
cblCountry.DataBind();
//phCheckBoxList.Attributes.Add("class", "groupbox");
phCheckBoxList.Controls.Add(cblCountry);
phCheckBoxList.Controls.Add(new LiteralControl("<br />"));
}
dBConnection.Close();
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
// Close data reader object and database connection
if (dBConnection.State == ConnectionState.Open)
dBConnection.Close();
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlCommand cmd = null;
SqlConnection dBConnection = new SqlConnection();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
string countryTeamIds = "";
try
{
dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
cmd = new SqlCommand("SP_AddNewSeries", dBConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MemberId", SqlDbType.Int).Value = memberId;
if (ddlTournamentType.SelectedValue != "Select")
cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
else
cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = DBNull.Value;
if (!string.IsNullOrEmpty(txtSeriesStartDate.Text))
cmd.Parameters.Add("@SeriesStartDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesStartDate.Text);
else
cmd.Parameters.Add("@SeriesStartDate", SqlDbType.Date).Value = DBNull.Value;
if (!string.IsNullOrEmpty(txtSeriesEndDate.Text))
cmd.Parameters.Add("@SeriesEndDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesEndDate.Text);
else
cmd.Parameters.Add("@SeriesEndDate", SqlDbType.Date).Value = DBNull.Value;
// get values from dynamic controls
CheckBoxList cb = (CheckBoxList)phCheckBoxList.FindControl("cblCountryTeam");
if (cb != null)
{
foreach (ListItem li in cb.Items)
{
if (li.Selected)
countryTeamIds += li.Value + "~";
}
}
if (!string.IsNullOrEmpty(countryTeamIds))
cmd.Parameters.Add("@CountryTeamIds", SqlDbType.NVarChar).Value = countryTeamIds;
else
cmd.Parameters.Add("@CountryTeamIds", SqlDbType.NVarChar).Value = DBNull.Value;
dBConnection.Open();
dataAdapter.InsertCommand = cmd;
int i = cmd.ExecuteNonQuery();
//hdnseriesId.Value = cmd.Parameters["@SeriesId"].Value.ToString();
if (i == 0)
{
msgNoRecords.Visible = true;
}
else
{
msgNoRecords.Visible = false;
//Response.Redirect("~/country.aspx", false);
}
dBConnection.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Close data reader object and database connection
cmd.Dispose();
cmd = null;
if (dBConnection.State == ConnectionState.Open)
dBConnection.Close();
}
}