1

I am a beginner in vb.net and asp.net. I am having a problem saving the date, which is a string value coming from the DevExpress datepicker control.

The column name is expiryDate and its type is date.

Here is my ASPX markup :

<tr>
    <td nowrap="nowrap" class="label" align="right" valign="top" style="width: 10%">
        Expiry Date
    </td>
    <td style="width: 40%" valign="top">
        <dxe:ASPxDateEdit ID="expiryDate" 
                          runat="server" 
                          EditFormat="Date" 
                          Date="13/06/2014" 
                          Width="200">
        </dxe:ASPxDateEdit>
    </td>
</tr>

And in C# code behind :

sqlSave = "INSERT INTO [Product] (Title, expiryDate) VALUES ("
          sqlSave += " '" & txtProductName.Text.Trim().Replace("'", "''") & "', "
          sqlSave += ""   & expiryDate.Text & " )"

I am using expiryDate.Text to save in the database but the date saved is: 1900-01-01 and sometime i get message Operand type clash: int is incompatible with date.

I don't know how to fix this issue, please help?

2
  • 3
    Please use SQL parameters. stackoverflow.com/questions/910465/… Commented Jun 13, 2014 at 12:26
  • 1
    @onskee yeah you are right.. this will avoid sql injections. Commented Jun 13, 2014 at 13:41

2 Answers 2

3

Try like this,

sqlSave = "INSERT INTO [Product] (Title, expiryDate) VALUES ("
                sqlSave += " '" & txtProductName.Text.Trim().Replace("'", "''") & "', "
                sqlSave += "'" & Convert.ToDateTime(expiryDate.Text).ToString("yyyy-MM-dd") & "' )"

But, I would like to suggest you to use parameterized query instead of simple concatenated string.

cmd.CommandText = "INSERT INTO [Product] (Title, expiryDate) VALUES (@Title,@expiryDate)";

cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = txtProductName.Text.Trim().Replace("'", "''");
cmd.Parameters.Add("@expiryDate", SqlDbType.DateTime).Value = Convert.ToDateTime(expiryDate.Text);
Sign up to request clarification or add additional context in comments.

3 Comments

Well that column's type is "date" in DB, not "datetime"
Why convert a string to a date and then back to a string only to insert it into a Date column in the DB? Why not just insert the date directly using a Date parameter?
have u read all comments in this question @ChrisDunaway. I have already suggested and upvoted onskee's comment.
0

Replace

" & expiryDate.Text & "

with

'" & expiryDate.Text & "'

4 Comments

what happen when date is formatted like 01/06/2014?
It is the rule to save data of type datetime in sql database. should be enclosed with two quotes e.g. 'MM-dd-yyyy'
No I am not talking about rule.. if the date format is "dd/MM/yyyy" or "MM/dd/yyyy" then it creates problem. by default sql format is "yyyy-MM-dd". So, either we have to pass date in "yyyy-MM-dd" format or we have to change sql date format using SET DATEFORMAT 'DMY'.
No, better to not use string concatenation. Instead, use parameters and specify the date directly as a datetime variable. That way you don't have to worry about string conversions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.