Insert Form (Page) Data Into DataBase using C# in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hello Sir,

Below is the code which i am using to insert the data.

here the data is not getting inserted.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="pension.aspx.cs" Inherits="pension" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="Ajax" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script type="text/javascript">
        function doWork4() {
            var fv48 = document.getElementById('<%=TextBox1.ClientID%>').value;
            var fv33 = document.getElementById('<%=TextBox2.ClientID%>').value;
            var fv62 = document.getElementById('<%=TextBox3.ClientID%>').value;
            if (fv48.length == "") {
                alert("Please enter first name");
                return false;
            }
            if (fv33.length == "") {
                alert("Please enter last name");
                return false;
            }
            var checkBoxList = document.getElementById("<%=chkYesNo.ClientID%>");
            var checkBoxes = checkBoxList.getElementsByTagName("INPUT");
            var value;
            for (var i = 0; i < checkBoxes.length; i++) {
                if (checkBoxes[i].checked) {
                    value = checkBoxes[i].value;
                    if (value == "Yes") {
                        if (fv62.length == 0) {
                            document.getElementById('<%=TextBox3.ClientID%>').removeAttribute("readonly");
                            alert('Please enter passport number');
                            return false;
                        }
                    } else {
                        document.getElementById('<%=TextBox3.ClientID%>').setAttribute("readonly", "readonly");
                    }
                }
            }
            var atLeast1 = 0;
            var counter1 = 0;
            for (var i = 0; i < checkBoxes.length; i++) {
                if (checkBoxes[i].checked) {
                    counter1++;
                }
            }
 
            if (atLeast1 < counter1) {
                if (fv62.length > 0 || value == "No") {
                    //document.getElementById("Button1").click();
                    __doPostBack('Button1', 'OnClick');
                }
            } else {
                alert('Do you have a passport');
                return false;
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <form id="form1" runat="server">
         <Ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release"></Ajax:ToolkitScriptManager>
                                     <div>
        <label>
            First name</label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <label>
            Last name</label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:CheckBoxList runat="server" ID="chkYesNo">
            <asp:ListItem Text="Yes" Value="Yes" />
            <asp:ListItem Text="No" Value="No" />
        </asp:CheckBoxList>
        <label>
            Passport No</label>
        <asp:TextBox ID="TextBox3" runat="server" ReadOnly="true"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="doWork4();return false;" />
    </div>
    </form>
</asp:Content>

 

public partial class pension : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["crm_db"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string str1 = "";
        for (int i = 0; i <= chkYesNo.Items.Count - 1; i++)
        {
            if (chkYesNo.Items[i].Selected)
            {
                if (str1 == "")
                { str1 = chkYesNo.Items[i].Text; }
                else
                { str1 += "," + chkYesNo.Items[i].Text; }
            }
        }
        string name = TextBox1.Text;
        string country = TextBox2.Text;
        string num = TextBox3.Text;
        TextBox1.Text = "";
        TextBox2.Text = "";
        string query = "INSERT INTO pension VALUES(@fname, @lname,@passport,@pass_no)";
        string constr = ConfigurationManager.ConnectionStrings["crm_db"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Parameters.AddWithValue("@fname", name);
                cmd.Parameters.AddWithValue("@lname", country);
                cmd.Parameters.AddWithValue("@passport", str1);
                cmd.Parameters.AddWithValue("@pass_no", num);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

Please help

Thanks

Posted one month ago

I have checked your code. Its working.

Please use try catch to check if any error while Executing the insert Query.