.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Gridview with SQLDatasource in ASP.Net

Posted By:Narayanan       Posted Date: October 31, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Last Article i explained the How to use "SQLDatasource" in ASP.net Now i will give an example of this task.
 

 Using in the Project:
   1)SQLDatasource
   2)Ajax Toolkit
   3)Gridview
   4)Table
   5) CSS File
   6)Validators

Project Des:
  It is a simple Project ," Registration Form".
Inputs are :
   First Name,Last Name,Age,Date of Birth
Age should be grater than 25 and Lesser than 80
Date of Birth should be "dd/MM/yyyy" Format.
Information are view in the Grid view
Now Coding :

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Theme="Application" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="atk" %>




   Registration Form
   

   
     

        <atk:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
       

Registration Form


   
   
   
   
   
   
    

      
   
   
   
   
   
   
   
   
   
   
   
   
      
First Name:
Last Name:
Age:
  
DOB:
       
       

      

      

      
      
      
      
      
      
      

      

      

      

      
      

      

   
   


   

                    BackColor="White" BorderColor="White" BorderWidth="2px" CellPadding="3" 
            AllowPaging="True" AllowSorting="True"  
            onpageindexchanging="GridView1_PageIndexChanging" Height="350px"
            onrowdeleting="GridView1_RowDeleting"  onsorting="GridView1_Sorting"
            Width="500px" CellSpacing="1">
           
           
           
           
                            HorizontalAlign="Center" VerticalAlign="Middle" />

                      InsertCommand="INSERT INTO tbl_registration_form (First_Name,Last_Name,reg_age,reg_dob) VALUES (@First_Name, @Last_name,@reg_Age,@reg_dob)" >
           
               
               
               
               
           

       

       

    



Here :
 I just call a  Theme for this page.So i add Theme="Name of the Theme"
before using Ajax tool, you should add this line atk:ToolkitScriptManager ID
atk -- TagPrefix for Ajax Tool
In this line for date Format :I am using Ajax calendar

asp:SqlDataSource ID -- For Inserting a information to database

Now Coding Part
   protected void BtnSave_Click(object sender, EventArgs e)
    {
        DateTime MyDate;
        MyDate = ConvertDate(txtDOB.Text);
        SqlDataSource1.InsertParameters["First_Name"].DefaultValue = txtFirst.Text;
        SqlDataSource1.InsertParameters["Last_name"].DefaultValue = txtLast.Text;
        SqlDataSource1.InsertParameters["reg_Age"].DefaultValue = txtAge.Text;
        SqlDataSource1.InsertParameters["reg_dob"].DefaultValue = MyDate.ToShortDateString();
         SqlDataSource1.Insert();
         Message.Text = "Saved Successfully";
        GrdviewData();
        Message.Text = "";
        txtAge.Text = "";
        txtDOB.Text = "";
        txtFirst.Text = "";
        txtLast.Text = "";
       

    }
// To convert Date format
 public static DateTime ConvertDate(string s)
    {

        DateTime time;

        try
        {

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

            DateTime time2 = Convert.ToDateTime(s);

            Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

            time = Convert.ToDateTime(time2);

        }
        catch
        {

            throw;

        }

        return time;

    }

// Data binding for gridview
 void GrdviewData()
    {
        try
        {
           
            string Connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
            SqlConnection con = new SqlConnection(Connectionstring);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select First_Name as [First Name],Last_Name as [Last Name],reg_age as [Age],CONVERT(VARCHAR(10),reg_dob,105) as [Date Of Birth] from tbl_registration_form", Connectionstring);
            da.SelectCommand.CommandType = CommandType.Text;
         
            da.Fill(ds);
            GridView1.DataSource = ds;
           
           
            GridView1.DataBind();
         }
        catch
        {
            Message.Text = "Unable to Connect to the Database";
        }
       
    }
// page Index (i mentioned in the Page Size 10 in the Design  selection
  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        ViewState["sortexp"] = e.SortExpression;
        DataView dv = Grdview();
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }

// Sort a Data in ascending and Descending
    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

Note: Please change your date format in the System(like this "dd/MM/yyyy")






 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend