<asp:TemplateField HeaderText="Grabbelpas">
                    <ItemTemplate>
				        <asp:CheckBox id="lblGrabbelpas" Enabled="false" runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.Grabbelpas") %>' ></asp:CheckBox>
			        </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox id="txtEditGrabbelpas" runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.Grabbelpas") %>' />
                    </EditItemTemplate>                                          
                </asp:TemplateField>
CheckBox txtGrabbelpas = (CheckBox)(myDetailsView.FindControl("txtEditGrabbelpas"));

strSQL = "UPDATE leden SET LidID = "Grabbelpas = '" +  txtGrabbelpas.Checked + "' " +
                " WHERE LidID = '" + userID + "'";

When i go to my detailsview itemtemplate, the checkbox returns the value correct, but every time i update it, it always returns 0.

I'm using a MySQL db and the type of "grabbelpas" is tinyint(1).

If more code is needed just ask. But my other textboxes are updated correctly, it's just the checkbox that doesnt update well.

If i'm correct then you should use "Bind" in the EditItemTemplate, because Eval is only read only.

Checked='<%# Bind("Grabbelpas") %>'

I changed it in the EditItemTemplate to this, but still the same problem

Ok I reproduced the error and found a solution. Your EditItemTemplate should be:

<EditItemTemplate>                        
                        <asp:CheckBox id="txtEditGrabbelpas" runat="server" Checked='<%# Bind("Grabbelpas") %>' />                    
                    </EditItemTemplate>

Then in your code should be:

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
      System.Collections.Specialized.IOrderedDictionary dict = e.NewValues;
      string value = dict["Grabbelpas"].ToString();
}

Your detailsview has the OnItemUpdated="DetailsView1_ItemUpdated" that calls this method.

e has NewValues (after update) and OldValues (before update) so you can see both values. Be sure that the EditItemTemplate uses the Bind("ColumnName").

I hope this solves your problem

I'm sorry, but still the same problem. Maybe I need to explain it different.

When I load the page I see what is in the first image (see attachement). Then I click on edit and i check the checkbox, see the second image. Then i click on update and i see again what is in the 1st image

The 1st codeblock in your first post

line 3: lblGrabbelpas.Checked alway 0 after update?

And in the 2nd codeblock in your first post:

txtGrabbelpas.Checked is always 0 ?

Or both?

If it only is after updating, try DetailsView.DataBind() (if the value is successfuly writen to the database) or maybe you can programmaticaly set the checkbox lblGrabbelpas after the update.

Hope this helps

The 1st codeblock in your first post

line 3: lblGrabbelpas.Checked alway 0 after update?

And in the 2nd codeblock in your first post:

txtGrabbelpas.Checked is always 0 ?

Or both?

If it only is after updating, try DetailsView.DataBind() (if the value is successfuly writen to the database) or maybe you can programmaticaly set the checkbox lblGrabbelpas after the update.

Hope this helps

When I put a "1" in my db, it returns the checkbox correct, but if I want to edit another field via my site, it always returns 0 and in the db it changes to 0 also.

show me any onload code

show me any onload code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;

namespace Project4
{
    public partial class Informatie : System.Web.UI.Page
    {
        //http://www.thaicreate.com/asp.net/c-sharp-asp.net-detailsview-control-edit-update.html
        MySqlConnection cn;
        MySqlCommand cmd;
        String strSQL;
        int userID;

