locked
I cannot set the value of my textbox to the variable RRS feed

  • Question

  • User419401696 posted

    I have a details view, with item, edit templates. I am populating from a database accessed in c#. The details view looks ok, I can click on the edit button, and my textboxes appear, but when I click on the update button, the database does not update.

    My client side code is:

     <asp:TemplateField HeaderText="TestRef">
                            <ItemTemplate>
                                <asp:Label ID="lblTestRef" runat="server" Text='<%# Bind("TestRef") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtTestRef" runat="server" Text='<%# Bind("TestRef") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="TestDescription"> <ItemTemplate>
                                <asp:Label ID="lblTestDescription" runat="server" Text='<%# Bind("TestDescription") %>'></asp:Label>
                            </ItemTemplate>                      
                            <EditItemTemplate>
                                <asp:TextBox ID="txtTestDescription" runat="server" Text='<%# Bind("TestDescription") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>

    and in c# it is:

                TextBox txtTestRef = new TextBox();
                txtTestRef = (TextBox)DetailsView1.FindControl("TestRef");
                TextBox txtTestDescription = new TextBox();
                txtTestDescription = (TextBox)DetailsView1.FindControl("TestDescription");

    The variable txtTestRef & txtTextDescription are just not populating with whatever I type in the Text Boxes.

    so when the code gets too:

    dbConnection.Open();
                cmd.CommandText = "update tblOrgTest set TestDescription=@TestDescription, where TestRef=@TestRef";
                cmd.Connection = dbConnection;
                cmd.Parameters.AddWithValue("@TestRef", txtTestRef.Text);
                cmd.Parameters.AddWithValue("@TestDescription", txtTestDescription.Text);

    It fails stating the two variables are equal to null.

    Please help!!!

    Saturday, September 30, 2017 3:07 PM

All replies

  • User-707554951 posted

    Hi  danchu_uk,

    Use the following code:

    1.

     TextBox txtTestRef = new TextBox();
                txtTestRef = (TextBox)DetailsView1.Rows[1].FindControl("txtTestRef");
                TextBox txtTestDescription = new TextBox();
                txtTestDescription = (TextBox)DetailsView1.Rows[1].FindControl("txtTestDescription");

    2. 

    update tblOrgTestset TestDescription=@TestDescription where TestRef=@TestRef

    Working sample as below:

        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"  AutoGenerateRows="false" AutoGenerateEditButton="true" OnItemUpdating="DetailsView1_ItemUpdating" OnModeChanging="DetailsView1_ModeChanging">
                <Fields>
                    <asp:TemplateField HeaderText="TestRef">
                        <ItemTemplate>
                            <asp:Label ID="lblTestRef" runat="server" Text='<%# Bind("TestRef") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTestRef" runat="server" Text='<%# Bind("TestRef") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="TestDescription">
                        <ItemTemplate>
                            <asp:Label ID="lblTestDescription" runat="server" Text='<%# Bind("TestDescription") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTestDescription" runat="server" Text='<%# Bind("TestDescription") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>

    CodeBehind:

    protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!this.IsPostBack)
                {
                    this.BindDetailView();
                }
            }
            private void BindDetailView()
            {
                string constr = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("select * from Test2Table"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                DetailsView1.DataSource = dt;
                                DetailsView1.DataBind();
                            }
                        }
                    }
                }
            }
    
            protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
                DetailsView1.ChangeMode(e.NewMode);
                BindDetailView();
            }
    
            protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
            {
                TextBox txtTestRef = new TextBox();
                txtTestRef = (TextBox)DetailsView1.Rows[1].FindControl("txtTestRef");
                TextBox txtTestDescription = new TextBox();
                txtTestDescription = (TextBox)DetailsView1.Rows[1].FindControl("txtTestDescription");
                string constr = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("update Test2Table  set TestDescription=@TestDescription where TestRef=@TestRef"))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@TestRef", txtTestRef.Text);
                        cmd.Parameters.AddWithValue("@TestDescription", txtTestDescription.Text);
                        cmd.ExecuteNonQuery();
                        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                        BindDetailView();
                        con.Close();
                    }
                }
    
            }

    Output:

    Best regards

    Cathy

    Monday, October 2, 2017 7:58 AM
  • User419401696 posted

    Hi Cathy,

    Many thanks for your help. I think the key difference was me using the column names from my query, after the Findcontrol, instead of the named objects from my asp.

    The errant comma on my update.

    I have made the necessary changes, and added the reference to the rows before the Findcontrol.

    Now I have the problem, with the objects value not changing to the edit I make. I have broke the code and confirmed this in debug.

    Feels like this is going to be something silly I have forgotten, being the noob I am. Any ideas???  

    Sunday, October 15, 2017 1:08 PM
  • User-707554951 posted

    Hi  danchu_uk,

    Now I have the problem, with the objects value not changing to the edit I make. I have broke the code and confirmed this in debug.

    Feels like this is going to be something silly I have forgotten, being the noob I am. Any ideas???  

     Only from your description. I couldn't figure out what is your problem.

    For it seems that you original problem has bee solved, this is a new problem which not related to your original problem

    So I suggest you could open a new thread to post your problem above with more detailed description or code would help us to reproduce your problem.

    Best Regards

    Cathy

    Tuesday, October 17, 2017 3:23 AM