Javascript Validation Form

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <%--this code for disable cut copy paste but enter number by key--%>
    <script language="javascript">
        function whichButton(event) {
            if (event.button == 2)//RIGHT CLICK
            {
                alert("Not Allow Right Click!");
            }

        }
        function noCTRL(e) {
            var code = (document.all) ? event.keyCode : e.which;

            var msg = "Sorry, this functionality is disabled.";
            if (parseInt(code) == 17) //CTRL
            {
                alert(msg);
                window.event.returnValue = false;
            }
        }
    </script>
    <%--this code for only numeric and strengh completed check--%>
    <script type="text/javascript">
        function ValidNumeric() {
            var p = document.getElementById('<%=txtpin.ClientID %>').value;
            var charCode = (event.which) ? event.which : event.keyCode;
            if (charCode >= 48 && charCode <= 57) {
                return true;
            }
            else if (p < 5) {
                alert('Only numeric number allow !');
                return false;
            }
            else {
                alert('Pin strengh has completed !');
                return false;
            }
        }
    </script>
    <script language="javascript" type="text/javascript">
        function validation() {
            var mobile = document.getElementById('<%=txtmbl.ClientID %>').value;
            var Email = document.getElementById('<%=txtmail.ClientID %>').value;
            var WebUrl = document.getElementById('<%=txturl.ClientID %>').value;
            var p = document.getElementById('<%=txtpin.ClientID %>').value;


            if (p == "") {
                alert("Enter Your Pin");
                document.getElementById("<%=txtpin.ClientID%>").focus();
                return false;
            }


            //            var digits = "0123456789";
            //            var temp;
            //            for (var i = 0; i < document.getElementById("<%=txtpin.ClientID %>").value.length; i++) {
            //                temp = document.getElementById("<%=txtpin.ClientID%>").value.substring(i, i + 1);
            //                if (digits.indexOf(temp) == -1) {
            //                    alert("Please enter correct pin code");
            //                    document.getElementById("<%=txtpin.ClientID%>").focus();
            //                    return false;
            //                }
            //            }

            if (WebUrl == "") {
                alert("Enter Your URL");
                document.getElementById("<%=txturl.ClientID %>").focus();
                return false;
            }
            var Url = "^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";
            var matchURL = WebUrl.match(Url);
            if (matchURL == null) {
                alert("Web URL does not look valid");
                document.getElementById("<%=txturl.ClientID %>").focus();
                return false;
            }

            if (Email == "") {
                alert("Enter Your Email");
                document.getElementById("<%=txtmail.ClientID%>").focus();
                return false;
            }
            var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/
            var EmailmatchArray = Email.match(emailPat);
            if (EmailmatchArray == null) {
                alert("Your email must be this format eg. k@gmail.com !");
                document.getElementById("<%=txtmail.ClientID%>").focus();
                return false;
            }
            if (mobile == "") {
                alert("Enter Your Mobile Number !");
                document.getElementById("<%=txtmbl.ClientID%>").focus();
                return false;
            }
            var phoneno = /^\d{10}$/
            var EmailmatchArray = mobile.match(phoneno);
            if (EmailmatchArray == null) {
                alert("Number must be 10 digit !");
                document.getElementById("<%=txtmbl.ClientID%>").focus();
                return false;
            }

            if (document.getElementById('<%=txtname.ClientID%>').value == "") {
                alert('First Name must be required !');
                document.getElementById('<%=txtname.ClientID%>').focus();
                return false;
            }
            if (document.getElementById("<%=txtlname.ClientID%>").value == "") {
                alert('Last Name must be required !');
                document.getElementById("<%=txtlname.ClientID%>").focus();
                return false;
            }
            if (!document.getElementById('<%=rdomale.ClientID%>').checked && !document.getElementById('<%=rdofemale.ClientID%>').checked) {
                alert("Gender must be required !");
                document.getElementById('<%=rdomale.ClientID%>').focus();
                return false;
            }
            var atLeast = 1
            var CHK = document.getElementById('<%=CheckBoxList1.ClientID%>');
            var checkbox = CHK.getElementsByTagName("input");
            var counter = 0;
            for (var i = 0; i < checkbox.length; i++) {
                if (checkbox[i].checked) {
                    counter++;
                }
            }
            if (atLeast > counter) {
                alert('Please select one hobbies !');

                document.getElementById('<%=CheckBoxList1.ClientID%>').focus();
                return false;

            }

            var result = document.getElementById('<%=ddlEducation.ClientID%>').value.toString();
            if (result == "0") {
                alert('Please select a streem !');
                document.getElementById('<%=ddlEducation.ClientID%>').focus();
                return false;

            }
        }

    </script>
    <%--var ddltrader = document.getElementById('<%=ddltradername.ClientID%>');
            var ddlproductype1 = ddltrader.options[ddltrader.selectedIndex].value;--%>
    <style type="text/css">
        .style1
        {
            width: 50%;
            background-color: #FFCC99;
        }
        .style3
        {
            width: 68px;
        }
    </style>
