2014年7月13日 星期日

[RESOLVED] Updating dropdown list in the datalist


Hi there,


I have a datalist where by have a dropdown list that populate from database. But when I update the information, the system giving me this error:


System.NullReferenceException: Object reference not set to an instance of an object.


And focus in this line 


String Category =  ((DropDownList)e.Item.FindControl("Category")).Text;

Below is my code:


                            RepeatColumns="1" DataKeyField="ID" 
class="Full-table" CellPadding="4" ForeColor="#333333"
OnItemDataBound="dlPromotionList_ItemDataBound"
OnEditCommand="dlPromotionList_EditCommand"
OnCancelCommand="dlPromotionList_CancelCommand"
OnDeleteCommand="dlPromotionList_DeleteCommand"
OnUpdateCommand="dlPromotionList_UpdateCommand"
>






















 


 


Category  

 
 





























 
 
Category  


   
 






Code behind: onItemBound


protected void dlPromotionList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList Category = (DropDownList)e.Item.FindControl("PromotionCategory");
String catID = ((Label)e.Item.FindControl("lblCat")).Text;

bindCat(Category, catID);

}
}

protected void bindCat(DropDownList Category, string catID) {
string sqlQuery;
// sqlQuery = " SELECT CategorySubID, CategorySubName FROM cs_CategorySubList WHERE CategorySubID = '" + catID + "' AND (Flag = 0) AND (Status = 0) AND CategoryID = 1";
sqlQuery = " SELECT CategorySubID, CategorySubName FROM cs_CategorySubList WHERE CategorySubID = '" + catID + "' AND CategorySubID != 11 AND (Flag = 0) AND (Status = 0) AND CategoryID = 1";
sqlQuery += " UNION SELECT CategorySubID, CategorySubName FROM cs_CategorySubList WHERE (Flag = 0) AND CategorySubID != 11 AND (Status = 0) AND CategoryID = 1";

using (SqlConnection conn = new SqlConnection(connection()))
{
try
{
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Category.DataSource = ds;
Category.DataTextField = "CategorySubName";
Category.DataValueField = "CategorySubID";
// professionlist.SelectedValue = ProfName;
int x = Convert.ToInt32(catID) - 1 ;
Category.SelectedIndex = x;
Category.DataBind();
}
catch (SqlException ex)
{
string msg = "Fetch Error: ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}

}

on update:


protected void dlPromotionList_UpdateCommand(object source, DataListCommandEventArgs e)
{
getSession();
DateTime date = System.DateTime.Now;

string ID = Convert.ToString(dlPromotionList.DataKeys[e.Item.ItemIndex]);
String Title = ((TextBox)e.Item.FindControl("PromotionTitle")).Text;
String Description = ((TextBox)e.Item.FindControl("PromotionDescription")).Text;

String Category = ((DropDownList)e.Item.FindControl("Category")).Text;
String Url = ((TextBox)e.Item.FindControl("PromotionUrl")).Text;
String PromotionDeadline= ((TextBox)e.Item.FindControl("PromotionDeadline")).Text;
String PhotoID = ((Label)e.Item.FindControl("lblPromotionPhoto")).Text;
FileUpload promotionupload = ((FileUpload)e.Item.FindControl("PromotionPicUpload"));

FileUpload img = (FileUpload)promotionupload;
string fileExt = Path.GetExtension(promotionupload.FileName).ToLower();
string x;
string fn = "update";

if (img.HasFile && img.PostedFile != null)
{
deleteRowPromo(PhotoID);

if (fileExt != ".gif" || fileExt != ".jpg" || fileExt != ".jpeg" || fileExt != ".png" || fileExt != ".swf" || fileExt != ".bmp")
{
x = country + id + "+" + ipx + "+" + date + "+" + hour;
updatePic(img, fileExt, x);
savePromotionInformation(fn, ID);
}
else
{
string alert = "alert('You can upload only jpg ,jpg,gif,swf,bmp file');";
ScriptManager.RegisterStartupScript(this, GetType(), "JScript", alert, true);
}
}
using (SqlConnection con = new SqlConnection(connection()))
{
string query = "update ws_Offer set ";
query += " Name = @Title, Description = @Description, Price = @ActualPrice, PromotionDeadline=@PromotionDeadline, PromotionPrice = @PromotionPrice, Category = @Category, Url = @Url, LastModified=@LastModified, LastModifiedBy=@LastModifiedBy, LastModifiedFromIP=@LastModifiedFromIP";
query += " where ID = @ID";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Title", Title);
cmd.Parameters.AddWithValue("@Description", Description);
cmd.Parameters.AddWithValue("@ActualPrice", ActualPrice);
cmd.Parameters.AddWithValue("@PromotionPrice", PromotionPrice);
cmd.Parameters.AddWithValue("@PromotionDeadline", PromotionDeadline);
cmd.Parameters.AddWithValue("@Category", Category);
cmd.Parameters.AddWithValue("@Url", Url);
cmd.Parameters.AddWithValue("@LastModified", date);
cmd.Parameters.AddWithValue("@LastModifiedBy", HiddenGUIDx.Value);
cmd.Parameters.AddWithValue("@LastModifiedFromIP", ipx);
cmd.Parameters.AddWithValue("@ID", ID);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

}
dlPromotionList.EditItemIndex = -1;
BinddlPromotionList();
}













Check the id of the control, make sure you type correct control id.


AFAIK, this is  PromotionCategory instead Category


Try this


DropDownList ddlcat=(DropDownList)e.Item.FindControl("PromotionCategory");
String Category = ddlcat.SelectedItem.Text;







Try this:


String Category = ((DropDownList)e.Item.FindControl("Category")).SelectedItem.Text;



 


沒有留言:

張貼留言