Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

Must implement Iconvertible

Hello experts,
I am having a hard time with this problem.
I am simply trying to update some fields from a Details View control to the "Main" table in a database.

Below is the code with the Sql connection and  Sql command.
The code fails to convert the parameter value from a TextBox to a DateTime
Or object must implement IConvertible

Any suggestions??

Thanks
Dim con As SqlConnection
        Dim com As SqlCommand
        con = New SqlConnection("Server=Adam\; Database=HelpDesk;Integrated Security=True")
        com = New SqlCommand("Update Main set[dDate]=@dDate,[tTime]=@ttime,[nName]=@nName,[sSubject]=@sSubject,[tType]=@tType,[mMonth]=@mMonth,[Remarks]=@Remarks where ID=@ID", con)
        com.Parameters.Add("@dDate", System.Data.SqlDbType.Date)
        com.Parameters("@dDate").Value = DateTime.Parse(txtNewDate.Text)
        com.Parameters.Add("@tTime", System.Data.SqlDbType.Time)
        com.Parameters("@tTime").Value = DateTime.Parse(txtNewTime.Text)
        com.Parameters.Add("@nName", System.Data.SqlDbType.NVarChar)
        com.Parameters("@nName").Value = txtNewName.Text
        com.Parameters.Add("@sSubject", System.Data.SqlDbType.NVarChar)
        com.Parameters("@sSubject").Value = txtNewSubject.Text
        com.Parameters.Add("@tType", System.Data.SqlDbType.NVarChar)
        com.Parameters("@tType").Value = txtNewType.Text
        com.Parameters.Add("@mMonth", System.Data.SqlDbType.NVarChar)
        com.Parameters("@mMonth").Value = txtNewMonth.Text
        com.Parameters.Add("@Remarks", System.Data.SqlDbType.VarChar)
        com.Parameters("@Remarks").Value = txtNewRemarks.Text
        com.Parameters.Add("@ID", System.Data.SqlDbType.Int)
        com.Parameters("@ID").Value = currentID
        con.Open()
        com.ExecuteNonQuery()
        con.Close()
        DataDetails.ChangeMode(DetailsViewMode.ReadOnly)
        getGridData()
        BindDetails()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adamtrask
adamtrask

ASKER

Thank you CodeCruiser:

I did as you suggested, but got this error:

Failed to convert parameter value from a DateTime to a TimeSpan
What is the type of dDate in stored procedure and what is in txtNewDate.text?
dDate is of type date in the database table
txtNewDate.Text is  a Text Box which  gets its text value from a  Details View control template.
It was created in the following way:


Dim txtNewDate As TextBox = DataDetails.FindControl("txtEditDate")
But what text is actually contained in the textbox? I suspect its nothing!
It should contains a date.

Ok, let me clarif:

I have a gridView which is actually displaying several records in the database. Ech record has dDate filed which contains a date

The gridView contains a Select link which, when clicked,  displays the Details View control. This control displays the data of a single record from the Grid View.

 To access the data field in the Details View I created Templates in the form of textBoxes.

 So txtNewDate should contain the date that's actually being displayed in the Details View control.



 
> So txtNewDate should contain the date that's actually being displayed in the Details View control.

It should. But have you checked that it does? By stepping through the code?
Let me try
Yes it does contain the date: I don't know how to step through the code so I did this instead:
I created a new label named "lblTesting" and placed it immediately after the line of code which creates
txtNewDate in the following way:


Dim txtNewDate As TextBox = DataDetails.FindControl("txtEditDate")
        Me.lbltesting.Text = txtNewDate.Text


I deleted all the code that comes after these two lines
Sure enough the date which I changed in the Details View controls was displayed in the new label.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IJZ:  Thanks for you comment, but I still get the following error:

"String was not recognized as a valid Datetime"

This is what I did:
Instead of making the  textbox the parameter's value, I first created a date variable and tried to place the date into the variable then use the variable as the parameter's value in the following way:


Dim stringDate As Date = Date.ParseExact(txtNewDate.Text, "MM/dd/yyyy", Globalization.CultureInfo.CurrentCulture)
com.Parameters.Add("@dDate", System.Data.SqlDbType.Date)
com.Parameters("@dDate").Value = stringDate:

if you are getting exception on
Dim stringDate As Date = Date.ParseExact(txtNewDate.Text, "MM/dd/yyyy", Globalization.CultureInfo.CurrentCulture)




or
com.Parameters("@dDate").Value = stringDate:
The top one

The exception is on this one:


Dim stringDate As Date = Date.ParseExact(txtNewDate.Text, "MM/dd/yyyy", Globalization.CultureInfo.CurrentCulture)
can you tell what value you can see in txtNewDate.Text in debugger?
as I explained to  CodeCruiser: I don't know how to step through the code using the debugger
so I did this instead:
I created a new label named "lblTesting" and placed it immediately after the line of code which creates
txtNewDate in the following way:

Dim txtNewDate As TextBox = DataDetails.FindControl("txtEditDate")
        Me.lbltesting.Text = txtNewDate.Text

the label displayed the following date:
4/20/2011 11:00:00 AM
Hi,

Please check all DAte Textbox have value other than blank. If you convert those values have blank to Date will give you exception.

