I have a nested DataList structure DataList2
inside DataList1
where I have two Buttons
inside DataList2
that perform certain commands.
I want to call a procedure that takes as input the datakeyfield
of DataList1
and datakeyfield
of DataList2
. However there is a problem with reading the datakeyfield
of DataList2
. Here is my code:
.aspx.cs code
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if ((e.CommandName == "accept") && (e.CommandArgument != null))
{
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
int team_ID = (int)DataList1.DataKeys[e.Item.ItemIndex];
SqlCommand cmd = new SqlCommand("accept_member", conn);
string member = (string)DataList2.DataKeys[e.Item.ItemIndex];
cmd.CommandType = CommandType.StoredProcedure;
string email = Session["email"].ToString();
int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
cmd.Parameters.Add(new SqlParameter("@team_ID", team_ID));
cmd.Parameters.Add(new SqlParameter("@myemail", email));
cmd.Parameters.Add(new SqlParameter("@member", member));
cmd.Parameters.Add(new SqlParameter("@respond", 1));
SqlParameter count = cmd.Parameters.Add("@count", SqlDbType.Int);
count.Direction = ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
if (@count.Value.ToString().Equals("1"))
{
Response.Write(@member + " " + "joined your team");
}
else
{
Response.Write("team is full or not found");
}
}
else if ((e.CommandName == "reject") && (e.CommandArgument != null))
{
int team_ID = (int)DataList1.DataKeys[e.Item.ItemIndex];
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
string member = (string)DataList2.DataKeys[e.Item.ItemIndex];
SqlCommand cmd = new SqlCommand("accept_member", conn);
cmd.CommandType = CommandType.StoredProcedure;
string email = Session["email"].ToString();
int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
cmd.Parameters.Add(new SqlParameter("@team_ID", team_ID));
cmd.Parameters.Add(new SqlParameter("@myemail", email));
cmd.Parameters.Add(new SqlParameter("@member", member));
cmd.Parameters.Add(new SqlParameter("@respond", "0"));
SqlParameter count = cmd.Parameters.Add("@count", SqlDbType.Int);
count.Direction = ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
if (@count.Value.ToString().Equals("1"))
{
Response.Write(@member + " " + "has been rejected");
}
else
{
Response.Write("team is full or not found");
}
}
DataList2.DataBind();
}
Error
The name "DataList2" does not exist in the current context
Assuming that your data produces more than one row in DataList1, there is more than one DataList2. ASP.NET therefore doesn't make DataList2 a member of your page (codebehind) class.
Fortunately, in this instance sender (which you called source, same thing) should be the DataList2 that you want. Try casting it to DataList and see what happens.