Question

variable through binding source to sql database

Asked by: garrynoblek

Hi

I have a form with a datetimepicker and a binding source

The date is entered into a variable:
Dim bookingdate As DateTime = DateTimePicker1.Value

How do i then use the bindingsource to bind that variable to the SQL database? The binding source name is BookingsBindingSource

Thanks,
Garry Noble

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-02-23 at 10:26:14ID23187325
Tags

VB .net

Topics

Microsoft Visual Basic.Net

,

Microsoft Visual Studio Express

,

.NET

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. DateTimePicker to DateTime SQL Server format
    Hi All, I have a DateTimePicker on my form in a win32 app. What format do I use to insert the DateTime into an SQL server datetime field!? Thanks
  2. BindingSource?
    I have a datagridview which is bound to a datasource like this: Public Sub RefreshForm() Dim i As Byte 'For i = 1 To dgvTeachers.Rows.Count ' dgvTeachers.Rows.RemoveAt(0) 'Next frmMain.connWorkshop.Open() Dim strSQL As S...
  3. using dates as a bindingsource filter?
    I have a dataset (based on a SQL table) which contains a column named DateOfOrder . The column is set to a datetime data type. I would like to use 2 datetimepickers (dtpFromDate & dtpToDate) to establish dates to filter the dataset with. I have tried: With me.myBindingSo...
  4. Binding DateTimePicker to a dataset
    Hi Experts, I'm having some trouble with my VB.Net MDI app. All my controls (textboxes and comboboxes etc) are bound to a dataset. Now I have to bind several DateTimepickers as well. I've tried binding the database value to the Text or Value properties but neither works. ...
  5. VB.Net DateTimePicker databinding
    I have a datetimepicker on a form which is bound (value not text) to a datetime field in a SQL database. The checkbox is enabled. The field is initially Null value. When the datetimepicker is checked, a date chosen and focus taken away the value does not save to the database....

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SanclerPosted on 2008-02-23 at 15:22:05ID: 20967274

A BindingSource is just one link in the chain between a Control and a DataBase.

It's purpose is to link some property or value of a control (or other class) to a DataSource.  A DataSource is something INTERNAL to the Application - usually a DataTable (or a List of some sort, although it can be a Type).  That DataTable will be "linked" to the DataBase which is EXTERNAL to the Application - in the sense that it gets its data from and can return data to the DataBase - by a TableAdapter or DataAdapter or DataReader.  Those, in turn, use a Connection to pass data back and forth between the external DataBase and the internal DataTable.

What all that means is that you can't just "use the bindingsource to bind that variable to the SQL database".  There will need to be other links in the chain: I assume there are, but we need to know what they are.  What is the DataSource of the BookingsBindingSource?  A DataTable called Bookings?  The value from your DateTimePicker1 would normally go in that, and that would normally happen by you binding the DateTimePicker1 to that DataTable with the BindingSource rather than you putting DateTimePicker1.Value into a separate variable.  And then how does that DataSource (whatever it is) originally get its data?  If it's with a TableAdapter then calling BindingSource.EndEdit and TableAdapter.Update will probably be enough to get the value back to the DataBase.

So, unless that is enough to get you going, can you please tell us some more details about your data set-up and your controls?

Roger

 

by: garrynoblekPosted on 2008-02-24 at 04:17:22ID: 20969620

Roger

Sorry, looking back on my orginal question it seems to vague. Not only does the form contain BookingsBindingSource, but it also has a table adapter called BookingsTableAdapter and a dataset simply known as DataSet. I also have a control called BookingsBindingNavigator. All these controls were preconfigured as I dragged and dropped them from the datasource window onto the form therefore creating the navigation bar along the top of the form and creating the fields in the form of textboxes. Running the program works great with the app communicating with the SQL database and updating any changed fields.
The textboxes come preconfigured as well with databinding. Thats all good and well but I have replaced a textbox with a datetimepicker control and i want to databind this to the 'date' column on my SQL database. See my below code:

Garry

Public Class Form1
 
    Private Sub BookingsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BookingsBindingNavigatorSaveItem.Click
 
        Me.Validate()
        Me.BookingsBindingSource.EndEdit()
        Me.BookingsTableAdapter.Update(Me.DataSet.Bookings)
 
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Me.BookingsTableAdapter.Fill(Me.DataSet.Bookings)
 
    End Sub
 
    Private Sub DateTimePicker1_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
 
        Dim bookingdate As DateTime = DateTimePicker1.Value
 
    End Sub
End Class

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window

 

by: garrynoblekPosted on 2008-02-24 at 04:18:13ID: 20969626

bookingdate is the variable i need to get onto the SQL database

 

by: SanclerPosted on 2008-02-24 at 06:01:58ID: 20969878

Open up your form in Design mode, and click on the DateTimePicker.  In its properties window, if necessary, click on the + sign next to DataBindings to expand it.  Click next to Value and, from the dropdown that appears, select from BookingsBindingSource - expanding it if necessary - and then the name of the field (probably BookingDate) that you want the DTP to be linked to.  That way, the DateTimePicker should be "bound" to your datatable in the same way as the TextBox that it has replaced was.  You should then be able to get rid of your DateTimePicker1_ValueChanged_1 sub.

There can, with DateTimePickers, be some quirks.  For instance, the above assumes that the DataType of the relevant field/column in your datatable is DateTime.  But try the above, and come back if you get problems, saying what they are.

Roger

 

by: garrynoblekPosted on 2008-03-02 at 02:56:27ID: 21025597

Roger,

I have tried this before which would seem to work but then poses me with a problem. Lets say that I have a SQL database with a datetime field in it. When the app is run and it binds the data to the database it puts in the date as eg. 02/03/08 10:55:09. I would need to it to show 02/03/08 00:00:00. There doesn't appear to be any property within the control to achieve this.

Any suggestions?

Thanks,
Garry

 

by: SanclerPosted on 2008-03-02 at 05:32:27ID: 21025926

Have a look at the DateTimePicker's .Format property.  Setting it to Short should display just the date part.  It won't show the 00:00:00, but do you really want to show that?  If you do, then it is achievable.  But it gets more complicated.

Roger

 

by: SanclerPosted on 2008-03-02 at 06:23:49ID: 21026058

On reflection, you might do better to set the .Format property to Custom and put your own pattern in .CustomFormat.  This is because the pattern that is used for Short is dictated by the user's own system settings, and it may not be what you want.  I'm in the UK, so would use "dd/MM/yy" for the date part.  Judging by your time zone, you are too.  But if you want American style dates it would be "MM/dd/yy".  And if you DID want to show "00:00:00" whatever the _real_ part of the value was, you could add that as a literal to the custom format string.  Like this

    myDateTimePicker.CustomFormat = "dd/MM/yy 00:00:00"

or

    myDateTimePicker.CustomFormat = "MM/dd/yy 00:00:00"

Roger

 

by: garrynoblekPosted on 2008-03-02 at 07:41:50ID: 21026332

This doesn't appear to work

At first i put it in the form load sub. Showed up in SQL as it did before
I then moved it to the datetimepicker value changed sub but that didnt work either.

Garry

 

by: SanclerPosted on 2008-03-02 at 08:25:48ID: 21026518

Did you also set

    myDateTimePicker.Format = DateTimePickerFormat.Custom

Or, rather than coding, you can do both steps in the properties window for the DateTimePicker at design time.

Roger

 

by: garrynoblekPosted on 2008-03-02 at 08:51:19ID: 21026587

Not working,

When i run the app the control shows the date and the 00:00:00 but when it binds to the database it still shows the date and the current time ie. the 02/03/08 10:55:09

Garry

 

by: SanclerPosted on 2008-03-02 at 10:13:27ID: 21026863

Are you saying that you want the time part of the value in the DateTime field in the sql database always to _be_ "00:00:00"?  Rather than that, whatever the time part of the value in the DateTime field in the sql database may be, it should always be _presented_ to the user of your app as "00:00:00"?

I thought it was the latter that concerned you.  The former is a slightly different issue.

DateTime values - both in the SQL database and in VB.NET (e.g. as the .Value from a DateTimePicker) - always contain a time part.  You can force that time part to be equivalent to "00:00:00" by creating the DateTime value concerned with just a date.  For example, with

             Dim mydate As DateTime = CDate("01/01/01")

the time part of mydate will be equivalent to "00:00:00".  You can make use of that behaviour to achieve (at least part of) what you want by binding your DateTimePicker's .Text, rather than its .Value, to your datasource, making sure that its .Format is set to something that either exlcudes any time, or - using the custom format string I posted before - includes a "00:00:00" time.  Then, when a user enters a new date, or alters an existing date, the binding will pass back to the datasource the DateTime value of what is _displayed_.  But if a record's date is not altered by the user, and it originally included (from your database) a time part other than "00:00:00", it will remain that way.

Does that achieve what you want?  And is it enough?

Roger

 

by: garrynoblekPosted on 2008-03-02 at 10:28:45ID: 21026927

Roger

Just to summarize. I went into the databinding properties on the datetimepicker control and took out "value" and replaced with "text".
I didn't need to use the variable posted above. It worked fine without.

It now binds the text value of the control to the SQL database into a column defined as datetime

Thanks,
Garry Noble

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...