Share Blog

Friday, May 09, 2014

How to Auto Generate Registration Random Number fill the Form

-------First create Table for Database-----

-------------source Code For Design Code--------

----------RagistrationPage Aspx code---------
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 _Default : System.Web.UI.Page
{
 SqlConnection con =new SqlConnection("Data Source=.;Initial Catalog=adonetpractices;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string regno = "";
        regno = TextBox1.Text.Trim().Substring(0, 2).ToUpper();
        regno += DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString();
        Random rr = new Random();
        regno += rr.Next().ToString();
        con.Open();
       
        SqlCommand cmd = new SqlCommand("insert into employee values(@regno,@Name,@email,@mobile,@gender,@dob,@photo,@state,@city,@course)", con);
        cmd.Parameters.AddWithValue("@regno", regno);
        cmd.Parameters.AddWithValue("@name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@email", TextBox2.Text);
        cmd.Parameters.AddWithValue("@mobile", TextBox3.Text);
        cmd.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@dob", TextBox4.Text);
        cmd.Parameters.AddWithValue("@photo", FileUpload1.FileName);
        cmd.Parameters.AddWithValue("@state", DropDownList1.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@city", DropDownList2.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@course", CheckBoxList1.SelectedItem.Text);
        string course = "";
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                course +=li.Text ;
            }
        }
        course = course.Remove(course.Length - 1);
        int i = cmd.ExecuteNonQuery();

        if (i == 1)
        {
            Response.Write("<script>alert('" + regno + "')</script>");
        }
        else
        {
            Response.Write("<script>alert('data not submit')</script>");
        }
        con.Close();
       
    }

}
-------Result------



No comments:

Post a Comment