</head>
<body>
    <form runat="server" id="form1">
    <table align="center" class="style1">
        <tr>
            <td class="style3">
                Pin
            </td>
            <td>
                <asp:TextBox ID="txtpin" runat="server" MaxLength="5" onMouseDown="whichButton(event)"
                    onKeyDown="return noCTRL(event)" onkeypress="return ValidNumeric()"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Url
            </td>
            <td>
                <asp:TextBox ID="txturl" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Email
            </td>
            <td>
                <asp:TextBox ID="txtmail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Mobile
            </td>
            <td>
                <asp:TextBox ID="txtmbl" MaxLength="10" onkeypress="return ValidNumeric()" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <tr>
                <td class="style3">
                    First Name
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Last Name
                </td>
                <td>
                    <asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Gender
                </td>
                <td>
                    <asp:RadioButton ID="rdomale" runat="server" GroupName="k" name="gender" Text="Male" />
                    <asp:RadioButton ID="rdofemale" runat="server" GroupName="k" name="gender" Text="Female" />
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Hobbies
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                        <asp:ListItem Text="Cricket" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Hockey" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Chess" Value="3"></asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Country
                </td>
                <td>
                    <asp:DropDownList ID="ddlEducation" runat="server">
                        <asp:ListItem Text="--Select Education--" Value="0" />
                        <asp:ListItem Text="B.Tech" Value="B.Tech" />
                        <asp:ListItem Text="MCA" Value="MCA" />
                        <asp:ListItem Text="MBA" Value="MBA" />
                        <asp:ListItem Text="AGBSC" Value="AGBSC" />
                        <asp:ListItem Text="MBBS" Value="MBBS" />
                    </asp:DropDownList>
                </td>
            </tr>
            <td class="style3">
                &nbsp;
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validation();" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Share:

How to create dynamic textboxes and stored textboxes values save in SQL database in C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
        <asp:Table id="Table1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" Font-Bold="True" >
   
        </asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
</asp:PlaceHolder><BR><BR>
        <br />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="btn" runat="server" Text="Button" onclick="btn_Click1" />
    </form>
    </body></html>
