The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Thursday 12 January 2017

How to insert Data in Asp.net to sql with date of birth


In this article I will explain How to insert Data in Asp.net to sql with date of birth. We will use CalendarExtender from AjaxToolkits, TextBox, Label, DropDownList, Button control. We show you long form so you can learn many problem.


Step 1 : We will create table Namely : "User_reg"



CREATE TABLE [dbo].[User_reg]
(
            [userid] [int] IDENTITY(1,1) NOT NULL primary key,
            [Title] [nvarchar](15) NULL,
            [Name] [nvarchar](100) NULL,
            [Father_Name] [nvarchar](100) NULL,
            [DOB] [date] NULL,
            [Occupation] [nvarchar](100) NULL,
            [Address_1] [nvarchar](200) NULL,
            [Address_2] [nvarchar](200) NULL,
            [Village] [nvarchar](50) NULL,
            [Town] [nvarchar](50) NULL,
            [State] [nvarchar](50) NULL,
            [India] [nvarchar](50) NULL,
            [Pin_Code] [nvarchar](15) NULL,
            [Mobile] [nvarchar](11) NULL,
            [Email] [nvarchar](50) NULL,
            [User_Id] [nvarchar](50) NULL,
            [Password] [nvarchar](50) NULL,
            [Utype] [nvarchar](10) NULL
      )

Step 2 : After complition table creation write code in form namely "userreg.aspx". There are below source code:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="userreg.aspx.cs" Inherits="userreg" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width: 100%; border: 1px thin black">
                <tr>
                    <td>Title</td>
                    <td>
                        <asp:DropDownList ID="ddltitle" runat="server" Height="16px" Width="119px">
                            <asp:ListItem>Dr</asp:ListItem>
                            <asp:ListItem>Mr</asp:ListItem>
                            <asp:ListItem>Mrs</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Father&#39;s Name</td>
                    <td>
                        <asp:TextBox ID="txtfathername" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>DOB</td>
                    <td>
                        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
                        <asp:TextBox ID="txtdob" runat="server" TextMode="DateTime"></asp:TextBox>
                        <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtdob"></asp:CalendarExtender>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Occupation</td>
                    <td>
                        <asp:TextBox ID="txtoccupation" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Address 1</td>
                    <td>
                        <asp:TextBox ID="txtaddress1" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Address 2</td>
                    <td>
                        <asp:TextBox ID="txtaddress2" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Village</td>
                    <td>
                        <asp:TextBox ID="txtvillage" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Town</td>
                    <td>
                        <asp:TextBox ID="txttown" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>State</td>
                    <td>
                        <asp:TextBox ID="txtstatus" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>India</td>
                    <td>
                        <asp:TextBox ID="txtcountry" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Pin Code</td>
                    <td>
                        <asp:TextBox ID="txtpincode" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Mobile</td>
                    <td>
                        <asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td>
                        <asp:TextBox ID="txtemil" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>User Id</td>
                    <td>
                        <asp:TextBox ID="txtuserid" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>
                        <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Compare Password</td>
                    <td>
                        <asp:TextBox ID="txtcomparepassword" runat="server"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="btnsubmit" runat="server" Text="Button" OnClick="btnsubmit_Click" />
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>

        </div>
    </form>
</body>
</html>

OutPut :- 

 

How to add CalenderExtender in text box or form

  1. Add ToolkitScriptManager from AjaxToolKits :

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>

   2. Add TextBox form Tools :

<asp:TextBox ID="txtdob" runat="server" TextMode="DateTime"></asp:TextBox>


   3. Add CalendarExtender1 from AjaxToolKits :

<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtdob"></asp:CalendarExtender>



 

After write source code then double click on button to create event onClick.

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.Drawing;
using System.Configuration;

public partial class userreg : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand com =new  SqlCommand(@"INSERT INTO [complaintgo].[dbo].[User_reg]
           ([Title]
           ,[Name]
           ,[Father_Name]
           ,[DOB]
           ,[Occupation]
           ,[Address_1]
           ,[Address_2]
           ,[Village]
           ,[Town]
           ,[State]
           ,[India]
           ,[Pin_Code]
           ,[Mobile]
           ,[Email]
           ,[User_Id]
           ,[Password]
           ,[Utype])
     VALUES
           ('"+ddltitle.Text+"','"+txtname.Text+"','"+txtfathername.Text+"','"+txtdob.Text+"','"+txtoccupation.Text+"','"+txtaddress1.Text+"','"+txtaddress2.Text+"','"+txtvillage.Text+"','"+txttown.Text+"','"+txtstatus.Text+"','"+txtcountry.Text+"','"+txtpincode.Text+"','"+txtmobile.Text+"','"+txtemil.Text+"','"+txtuserid.Text+"','"+txtpassword.Text+"','U') ",con);
            com.ExecuteNonQuery();
            con.Close();
        }
     
       
    }
    protected void txtdob_TextChanged(object sender, EventArgs e)
    {

    }
}

sqlcon is connectionstrings which is take from webconfig file like

<connectionStrings>
    <add name="sqlcon" connectionString="Data Source=amit-pc;Initial Catalog=complaintgo;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>


If any problem with this code please comments


.............Thanks for.............. 
 



 
 

0 comments:

Post a Comment

Do not enter spam link

Popular Posts