        protected void Page_Load(object sender, EventArgs e)
        {
            String strConnString;
            strConnString = "Server=localhost;User Id=jdn; Password=me; Database=project; Pooling=false";
            cn = new MySqlConnection(strConnString);
            cn.Open();

            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        void BindData()
        {
            strSQL = "SELECT LidID FROM gebruikers WHERE Gebruikersnaam=?Gebruikersnaam;";

            cmd = new MySqlCommand(strSQL, cn);
            MySqlDataReader dr = default(MySqlDataReader);
            cmd.Parameters.Add("?Gebruikersnaam", MySqlDbType.VarChar);
            cmd.Parameters["?Gebruikersnaam"].Value = User.Identity.Name;

            dr = cmd.ExecuteReader();
            dr.Read();
            userID = dr.GetInt32("LidID");
            dr.Close();

            Label1.Text = userID.ToString();

            //SELECT gebruiker aan hand van ID
            MySqlDataAdapter myDataAdapter = default(MySqlDataAdapter);
            DataSet myDataSet = default(DataSet);
            string selUser = "SELECT Naam, Voornaam, Straat, Nummer, Postcode, Gemeente, Telefoon, GSM, Geboortedatum, Email, Contactpersoon, Grabbelpas, Grabbelpasnr, Korting, ExtraInformatie FROM leden WHERE LidID =" + userID;
            myDataAdapter = new MySqlDataAdapter(selUser, cn);
            myDataSet = new DataSet();
            myDataAdapter.Fill(myDataSet, "leden");

            //*** BindData to DetailsView ***//
            myDetailsView.DataSource = myDataSet;
            myDetailsView.DataBind();
        }

        protected void myDetailsView_ModeChanging(Object sender, DetailsViewModeEventArgs e)
        {
            switch (e.NewMode)
            {
                case DetailsViewMode.Edit:
                    myDetailsView.ChangeMode(DetailsViewMode.Edit);
                    break;
                case DetailsViewMode.ReadOnly:
                    myDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
                    break;
            }
            BindData();
        }

        protected void myDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            strSQL = "SELECT LidID FROM gebruikers WHERE Gebruikersnaam=?Gebruikersnaam;";

            cmd = new MySqlCommand(strSQL, cn);
            MySqlDataReader dr = default(MySqlDataReader);
            cmd.Parameters.Add("?Gebruikersnaam", MySqlDbType.VarChar);
            cmd.Parameters["?Gebruikersnaam"].Value = User.Identity.Name;

            dr = cmd.ExecuteReader();
            dr.Read();
            userID = dr.GetInt32("LidID");
            dr.Close();

            TextBox txtNaam = (TextBox)(myDetailsView.FindControl("txtEditNaam"));
            TextBox txtVoornaam = (TextBox)(myDetailsView.FindControl("txtEditVoornaam"));
            TextBox txtStraat = (TextBox)(myDetailsView.FindControl("txtEditStraat"));
            TextBox txtNummer = (TextBox)(myDetailsView.FindControl("txtEditNummer"));
            TextBox txtPostcode = (TextBox)(myDetailsView.FindControl("txtEditPostcode"));
            TextBox txtGemeente = (TextBox)(myDetailsView.FindControl("txtEditGemeente"));
            TextBox txtTelefoon = (TextBox)(myDetailsView.FindControl("txtEditTelefoon"));
            TextBox txtGSM = (TextBox)(myDetailsView.FindControl("txtEditGSM"));
            TextBox txtGeboortedatum = (TextBox)(myDetailsView.FindControl("txtEditGeboortedatum"));
            TextBox txtEmail = (TextBox)(myDetailsView.FindControl("txtEditEmail"));
            TextBox txtContactpersoon = (TextBox)(myDetailsView.FindControl("txtEditContactpersoon"));
            CheckBox txtGrabbelpas = (CheckBox)(myDetailsView.FindControl("txtEditGrabbelpas"));
            TextBox txtGrabbelpasnr = (TextBox)(myDetailsView.FindControl("txtEditGrabbelpasnr"));
            TextBox txtKorting = (TextBox)(myDetailsView.FindControl("txtEditKorting"));
            TextBox txtExtraInformatie = (TextBox)(myDetailsView.FindControl("txtEditExtraInformatie"));

            strSQL = "UPDATE leden SET LidID = '" + userID + "' " +
                " ,Naam = '" + txtNaam.Text + "' " +
                " ,Voornaam = '" + txtVoornaam.Text + "' " +
                " ,Straat = '" + txtStraat.Text + "' " +
                " ,Nummer = '" + txtNummer.Text + "' " +
                " ,Postcode = '" + txtPostcode.Text + "' " +
                " ,Gemeente = '" + txtGemeente.Text + "' " +
                " ,Telefoon = '" + txtTelefoon.Text + "' " +
                " ,GSM = '" + txtGSM.Text + "' " +
                " ,Geboortedatum = '" + txtGeboortedatum.Text + "' " +
                " ,Email = '" + txtEmail.Text + "' " +
                " ,Contactpersoon = '" + txtContactpersoon.Text + "' " +
                " ,Grabbelpas = '" + txtGrabbelpas.Checked + "' " +
                " ,Grabbelpasnr = '" + txtGrabbelpasnr.Text + "' " +
                " ,Korting = '" + txtKorting.Text + "' " +
                " ,ExtraInformatie = '" + txtExtraInformatie.Text + "' " +
                " WHERE LidID = '" + userID + "'";
            cmd = new MySqlCommand(strSQL, cn);
            cmd.ExecuteNonQuery();
            myDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            BindData();
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            System.Collections.Specialized.IOrderedDictionary dict = e.NewValues;
            string value = dict["Grabbelpas"].ToString();
        }
    }
}

That's my whole .aspx.cs code

In your myDetailsView_ItemUpdating try getting the right value of the checkbox using the code i gave you in the DetailsView1_ItemUpdated. It should return the right value.

In your myDetailsView_ItemUpdating try getting the right value of the checkbox using the code i gave you in the DetailsView1_ItemUpdated. It should return the right value.

If i do that it always gives me a System.NullReferenceException: Object reference not set to an instance of an object.

I deleted the line CheckBox txtGrabbelpas = ...
pasted the code you gave me instead of it and changed txtGrabbelpas.Checked to value in my UPDATE statement

Is now your code like this?

System.Collections.Specialized.IOrderedDictionary dict = e.NewValues;
CheckBox txtGrabbelpas = new CheckBox();
txtGrabbelpas.Checked = (bool)dict["Grabbelpas"];

Or is the System.NullReferenceException now at the line txtGrabbelpas.Checked = (bool)dict["Grabbelpas"]?

Is now your code like this?
Or is the System.NullReferenceException now at the line txtGrabbelpas.Checked = (bool)dict["Grabbelpas"]?

Yes, the error is on that line

thanks for info

Ok, another try. I found a way to access the table fields from the Detailsview. I don't know in what row / cell you have the checkbox but you can check the dv.Rows[1].Cells[1].Controls[0].ToString() till you have the checkbox. The Cell will probably be 1.

DetailsView dv = (DetailsView)sender;
CheckBox Grabbelpas = (CheckBox)dv.Rows[2].Cells[1].Controls[0];

I used the "object sender" parameter of the "myDetailsView_ItemUpdating" to get the DetailsView.

I hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.