...................................................................................................................................
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.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(@"Data Source=COMTECH-PC\SQLEXPRESS;database=kabir;integrated security=true;");

     protected void Page_Load(object sender, EventArgs e)
    {
     
        if (ViewState["Count"] != null)
        {
         
          
            //AddTextBoxs();
            //AddButton();
            AddTextBoxs_Session();
         
        }
    }
     private void AddTextBoxs()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
           

         }


     }

     private void AddTextBoxs_Session()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
             cn.Open();
             SqlCommand cmd = new SqlCommand("alter table pratima add " + (t.ID).ToString() + " varchar(50)", cn);
             cmd.ExecuteNonQuery();
             cn.Close();

         }

     }

    //private void AddButton()
    //{
    //    Button b = new Button();
    //    b.ID = "btn";
    //    b.Text = "Button";
    //    b.Click += new System.EventHandler(btn_Click);
    //    PlaceHolder1.Controls.Add(b);
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddTextBoxs();
       // if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;

    }
  
    protected void Button2_Click(object sender, EventArgs e)
    {
      
       // AddButton();

        clear();
        AddTextBoxs();
        //if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
    }
  
    public void bindgrid()
    {
        //SqlConnection cn = new SqlConnection("Data Source=SQLDB;User ID=Demoh;Password=Demo1@");
        //SqlCommand cmd = new SqlCommand("select * from poll", cn);
        //DataTable dt = new DataTable();
        //SqlDataAdapter sa = new SqlDataAdapter();
        //cn.Open();
        //sa.SelectCommand = cmd;
        //cmd.ExecuteNonQuery();
        //sa.Fill(dt);
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
    }
    public void clear()
    {
        Table1.Rows.Clear();
        for (int i = 0; i < Table1.Rows.Count; i++)
            Table1.Rows.RemoveAt(i);
    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        //AddTextBoxs_Session();
        
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        try
        {
            for (int i = 1; i < Table1.Rows.Count; i++)
            {
                //string dp = ((TextBox)Table1.Rows[i+1].FindControl("txtnm" + i+1)).Text;
                TextBox tnm = (TextBox)Table1.Rows[i].FindControl("CN" + i);

                //TextBox tsal = (TextBox)Table1.Rows[i].FindControl("txtsal" + i);
                //DropDownList tyear = (DropDownList)Table1.Rows[i].FindControl("dpl" + i);
                string colname = "CN" + i;
                string str="select * from pratima";
                SqlDataAdapter da=new SqlDataAdapter(str,cn);
                DataSet ds=new DataSet();
                da.Fill(ds);
                int k=Convert.ToInt32(ds.Tables[0].Rows.Count);
                if(k<=0)
                {
                SqlCommand cmd1 = new SqlCommand("insert into  pratima (" + colname + ") values('" + tnm.Text + "')",cn);
                cmd1.ExecuteNonQuery();
                }
                else
                {

                    SqlCommand cmd1 = new SqlCommand("update  pratima set " + colname + "='" + tnm.Text + "' where id="+k+"", cn);
                    cmd1.ExecuteNonQuery();
                }
                

                // bindgrid();
            }
        }
            catch(Exception ex)
            {
               
            }
        finally
            {
            cn.Close();
             clear();
            }

        }

        
    
}


Share:

Bind database table in html format at asp.net page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Display Data in Dynamically Created Table</title>
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 50%; text-align: center; background-color: skyblue;">
            <tr>
                <td align="center">
                    <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
..............................................................
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.Data.SqlClient;  
using System.Text;  
  

public partial class Default2 : System.Web.UI.Page
{
 
        SqlDataAdapter da;  
        DataSet ds = new DataSet();  
        StringBuilder htmlTable = new StringBuilder();  
  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack)  
                BindData();  
        }  
  
        private void BindData()  
        {  
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=COMTECH-PC\SQLEXPRESS;Integrated Security=true;Initial Catalog=kabir";  
            SqlCommand cmd = new SqlCommand("SELECT * FROM employee", con);  
            da = new SqlDataAdapter(cmd);  
            da.Fill(ds);  
            con.Open();  
            cmd.ExecuteNonQuery();  
            con.Close();  
  
            htmlTable.Append("<table border='1'>");  
            htmlTable.Append("<tr style='background-color:green; color: White;'><th>ID.</th><th>Name</th><th>Salary</th></tr>");  
  
            if (!object.Equals(ds.Tables[0], null))  
            {  
                if (ds.Tables[0].Rows.Count > 0)  
                {  
  
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
                    {  
                        htmlTable.Append("<tr style='color: White;'>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["ID"] + "</td>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Name"] + "</td>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Salary"] + "</td>");  
                        htmlTable.Append("</tr>");  
                    }  
                    htmlTable.Append("</table>");  
                    DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });  
                }  
                else  
                {  
                    htmlTable.Append("<tr>");  
                    htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");  
                    htmlTable.Append("</tr>");  
                }  
            }  
        }  
    }  
 

Share:

Tuesday, 23 June 2015

