Thursday 8 August 2013

HOW TO ADD DATE FIELD IN GRIDVIEW AND SAVING IT TO DATABASE IN ASP.NET(C#)


Under <head> add this Script


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="Stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function () {
            $("[id$=Date]").datepicker({
                showOn: 'button',
                buttonImageOnly: true,
                buttonImage: 'calendaricon.jpg'            });
        });
     </script>



You can download the JQUERY from the following link.

Now add this in gridview.


                 <asp:TemplateField HeaderText=" Date">
                 <ItemTemplate>
                 <asp:TextBox ID="Date" runat="server" ReadOnly = "true"></asp:TextBox>
                 </ItemTemplate>
                 </asp:TemplateField>



At CS page give this code.


string tempdate = Request.Form[thisGridViewRow.FindControl("Date").UniqueID];
SqlConnection con = new SqlConnection(connectionstring);
string query = "insert into table values('" +tempdate+ "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

0 comments:

Post a Comment