dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Date insert into database sql server

  1. #1

    Thread Starter
    Member Ahmed Shahni's Avatar
    Join Date
    Feb 2011
    Posts
    45

    Resolved [RESOLVED] Date insert into database sql server

    Dear Sir,

    I have a Textbox on my windows Form & I use that text to input date value.

    My problem is how can I use this text box when I insert date into database.

    sql = "Insert into Emp (DOB) values(????)

    Pls guide me.

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,134

    Re: Date insert into database sql server

    Generally you include the date value within single parenthesis i.e '01/01/01', but using parameter is more proper way to do that. You can find more info about parameter on MSDN or JMC's blog which i have linked in my signature


    http://jmcilhinney.blogspot.com/
    Last edited by aashish_9601; Mar 19th, 2011 at 07:05 AM. Reason: looks like the blog link is missing

  3. #3

    Thread Starter
    Member Ahmed Shahni's Avatar
    Join Date
    Feb 2011
    Posts
    45

    Re: Date insert into database sql server

    Sir in my form I have 2 textbox for EmpName and for DOB.. for Name I could enter simply but date if found difficulty.

    This way ..

    sql = "Insert into Emp (EmpName, DOB) values('" & textbox1.text & "',???????) for date how can convert text into date value.

    Thanks a lot.

  4. #4
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,134

    Re: Date insert into database sql server

    like the name part, the same way you should do for the dob.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,877

    Re: [RESOLVED] Date insert into database sql server

    You've already asked this question in another thread. Please keep each thread to a single topic and each topic to a single thread. If you were going to start a new thread for this then you shouldn;t have asked it in the other thread, which started with a different topic.

    Anyway, first things first, the code you have posted in post #3 is not using parameters. Regardless of anything else, you are still using string concatenation to build your SQL, which is bad. Have you read the blog post of mine that both I (in that other thread) and aashish_9601 have recommended? If so then you know how to use parameters, so that's the first change you should make.

    As for the date value, which has nothing specific to do with the data access code, you should probably be using a DateTimePicker rather than a TextBox. The DTP will handle both validation and conversion for you. All you have to do is get the Value property, which is type DateTime. In your case, you don't want the time portion, so you should get the Date property of that DateTime, which returns another DateTime with the same date and the time zeroed.

    If you must use a TextBox then you can use the Date.TryParse method to validate and convert the String. Either way, you then assign the Date to parameter.

  6. #6
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,134

    Re: [RESOLVED] Date insert into database sql server

    Your code is likely to get an error if the user enter an name like Ahmed's

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width