Javascript Validation Form

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <%--this code for disable cut copy paste but enter number by key--%>
    <script language="javascript">
        function whichButton(event) {
            if (event.button == 2)//RIGHT CLICK
            {
                alert("Not Allow Right Click!");
            }

        }
        function noCTRL(e) {
            var code = (document.all) ? event.keyCode : e.which;

            var msg = "Sorry, this functionality is disabled.";
            if (parseInt(code) == 17) //CTRL
            {
                alert(msg);
                window.event.returnValue = false;
            }
        }
    </script>
    <%--this code for only numeric and strengh completed check--%>
    <script type="text/javascript">
        function ValidNumeric() {
            var p = document.getElementById('<%=txtpin.ClientID %>').value;
            var charCode = (event.which) ? event.which : event.keyCode;
            if (charCode >= 48 && charCode <= 57) {
                return true;
            }
            else if (p < 5) {
                alert('Only numeric number allow !');
                return false;
            }
            else {
                alert('Pin strengh has completed !');
                return false;
            }
        }
    </script>
    <script language="javascript" type="text/javascript">
        function validation() {
            var mobile = document.getElementById('<%=txtmbl.ClientID %>').value;
            var Email = document.getElementById('<%=txtmail.ClientID %>').value;
            var WebUrl = document.getElementById('<%=txturl.ClientID %>').value;
            var p = document.getElementById('<%=txtpin.ClientID %>').value;


            if (p == "") {
                alert("Enter Your Pin");
                document.getElementById("<%=txtpin.ClientID%>").focus();
                return false;
            }


            //            var digits = "0123456789";
            //            var temp;
            //            for (var i = 0; i < document.getElementById("<%=txtpin.ClientID %>").value.length; i++) {
            //                temp = document.getElementById("<%=txtpin.ClientID%>").value.substring(i, i + 1);
            //                if (digits.indexOf(temp) == -1) {
            //                    alert("Please enter correct pin code");
            //                    document.getElementById("<%=txtpin.ClientID%>").focus();
            //                    return false;
            //                }
            //            }

            if (WebUrl == "") {
                alert("Enter Your URL");
                document.getElementById("<%=txturl.ClientID %>").focus();
                return false;
            }
            var Url = "^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";
            var matchURL = WebUrl.match(Url);
            if (matchURL == null) {
                alert("Web URL does not look valid");
                document.getElementById("<%=txturl.ClientID %>").focus();
                return false;
            }

            if (Email == "") {
                alert("Enter Your Email");
                document.getElementById("<%=txtmail.ClientID%>").focus();
                return false;
            }
            var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/
            var EmailmatchArray = Email.match(emailPat);
            if (EmailmatchArray == null) {
                alert("Your email must be this format eg. k@gmail.com !");
                document.getElementById("<%=txtmail.ClientID%>").focus();
                return false;
            }
            if (mobile == "") {
                alert("Enter Your Mobile Number !");
                document.getElementById("<%=txtmbl.ClientID%>").focus();
                return false;
            }
            var phoneno = /^\d{10}$/
            var EmailmatchArray = mobile.match(phoneno);
            if (EmailmatchArray == null) {
                alert("Number must be 10 digit !");
                document.getElementById("<%=txtmbl.ClientID%>").focus();
                return false;
            }

            if (document.getElementById('<%=txtname.ClientID%>').value == "") {
                alert('First Name must be required !');
                document.getElementById('<%=txtname.ClientID%>').focus();
                return false;
            }
            if (document.getElementById("<%=txtlname.ClientID%>").value == "") {
                alert('Last Name must be required !');
                document.getElementById("<%=txtlname.ClientID%>").focus();
                return false;
            }
            if (!document.getElementById('<%=rdomale.ClientID%>').checked && !document.getElementById('<%=rdofemale.ClientID%>').checked) {
                alert("Gender must be required !");
                document.getElementById('<%=rdomale.ClientID%>').focus();
                return false;
            }
            var atLeast = 1
            var CHK = document.getElementById('<%=CheckBoxList1.ClientID%>');
            var checkbox = CHK.getElementsByTagName("input");
            var counter = 0;
            for (var i = 0; i < checkbox.length; i++) {
                if (checkbox[i].checked) {
                    counter++;
                }
            }
            if (atLeast > counter) {
                alert('Please select one hobbies !');

                document.getElementById('<%=CheckBoxList1.ClientID%>').focus();
                return false;

            }

            var result = document.getElementById('<%=ddlEducation.ClientID%>').value.toString();
            if (result == "0") {
                alert('Please select a streem !');
                document.getElementById('<%=ddlEducation.ClientID%>').focus();
                return false;

            }
        }

    </script>
    <%--var ddltrader = document.getElementById('<%=ddltradername.ClientID%>');
            var ddlproductype1 = ddltrader.options[ddltrader.selectedIndex].value;--%>
    <style type="text/css">
        .style1
        {
            width: 50%;
            background-color: #FFCC99;
        }
        .style3
        {
            width: 68px;
        }
    </style>
