Tuesday, August 12, 2014

Auto Increment Text Box

<%@ 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 runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 35%;
            height: 162px;
        }
        .style2
        {
            font-size: x-large;
        }
        .style3
        {
            width: 131px;
        }
        .style4
        {
            width: 103px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <br />
        <table align="center" border="1" style="height: 178px; width: 323px">
            <tr>
                <td class="style2" colspan="2" style="text-align: center">
                    Student Information Register
                </td>
            </tr>
            <tr>
                <td class="style4">
                    Student roll no:-
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtrollid" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style4">
                    Student name:-
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtname" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style4">
                    Student age:-
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtage" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style4">
                    Student mail id:-
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtmailid" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="font-weight: 700"
                        Text="Save" />
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Visible="False"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </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.SqlClient; //don't forget to add
using System.Data;// don't forget to add

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=naresh-PC\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        txtincr();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (con.State == 0)
        {
            con.Open();
        }
        string sqlstr = "insert into student values(" + Convert.ToInt32(txtrollid.Text) + ",'" + txtname.Text + "'," + Convert.ToInt32(txtage.Text) + ",'" + txtmailid.Text + "')";
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.Connection = con;
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();
        Label1.Visible = true;
        Label1.Text = "Record stored";
        cleartext();
        con.Close();
    }
    private void txtincr()
    {
        int a;
        if (txtrollid.Text == "")
        {
            if (con.State == 0)
            {
                con.Open();
            }
            string sqlstr = "select * from student";
            SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
            DataSet dst = new DataSet();
            sda.Fill(dst);
            if (dst.Tables[0].Rows.Count != 0)
            {
                a = dst.Tables[0].Rows.Count;
                a = a + 1;
                txtrollid.Text = Convert.ToString(a);
                txtname.Focus();
            }
            else
            {
                txtrollid.Text = "1";
                txtname.Focus();
            }
            con.Close();
        }
    }

    private void cleartext()
    {
        txtrollid.Text = "";
        txtname.Text = "";
        txtage.Text = "";
        txtmailid.Text = "";
        txtincr();
    }
}