Home > Software engineering >  Data in update panel is not save in database
Data in update panel is not save in database

Time:01-02

I want to save the data in my modal with is in an update panel to my database.

The value in my textbox are behind save in there variable but there are not save in the database. I do not know why. I use an updModal.Update(); each time because the modal is getting from the database via a list view.

Here is my modal code:

<div  id="studinfo" role="dialog">
    <div >
        <div >
            <div >
                <h4 >Student Info</h4>
                <button type="button" 
                    data-dismiss="modal">
                    &times;</button>
            </div>
            <div >
                    <asp:UpdatePanel ID="updModal" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                <contenttemplate>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

                    <div >
                        <div >
                            <ul >
                                <asp:TextBox ID="txtId" Visible="false" runat="server"></asp:TextBox>
                                <asp:Panel ID="typepannel" CssClass="list-group-item" runat="server">
                                    <asp:Label ID="lbltype" runat="server"
                                        Font-Bold="true" Text="Candidate Type: "></asp:Label>
                                    <asp:Label ID="type" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </asp:Panel>

                                <asp:Panel ID="colpanel" CssClass="list-group-item" runat="server">
                                    <asp:Label ID="lblCol" runat="server"
                                        Font-Bold="true" Text="College Attend: "></asp:Label>
                                    <asp:Label ID="college" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </asp:Panel>

                                <div >
                                    <asp:Label ID="lblName" runat="server"
                                        Font-Bold="true" Text="Name: "></asp:Label>
                                    <asp:Label ID="Name" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </div>

                                <div >
                                    <asp:Label ID="lblNic" runat="server"
                                        Font-Bold="true" Text="Nic: "></asp:Label>
                                    <asp:Label ID="Nic" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </div>

                                <div >
                                    <asp:Label ID="lblAge" runat="server"
                                        Font-Bold="true" Text="Age: "></asp:Label>
                                    <asp:Label ID="Age" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </div>

                                <div >
                                    <asp:Label ID="lblAdress" runat="server"
                                        Font-Bold="true" Text="Address: "></asp:Label>
                                    <asp:Label ID="Address" runat="server"
                                        Text=''>
                                    </asp:Label>

                                </div>

                                <div >
                                    <asp:Label ID="lblacad" runat="server"
                                        Font-Bold="true" Text="Academy: "></asp:Label>
                                    <asp:Label ID="Academy" runat="server"
                                        Text=''>
                                    </asp:Label>
                                </div>

                                <h6 >Parent in Charge</h6>
                                <div >
                                    <asp:Label ID="lblParent" runat="server"
                                        Font-Bold="true" Text="Parent Name: "></asp:Label>
                                    <asp:Label ID="parentName" runat="server"
                                        Text=''>
                                    </asp:Label>
                                </div>
                                <div >
                                    <asp:Label ID="lblNumber" runat="server"
                                        Font-Bold="true" Text="Parent Contact Number: "></asp:Label>
                                    <asp:Label ID="Number" runat="server"
                                        Text=''>
                                    </asp:Label>
                                </div>

                                <div >
                                    <asp:Label ID="lblEmail" runat="server"
                                        Font-Bold="true" Text="Parent Email Address: "></asp:Label>
                                    <asp:Label ID="Email" runat="server"
                                        Text=''>
                                    </asp:Label>
                                </div>

                                <div >
                                    <asp:Label ID="lblResult" runat="server"
                                        Font-Bold="true" Text="Result: "></asp:Label>
                                    <asp:DropDownList ID="ddlResult"
                                        CssClass="select form-control"
                                        runat="server">
                                    </asp:DropDownList>

                                </div>


                                <div >
                                    <asp:Label ID="lblMarks" runat="server" Font-Bold="true" Text="Marks: "></asp:Label>
                                    <asp:TextBox runat="server" ID="txtMarks"
                                        CssClass="form-control form-control-lg" />
                                    <asp:RequiredFieldValidator ID="rfvMarks" runat="server"
                                        Display="Dynamic" ForeColor="Red"
                                        ControlToValidate="txtMarks"
                                        SetFocusOnError="true"
                                        ErrorMessage="Marks is Mandatory">
                                    </asp:RequiredFieldValidator>
                                    <asp:CompareValidator ID="cmpVMarks" runat="server"
                                        ControlToValidate="txtMarks"
                                        Operator="DataTypeCheck"
                                        Type="Double"
                                        SetFocusOnError="true"
                                        Display="Dynamic"
                                        Text="Incorect data type"
                                        ForeColor="Red"
                                        ErrorMessage="Invalid">
                                    </asp:CompareValidator>
                                    <asp:RegularExpressionValidator ID="revPhone"
                                        runat="server"
                                        ControlToValidate="txtMarks"
                                        SetFocusOnError="true"
                                        Display="Dynamic"
                                        ForeColor="Red"
                                        ValidationExpression="^\d\d (\.[1-9])?$"
                                        ErrorMessage="Invalid">

                                    </asp:RegularExpressionValidator>
                                </div>
                                <div >
                                    <asp:Label ID="lblpurcentage" runat="server"
                                        Font-Bold="true" Text="Purcentage : "></asp:Label>
                                    <asp:TextBox runat="server" ID="txtpur"
                                        CssClass="form-control form-control-lg" />
                                    <asp:RequiredFieldValidator
                                        ID="rfvpur" runat="server"
                                        Display="Dynamic" ForeColor="Red"
                                        ControlToValidate="txtpur"
                                        SetFocusOnError="true"
                                        ErrorMessage="Purcentage is Mandatory">
                                    </asp:RequiredFieldValidator>
                                    <asp:CompareValidator ID="cvpur" runat="server"
                                        ControlToValidate="txtpur"
                                        Operator="DataTypeCheck"
                                        Type="Double"
                                        SetFocusOnError="true"
                                        Display="Dynamic"
                                        Text="Incorect data type"
                                        ForeColor="Red"
                                        ErrorMessage="Invalid">
                                    </asp:CompareValidator>
                                </div>
                            </ul>
                        </div>

                    </div>

                 
                                            </ContentTemplate>

                </asp:UpdatePanel>

                   <asp:Button ID="btnSave" OnClick="btnSave_Click" CausesValidation="false"
                        CssClass="btn btn-success btn-block" runat="server" Text="Save" />
            </div>
        </div>

Here is the code behind of the save button:

    protected void btnSave_Click(object sender, EventArgs e)
    {

        try
        {
            updModal.Update();

           
            string id = txtId.Text;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update tblStudent set stud_result=@res,stud_purcentage=@pur,stud_totalMarks=@marks where stud_Id=@id";
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@res", ddlResult.SelectedValue);
            cmd.Parameters.AddWithValue("@pur", txtpur.Text.Trim());
            cmd.Parameters.AddWithValue("@marks", txtMarks.Text.Trim());
            con.Open();
            cmd.Connection = con;

            Label1.Text = txtId.Text;
            gridView();
          
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "LaunchServerSide",
                      "$(function () { showUErrorModal(); });", true);

        }
        finally
        {

            con.Close();

        }

    }

CodePudding user response:

Before Label1.Text = txtId.Text; you write cmd.ExecuteNonQuery()

  • Related