</head>
<body>
    <form runat="server" id="form1">
    <table align="center" class="style1">
        <tr>
            <td class="style3">
                Pin
            </td>
            <td>
                <asp:TextBox ID="txtpin" runat="server" MaxLength="5" onMouseDown="whichButton(event)"
                    onKeyDown="return noCTRL(event)" onkeypress="return ValidNumeric()"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Url
            </td>
            <td>
                <asp:TextBox ID="txturl" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Email
            </td>
            <td>
                <asp:TextBox ID="txtmail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Mobile
            </td>
            <td>
                <asp:TextBox ID="txtmbl" MaxLength="10" onkeypress="return ValidNumeric()" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <tr>
                <td class="style3">
                    First Name
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Last Name
                </td>
                <td>
                    <asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Gender
                </td>
                <td>
                    <asp:RadioButton ID="rdomale" runat="server" GroupName="k" name="gender" Text="Male" />
                    <asp:RadioButton ID="rdofemale" runat="server" GroupName="k" name="gender" Text="Female" />
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Hobbies
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                        <asp:ListItem Text="Cricket" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Hockey" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Chess" Value="3"></asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Country
                </td>
                <td>
                    <asp:DropDownList ID="ddlEducation" runat="server">
                        <asp:ListItem Text="--Select Education--" Value="0" />
                        <asp:ListItem Text="B.Tech" Value="B.Tech" />
                        <asp:ListItem Text="MCA" Value="MCA" />
                        <asp:ListItem Text="MBA" Value="MBA" />
                        <asp:ListItem Text="AGBSC" Value="AGBSC" />
                        <asp:ListItem Text="MBBS" Value="MBBS" />
                    </asp:DropDownList>
                </td>
            </tr>
            <td class="style3">
                &nbsp;
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validation();" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Monday, 1 June 2015

How to create dynamic textboxes and stored textboxes values save in SQL database in C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
        <asp:Table id="Table1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" Font-Bold="True" >
   
        </asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
</asp:PlaceHolder><BR><BR>
        <br />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="btn" runat="server" Text="Button" onclick="btn_Click1" />
    </form>
    </body></html>
