2 Replies - 1779 Views - Last Post: 25 March 2012 - 05:22 AM Rate Topic: -----

#1 Lennie Kuah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-March 12

VBNET2008 SQL COMMAND PARAMETER

Posted 24 March 2012 - 09:42 PM

Hullo Good Friends,
I need your help. Please help me.
I am using VBNET2008 Window Application AND SQLSERVER 2000 table.
I was requested to develop an Employee Registration Application and do encounter a problem
With the TextBox Date when using SQL Command Parameter to insert into the tblEmployee table

This is the error message:
TextBox Date is a string

Listed below is the over all coding;

Private Sub FInsertEmployee()
     Dim strsql As String = nothing
         strsql = " Insert into tblEmployees "
         strsql &= "(EmployeeID, LastName, FirstName, Title, "
         strsql &= " HireDate "
         strsql &= " Values (@intEmpID, @LastName, @FirstName, @Title  " @HireDate )  "
 
     Try
         sqlconn = New SqlConnection(connstr)
         sqlconn.Open()
         sqlcmd = New SqlCommand(strsql, sqlconn)

         With sqlcmd.Parameters
             .AddWithValue("@intEmpID", SqlDbType.Int).Value = Convert.ToInt32(intEmployeeId)
             .AddWithValue("@LastName", SqlDbType.NVarChar).Value = Me.txtLastName.Text.ToString
             .AddWithValue("@FirstName", SqlDbType.NVarChar).Value = Me.txtFirstName.Text.ToString
             .AddWithValue("@Title", SqlDbType.NVarChar).Value = Me.txtTitle.Text.ToString 
             .AddWithValue("@HireDate", SqlDbType.DateTime).Value = DateTime.Parse(Me.txtHireDate.Text)             
          End With

          sqlcmd.ExecuteNonQuery()

    Catch ex As Exception
        MessageBox.Show(ex.Message)

    Finally
        sqlconn.Close()
        sqlcmd.Dispose()

     End Try
End Sub
[b]



Thank you very much for helping me. You are wonderful and helpful.
Cheers,
Lennie Kuah[/b]

Is This A Good Question/Topic? 0
  • +

Replies To: VBNET2008 SQL COMMAND PARAMETER

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: VBNET2008 SQL COMMAND PARAMETER

Posted 24 March 2012 - 09:53 PM

It would be better to use a dateTimePicker control than trust a textbox for a proper DateTime.

What is the exact error as TextBox Date is a string is most likely not the same.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,918
  • Joined: 25-September 09

Re: VBNET2008 SQL COMMAND PARAMETER

Posted 25 March 2012 - 05:22 AM

You either need to put
strsql &= " in front of the line that has @HireDate ) " and put a comma ater @Title

or replace the quote before the @HireDate with a comma.

strsql &= " Values (@intEmpID, @LastName, @FirstName, @Title " @HireDate ) "


EDIT:
I also just noticed you don't have a closing parenthesis after HireDate in the fields section of your insert statement.

This post has been edited by CharlieMay: 25 March 2012 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1