Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone...

I have a winform which saves data into my database...
But now i want to add a DateTimePicker and save it and display it in gridview but i don't know the code it...

Can you please Help me out... Iam giving you my save and display code just for extra information...

This is my save code:

SQL
try
                {
                    da.InsertCommand = new SqlCommand("INSERT INTO Number VALUES(@Serial, @RegistrationNumber, @Size, @Color, @OwnerName, @AmountPaid)", cs);
                    da.InsertCommand.Parameters.Add("@Serial", SqlDbType.VarChar).Value = TxtSerial.Text;
                    da.InsertCommand.Parameters.Add("@RegistrationNumber", SqlDbType.VarChar).Value = TxtRegistrationNumber.Text;
                    da.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar).Value = TxtSize.Text;
                    da.InsertCommand.Parameters.Add("@Color", SqlDbType.VarChar).Value = TxtColor.Text;
                    da.InsertCommand.Parameters.Add("@OwnerName", SqlDbType.VarChar).Value = TxtOwnerName.Text;
                    da.InsertCommand.Parameters.Add("@AmountPaid", SqlDbType.VarChar).Value = TxtAmountPaid.Text;

                    cs.Open();

                    da.InsertCommand.ExecuteNonQuery();
                    cs.Close();

                    ds.Clear();
                    da.Fill(ds);

                    StatusLabel.Text = "Records have been saved";
                }
                catch (Exception)
                {
                    MessageBox.Show(TxtRegistrationNumber.Text + " is already in the database");
                }



This is my Display code, By the way iam displaying in gridview:

C#
private void BtnDisplay_Click(object sender, EventArgs e)
        {
            da.SelectCommand = new SqlCommand("SELECT * FROM Number", cs);
            ds.Clear();
            da.Fill(ds);

            DataGridView.DataSource = ds.Tables[0];

            TableBinding.DataSource = ds.Tables[0];
            TxtSerial.DataBindings.Add(new Binding("Text", TableBinding,"Serial"));
            TxtRegistrationNumber.DataBindings.Add(new Binding("Text", TableBinding, "RegistrationNumber"));
            TxtSize.DataBindings.Add(new Binding("Text", TableBinding, "Size"));
            TxtColor.DataBindings.Add(new Binding("Text", TableBinding, "Color"));
            TxtOwnerName.DataBindings.Add(new Binding("Text", TableBinding, "OwnerName"));
            TxtAmountPaid.DataBindings.Add(new Binding("Text", TableBinding, "AmountPaid"));
            DateTimePickerPlate.DataBindings.Add(new Binding("Date", TableBinding, "Date"));

            records();
        }


Please help me out.
Posted
Updated 12-Dec-11 20:29pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-11 22:30pm    
So, ASP.NET of Forms?
--SA
Maazatron 13-Dec-11 2:29am    
winforms

XML
<script runat="server">

    protected void calEventDate_SelectionChanged(object sender, EventArgs e)
    {
        tbxdateofjoining.Text = calEventDate.SelectedDate.ToString("d");
    }

</script>

    <script type="text/javascript">

        function displayCalendar() {
            var datePicker = document.getElementById('datePicker');
            datePicker.style.display = 'block';
        }

    </script>

<style type="text/css">
        #datePicker
        {
            display:none;
            position:absolute;
            border:solid 2px black;
            background-color:white;
        }
        .content
        {
            width:400px;
            background-color:white;
            margin:auto;
            padding:10px;
        }
        html
        {
            background-color:white;
        }
    </style>




        <asp:TextBox ID="tbxdateofjoining" runat="server"
            style="z-index: 1; left: 555px; top: 394px; position: absolute" ></asp:TextBox>
        <img  alt="" src="Images/calendar-schedulehs-thumb.png" onclick="displayCalendar()"
            style="z-index: 1; left: 714px; top: 394px; position: absolute; height: 20px; width: 20px;" />

    <div id="datePicker">
    <asp:Calendar id="calEventDate" OnSelectionChanged="calEventDate_SelectionChanged" Runat="server" style="z-index: 1; left: 715px; top: 251px; position: absolute; height: 17px" />
    </div>



ON ADD BUTTON CLICK.



    protected void btnadd_Click(object sender, EventArgs e)
    {

        String connectionString = @"Data Source=TECH\SQLEXPRESS2008;Initial Catalog=employee;Integrated Security=True";

        SqlConnection thisConnection = new SqlConnection(connectionString);
        try
        {
            thisConnection.Open();
            String thisQuery = "INSERT INTO EmployeeDetails(FirstName,MiddleName,LastName,Address,Telephone,MobileNo,DateOfBirth,Department,Designation,DateOfJoining,BloodGroup) VALUES ('" + tbxfirstname.Text + "', '" + tbxmiddlename.Text + "', '" + tbxlastname.Text + "', '" + tbxaddress.Text + "', '" + tbxtelephoneno.Text + "', '" + tbxmobileno.Text + "', '" + tbxdateofbirth.Text + "','" + tbxdepartment.Text + "','" + tbxdesignation.Text + "','" + tbxdateofjoining.Text + "','" + tbxbloodgroup.Text + "')";

            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);
            thisCommand.ExecuteNonQuery();
            lblmessage.Text = "Record Added Successfully !!!";
            thisConnection.Close();

        }
        catch (SqlException exp)
        {
            lblmessage.Text = exp.Message;
            Console.WriteLine(exp.Message);
        }

    }
 
Share this answer
 
Found it..
I just had to treat it like a normal text...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900