error while exporting data of Outlook to sqlserver

P

peeyush

I am desgning an application in VB.Net and Sqlserver for exporting
data of Outlook to sqlserver. I am facing an error on
command.executenonquery() line.
the error being encountered is:
An unhandled exception of type 'System.Data.SqlClient.SqlException'
occurred in system.data.dll
Additional information: System error.

and the code is as follows:


Dim oApp As Outlook.Application = New Outlook.Application()

' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim cContacts As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

' Get the first contact from the Contacts folder.
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.ContactItem

Dim iCount As Int16
iCount = 0

oCt = oItems.GetFirst()
Dim conn As SqlConnection = New SqlConnection( _
"server=rajat; database=outlookcontacts; user id=sa;
password=hrhk;")
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO contactdetails (emailid, first,
middle, last) VALUES (@emailid, @first, @middle, @last)"
cmd.Parameters.Add("@emailid", SqlDbType.NVarChar, 100,
oCt.Email1Address)
cmd.Parameters.Add("@first", oCt.FirstName)
cmd.Parameters.Add("@middle", oCt.MiddleName)
cmd.Parameters.Add("@last", oCt.LastName)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

Please help me out ASAP
 
B

Brendan Reynolds

'First' and 'last' are both SQL-92 reserved keywords, so their use as field
names may be a problem. Consider changing the field names if you can. If
not, try putting square brackets around them ...

cmd.CommandText = "INSERT INTO contactdetails (emailid, [first], middle,
[last]) VALUES(@emailid, @first, @middle, @last)"

See 'reserved keywords' in SQL Server Books Online.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top