...................................................................................................................................
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.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(@"Data Source=COMTECH-PC\SQLEXPRESS;database=kabir;integrated security=true;");

     protected void Page_Load(object sender, EventArgs e)
    {
     
        if (ViewState["Count"] != null)
        {
         
          
            //AddTextBoxs();
            //AddButton();
            AddTextBoxs_Session();
         
        }
    }
     private void AddTextBoxs()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
           

         }


     }

     private void AddTextBoxs_Session()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
             cn.Open();
             SqlCommand cmd = new SqlCommand("alter table pratima add " + (t.ID).ToString() + " varchar(50)", cn);
             cmd.ExecuteNonQuery();
             cn.Close();

         }

     }

    //private void AddButton()
    //{
    //    Button b = new Button();
    //    b.ID = "btn";
    //    b.Text = "Button";
    //    b.Click += new System.EventHandler(btn_Click);
    //    PlaceHolder1.Controls.Add(b);
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddTextBoxs();
       // if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;

    }
  
    protected void Button2_Click(object sender, EventArgs e)
    {
      
       // AddButton();

        clear();
        AddTextBoxs();
        //if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
    }
  
    public void bindgrid()
    {
        //SqlConnection cn = new SqlConnection("Data Source=SQLDB;User ID=Demoh;Password=Demo1@");
        //SqlCommand cmd = new SqlCommand("select * from poll", cn);
        //DataTable dt = new DataTable();
        //SqlDataAdapter sa = new SqlDataAdapter();
        //cn.Open();
        //sa.SelectCommand = cmd;
        //cmd.ExecuteNonQuery();
        //sa.Fill(dt);
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
    }
    public void clear()
    {
        Table1.Rows.Clear();
        for (int i = 0; i < Table1.Rows.Count; i++)
            Table1.Rows.RemoveAt(i);
    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        //AddTextBoxs_Session();
        
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        try
        {
            for (int i = 1; i < Table1.Rows.Count; i++)
            {
                //string dp = ((TextBox)Table1.Rows[i+1].FindControl("txtnm" + i+1)).Text;
                TextBox tnm = (TextBox)Table1.Rows[i].FindControl("CN" + i);

                //TextBox tsal = (TextBox)Table1.Rows[i].FindControl("txtsal" + i);
                //DropDownList tyear = (DropDownList)Table1.Rows[i].FindControl("dpl" + i);
                string colname = "CN" + i;
                string str="select * from pratima";
                SqlDataAdapter da=new SqlDataAdapter(str,cn);
                DataSet ds=new DataSet();
                da.Fill(ds);
                int k=Convert.ToInt32(ds.Tables[0].Rows.Count);
                if(k<=0)
                {
                SqlCommand cmd1 = new SqlCommand("insert into  pratima (" + colname + ") values('" + tnm.Text + "')",cn);
                cmd1.ExecuteNonQuery();
                }
                else
                {

                    SqlCommand cmd1 = new SqlCommand("update  pratima set " + colname + "='" + tnm.Text + "' where id="+k+"", cn);
                    cmd1.ExecuteNonQuery();
                }
                

                // bindgrid();
            }
        }
            catch(Exception ex)
            {
               
            }
        finally
            {
            cn.Close();
             clear();
            }

        }

        
    
}


Bind database table in html format at asp.net page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Display Data in Dynamically Created Table</title>
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 50%; text-align: center; background-color: skyblue;">
            <tr>
                <td align="center">
                    <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
..............................................................
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.Data.SqlClient;  
using System.Text;  
  

public partial class Default2 : System.Web.UI.Page
{
 
        SqlDataAdapter da;  
        DataSet ds = new DataSet();  
        StringBuilder htmlTable = new StringBuilder();  
  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack)  
                BindData();  
        }  
  
        private void BindData()  
        {  
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=COMTECH-PC\SQLEXPRESS;Integrated Security=true;Initial Catalog=kabir";  
            SqlCommand cmd = new SqlCommand("SELECT * FROM employee", con);  
            da = new SqlDataAdapter(cmd);  
            da.Fill(ds);  
            con.Open();  
            cmd.ExecuteNonQuery();  
            con.Close();  
  
            htmlTable.Append("<table border='1'>");  
            htmlTable.Append("<tr style='background-color:green; color: White;'><th>ID.</th><th>Name</th><th>Salary</th></tr>");  
  
            if (!object.Equals(ds.Tables[0], null))  
            {  
                if (ds.Tables[0].Rows.Count > 0)  
                {  
  
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
                    {  
                        htmlTable.Append("<tr style='color: White;'>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["ID"] + "</td>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Name"] + "</td>");  
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Salary"] + "</td>");  
                        htmlTable.Append("</tr>");  
                    }  
                    htmlTable.Append("</table>");  
                    DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });  
                }  
                else  
                {  
                    htmlTable.Append("<tr>");  
                    htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");  
                    htmlTable.Append("</tr>");  
                }  
            }  
        }  
    }  
 

Popular

Total Pageviews

Archive