Please first check for blank if yes then pass DBNull value else convert to DAte and send to parameter.
Thank you PatelAlpesh. I did check for blanks and confirmed that there are no blanks.

Yet I get the exception:

Invalid cast from 'System.DateTime' to 'System.TimeSpan'.
>Invalid cast from 'System.DateTime' to 'System.TimeSpan'.

I dont see any TimeSpan anywhere in your code yet the exception is about timespan. Can you show us the method signature of the stored procedure?

Codecruiser,

I am not using a stored procedure. I am updating the values from the web form to the table Main.
When get rid of the dDate and tTime parts the remaining values are updated without a problem.

The following contains the code for the whole DataDetails_ItemUpdating procedure:



   Protected Sub DataDetails_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DataDetails.ItemUpdating
        Dim currentID As Integer = DataDetails.DataKey.Value
        Dim txtNewDate As TextBox = DataDetails.FindControl("txtEditDate")
        Dim varDate As Date
        If Date.TryParse(txtNewDate.Text, varDate) Then
            Me.lbldate.Text = varDate.ToShortDateString()
        Else
            Me.lbldate.Text = "Sorry"
        End If
        Dim txtNewTime As TextBox = DataDetails.FindControl("txtEditTime")
        Dim varTime As DateTime
        If Date.TryParse(txtNewTime.Text, varTime) Then
            Me.lblTime.Text = varTime.ToShortTimeString
        Else
            Me.lblTime.Text = "Sorry"
        End If

        Dim txtNewName As TextBox = DataDetails.FindControl("txtEditName")
        Dim txtNewSubject As TextBox = DataDetails.FindControl("txtEditSubject")
        Dim txtNewType As TextBox = DataDetails.FindControl("txtEditType")
        Dim txtNewMonth As TextBox = DataDetails.FindControl("txtEditMonth")
        Dim txtNewRemarks As TextBox = DataDetails.FindControl("txtEditRemarks")
        '---------
        Dim con As SqlConnection
        Dim com As SqlCommand
        con = New SqlConnection("Server=Tariq\; Database=HelpDesk;Integrated Security=True")
        com = New SqlCommand("Update Main set[dDat]=@dDate,[tTime]=@tTime,[nName]=@nName,[sSubject]=@sSubject,[tType]=@tType,[mMonth]=@mMonth,[Remarks]=@Remarks where ID=@ID", con)
        com.Parameters.Add("@dDate", System.Data.SqlDbType.Date)
        com.Parameters("@dDate").Value = varDate
        com.Parameters.Add("@tTime", System.Data.SqlDbType.Time)
        com.Parameters("@tTime").Value = varTime
        com.Parameters.Add("@nName", System.Data.SqlDbType.NVarChar)
        com.Parameters("@nName").Value = txtNewName.Text
        com.Parameters.Add("@sSubject", System.Data.SqlDbType.NVarChar)
        com.Parameters("@sSubject").Value = txtNewSubject.Text
        com.Parameters.Add("@tType", System.Data.SqlDbType.NVarChar)
        com.Parameters("@tType").Value = txtNewType.Text
        com.Parameters.Add("@mMonth", System.Data.SqlDbType.NVarChar)
        com.Parameters("@mMonth").Value = txtNewMonth.Text
        com.Parameters.Add("@Remarks", System.Data.SqlDbType.VarChar)
        com.Parameters("@Remarks").Value = txtNewRemarks.Text
        com.Parameters.Add("@ID", System.Data.SqlDbType.Int)
        com.Parameters("@ID").Value = currentID
        con.Open()
        com.ExecuteNonQuery()
        con.Close()
        DataDetails.ChangeMode(DetailsViewMode.ReadOnly)
        getGridData()
        BindDetails()

The lblDate and lblTime labels were just for testing. I used them to see if the varDate and varTime variables contained values. They did.

Since the Details View control gets its data from the GridView (myGrid) control, below is the code that shows how the gridview gets its data:

Private Sub getGridData()
        Dim dt As Date
        dt = Me.calendar1.SelectedDate
        Me.txtDate.Text = dt.ToShortDateString
        Dim con As SqlConnection
        Dim com As SqlCommand
        Dim reader As SqlDataReader
        con = New SqlConnection("Server=Tariq\; Database=HelpDesk;Integrated Security=True")
        com = New SqlCommand("SELECT * FROM Main where dDate=@dDate", con)
        com.Parameters.AddWithValue("@dDate", System.Data.SqlDbType.Date)
        com.Parameters("@dDate").Value = Me.txtDate.Text

        con.Open()
        reader = com.ExecuteReader

        myGrid.DataSource = reader
        myGrid.DataKeyNames = New String() {"ID"}
        myGrid.DataBind()

        reader.Close()
        con.Close()

    End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks

I tried ParseExact and it seems to work ok with the following line of code:
 varDate = Date.ParseExact(txtNewDate.Text, "M/d/yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture).ToShortDateString

But it generates "formatException was unhandled by user code
String was not recognized as a valid Datetime" when used witht the next
line of code:

 varTime = Date.ParseExact(txtNewTime.Text, "M/d/yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture).ToShortTimeString

formatException was unhandled by user code
String was not recognized as a valid Datetime
Is the txtNewTime.Text in the format  "M/d/yyyy hh:mm:ss tt" as well?
yes it is....
Thank you guys for your help