?
Solved

CheckedChanged Not Firing in DataList

Posted on 2015-01-05
6
Medium Priority
?
101 Views
Last Modified: 2015-01-07
When I click on the checkbox, I want the value to be inserted into the database. What am I doing wrong?

                    <asp:DataList ID="PicturesView" DataKeyField="PicturesID"  runat="server">
                    <ItemTemplate>
                        <asp:CheckBox ID="PrimaryPhoto" AutoPostBack="true" OnCheckedChanged="PrimaryPhoto_CheckedChanged" runat="server" />
			<asp:Image ID="PictureImage" ImageUrl='<%#Eval("PicturePath")%>' runat="server" />
                    </ItemTemplate>
                </asp:DataList>    

	protected void PrimaryPhoto_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            DataListItem item = (DataListItem)chk.NamingContainer;
            if (chk.Checked)
            {
                string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

Open in new window

0
Comment
Question by:RecipeDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 1000 total points
ID: 40531641
I would put a catch in your try statement to see what type of error is being generated
that's a good palce to start
try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
 catch (Exception ex)
{
// make a label
lbl1.text = ex.message;
}
                finally
                {
                    conn.Close();
                }

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40531665
I am not getting any error message.

            CheckBox chk = sender as CheckBox;
            DataListItem item = (DataListItem)chk.NamingContainer;
            if (chk.Checked)
            {
                string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    label1.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

Open in new window

0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40531670
you Stepped through everything with F8 ?


try it also without the if statement
 string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    label1.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 1

Author Comment

by:RecipeDan
ID: 40531693
Yes. I even tried just a Response.Redirect("http://www.google.com") by itself just to see if the oncheckedchanged will fire and nothing happens.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 1000 total points
ID: 40534001
- Did you try setting breakpoint in Page_Load?
- Are you doing any DataBinding in PageLoad?
- Are there any javascript errors?: If yes, they prevent postback
- Do you have any validations on the page? - If some validation is failing, it will prevent postback
- Make sure your page don't have AutoEventWireup="false",

Share your entire .aspx and .cs code.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40536053
Thank you both for your assistance. It turns out the error that nothing to do with the oncheckedchanged. A validator was causing the oncheckedchanged not to fire.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question