locked
Use SqlDataSource control to insert date into database RRS feed

  • Question

  • User-1506996564 posted

    Hello

    I added an SqlDataSource to my page, I configured it using the wizard

    I deleted the insert parameters

    I try to send insert parameters by VB code

    every thing is okay except sending and saving date field

    I use this code to send the date

    instituteSqlDataSource.InsertParameters.Add("establishingDate", Date.Parse(Calendar1.SelectedDate))

    field data type in the database is (date)

    at insert I get an erro

    it says , Incorrect format

    any ideas please?

    thank you

    Sunday, April 12, 2015 4:34 AM

All replies

  • User-1716253493 posted
    instituteSqlDataSource.InsertParameters("establishingDate").DefaultValue = Date.Parse(Calendar1.SelectedDate)

    Define establishingDate parameter in aspx code, set the value from code behind

    or Use BIND methosd directly in Calendar1 in insertitemtemplate

    SelectedDate = '<%# Bind("establishingDate") %>'

    Sunday, April 12, 2015 8:31 PM
  • User-1506996564 posted

    Thank you for response

    but still same error 

    I guess it is formating issue

    I use an Arabian calendar (Hijri)

    I used the watch window 

    Date.parse(Calendar1.selectedDate) give the selected date in this format #4/13/2015#

    but 

    If I assigned it to a textbox

    it assigns 24/6/1436 , this is the equivelant in my current calendar on the PC (Hijri)

    and it is the value that I want to store in the datebase

    what should I change?

    but I cant insert it to the database , 

    Monday, April 13, 2015 2:11 AM
  • User-1716253493 posted

    Dont delete insertparameter, AFAIK selecteddate is datetime

    instituteSqlDataSource.InsertParameters("establishingDate").DefaultValue = Calendar1.SelectedDate

    Don't worry about format if the parametertype is DateTime.

    If you want to get date value from spesific format, use ParseExact

    Dim dt as DateTime = DateTime.ParseExact("13/04/2015", "dd/MM/yyyy", Nothing)

    The date string must exacly same with the format.
     

    Monday, April 13, 2015 2:30 AM
  • User-1506996564 posted

    Hello

    yes , I added the insert parameter to the aspx page

    I used 

    instituteSqlDataSource.InsertParameters("establishingDate").DefaultValue = Calendar1.SelectedDate

    but still the same error

    System.FormatException was caught
    HResult=-2146233033
    Message=تنسيق سلسلة الإدخال غير صحيح.
    Source=mscorlib
    StackTrace:
    عند System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
    عند System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    عند System.Int32.Parse(String s)
    عند M3ahedsOne.NewInstitute.addInstituteBTN_Click(Object sender, EventArgs e) في C:\Users\e-ins\documents\visual studio 2013\Projects\M3ahedsOne\M3ahedsOne\EntryForms\NewInstitute.aspx.vb:السطر 69
    InnerException:

    Monday, April 13, 2015 2:51 AM