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
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()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is the type of dDate in stored procedure and what is in txtNewDate.text?
ASKER
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("t xtEditDate ")
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("t
But what text is actually contained in the textbox? I suspect its nothing!
ASKER
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.
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?
It should. But have you checked that it does? By stepping through the code?
ASKER
Let me try
ASKER
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("t xtEditDate ")
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.
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("t
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. CurrentCul ture)
com.Parameters.Add("@dDate ", System.Data.SqlDbType.Date )
com.Parameters("@dDate").V alue = stringDate:
"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
com.Parameters.Add("@dDate
com.Parameters("@dDate").V
if you are getting exception on
Dim stringDate As Date = Date.ParseExact(txtNewDate .Text, "MM/dd/yyyy", Globalization.CultureInfo. CurrentCul ture)
or
com.Parameters("@dDate").V alue = stringDate:
Dim stringDate As Date = Date.ParseExact(txtNewDate
or
com.Parameters("@dDate").V
ASKER
The top one
The exception is on this one:
Dim stringDate As Date = Date.ParseExact(txtNewDate .Text, "MM/dd/yyyy", Globalization.CultureInfo. CurrentCul ture)
The exception is on this one:
Dim stringDate As Date = Date.ParseExact(txtNewDate
can you tell what value you can see in txtNewDate.Text in debugger?
ASKER
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("t xtEditDate ")
Me.lbltesting.Text = txtNewDate.Text
the label displayed the following date:
4/20/2011 11:00:00 AM
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("t
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.
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.
ASKER
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'.
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?
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?
ASKER
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(B
Dim currentID As Integer = DataDetails.DataKey.Value
Dim txtNewDate As TextBox = DataDetails.FindControl("t
Dim varDate As Date
If Date.TryParse(txtNewDate.T
Me.lbldate.Text = varDate.ToShortDateString(
Else
Me.lbldate.Text = "Sorry"
End If
Dim txtNewTime As TextBox = DataDetails.FindControl("t
Dim varTime As DateTime
If Date.TryParse(txtNewTime.T
Me.lblTime.Text = varTime.ToShortTimeString
Else
Me.lblTime.Text = "Sorry"
End If
Dim txtNewName As TextBox = DataDetails.FindControl("t
Dim txtNewSubject As TextBox = DataDetails.FindControl("t
Dim txtNewType As TextBox = DataDetails.FindControl("t
Dim txtNewMonth As TextBox = DataDetails.FindControl("t
Dim txtNewRemarks As TextBox = DataDetails.FindControl("t
'---------
Dim con As SqlConnection
Dim com As SqlCommand
con = New SqlConnection("Server=Tari
com = New SqlCommand("Update Main set[dDat]=@dDate,[tTime]=@
com.Parameters.Add("@dDate
com.Parameters("@dDate").V
com.Parameters.Add("@tTime
com.Parameters("@tTime").V
com.Parameters.Add("@nName
com.Parameters("@nName").V
com.Parameters.Add("@sSubj
com.Parameters("@sSubject"
com.Parameters.Add("@tType
com.Parameters("@tType").V
com.Parameters.Add("@mMont
com.Parameters("@mMonth").
com.Parameters.Add("@Remar
com.Parameters("@Remarks")
com.Parameters.Add("@ID", System.Data.SqlDbType.Int)
com.Parameters("@ID").Valu
con.Open()
com.ExecuteNonQuery()
con.Close()
DataDetails.ChangeMode(Det
getGridData()
BindDetails()
ASKER
The lblDate and lblTime labels were just for testing. I used them to see if the varDate and varTime variables contained values. They did.
ASKER
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=Tari q\; Database=HelpDesk;Integrat ed Security=True")
com = New SqlCommand("SELECT * FROM Main where dDate=@dDate", con)
com.Parameters.AddWithValu e("@dDate" , System.Data.SqlDbType.Date )
com.Parameters("@dDate").V alue = Me.txtDate.Text
con.Open()
reader = com.ExecuteReader
myGrid.DataSource = reader
myGrid.DataKeyNames = New String() {"ID"}
myGrid.DataBind()
reader.Close()
con.Close()
End Sub
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=Tari
com = New SqlCommand("SELECT * FROM Main where dDate=@dDate", con)
com.Parameters.AddWithValu
com.Parameters("@dDate").V
con.Open()
reader = com.ExecuteReader
myGrid.DataSource = reader
myGrid.DataKeyNames = New String() {"ID"}
myGrid.DataBind()
reader.Close()
con.Close()
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. InvariantC ulture).To ShortDateS tring
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. InvariantC ulture).To ShortTimeS tring
formatException was unhandled by user code
String was not recognized as a valid Datetime
I tried ParseExact and it seems to work ok with the following line of code:
varDate = Date.ParseExact(txtNewDate
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
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?
ASKER
yes it is....
ASKER
Thank you guys for your help
ASKER
I did as you suggested, but got this error:
Failed to convert parameter value from a DateTime to a TimeSpan