Click here to Skip to main content
16,012,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Vb.net 2010. I written code to SAVE the records.I used Date Picker control to record dates.


VB
Dim str As String = ""

If (Saveflag = "Save") Then
    str = "Insert Into Employee_Detail values(" & txtSrn.Text & ",'" & txtTitle.Text & "','" & txtFullName.Text & "','" & dtDOJ.Value & "'," & cmbEclass.SelectedValue & "," & cmbDesignation.SelectedValue & "," & cmbAppStatus.SelectedValue & "," & cmbFaculty.SelectedValue & "," & cmbCampus.SelectedValue & "," & cmbInstitute.SelectedValue & ",'" & dtDOB.Value & "'," & txtYears.Text & ",'" & dtDORHR.Value & "','" & dtDOREllu.Value & "'," & cmbPositionClass.SelectedValue & "," & cmbDept.SelectedValue & "," & cmbSalaryGrade.SelectedValue & "," & cmbScale.SelectedValue & "," & txtAssnSalary.Text & ",'" & dtConfirmationDate.Value & "','" & txtNorms.Text & "'," & txtSymbiExp.Text & "," & txtIndusExp.Text & "," & txtTeachingExp.Text & "," & cmbWorkStatus.SelectedValue & ",'" & txtPan.Text & "')" & ""
    cnt = cnt + 1
    CurRec = cnt

If (obj.SetQuery(str) = True) Then
    MessageBox.Show("One Record" & Saveflag & " Successfully", "Employee_Detail", MessageBoxButtons.OK, MessageBoxIcon.Information)
    LockButton(True)
    LockText(True)


VB
Dim cmd As New SqlCommand()
        Dim con As New SqlConnection
        Dim Mode As Boolean
        con = SetCon()
        cmd.CommandText = strQuery
        cmd.Connection = con
        If cmd.ExecuteNonQuery > 0 Then
            Mode = True
        Else
            Mode = False
        End If
        con.Close()
        Return Mode


System throws an error: "Conversion failed when converting date and/or time from character string." for "cmd.CommandText = strQuery" this statement
Posted
Updated 21-Dec-13 18:56pm
v2

1 solution

Well, yes, it probably will.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
They make your code more readable, are more reliable, and they prevent me from destroying your database by typing into your application...

In addition, list the fields you are trying to insert into - again, it makes your code more reliable, and it's a lot safer.
VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
        com.Parameters.AddWithValue("@C1", myValueForColumn1)
        com.Parameters.AddWithValue("@C2", myValueForColumn2)
        com.ExecuteNonQuery()
    End Using
End Using

Then, convert your dates to DateTime values using DateTime.TryParse and report any problems to the user before you even try inserting a record - that way you can give them a more accurate error which helps them get it right. You then pass the DateTime values directly to SQL so there is no more conversion to do and it all works.

It improves your code, gets rid of your problem and a number of future problems as well...


"Still I am not clear, what are you trying to say. Can you show me example in my code?"

I'm not going to sit here and convert all that hard-to-read code to proper code: I have no use for it, so I'm not going to spend my time on it.
But...The code above, if written in your code would be:
VB
str = "Insert Into Employee_Detail values("& myValueForColumn1 & ",'" & myValueForColumn2 & "')"

So you should be able to work it out from that.
 
Share this answer
 
v2
Comments
Yogi ,Pune 21-Dec-13 4:23am    
Still I am not clear, what are you trying to say. Can you show me example in my code?
OriginalGriff 21-Dec-13 5:36am    
Answer updated.
Christian Graus 22-Dec-13 2:59am    
Your code is a disaster. It's broken in every possible way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900