Click here to Skip to main content
15,889,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying with the below code to retrieve data from sql server database and display it in excel sheet. But its not working fine.

Getting exception as "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another" in the expression RecordSet.open(SQL,con).

Below is the code which I used,
VB
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim con As New SqlConnection(WebConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString)
con.Open()
Dim SQL = "select username,password,id from tbl_Login_Details "
Dim Recordset = Server.CreateObject("ADODB.Recordset")

Recordset.Open(SQL,con)

'Create a new workbook in Excel
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
'Transfer the data to Excel
oSheet.Range("A1").CopyFromRecordset(Recordset)
'Save the Workbook and Quit Excel
oBook.SaveAs("C:\Book1.xls")
oExcel.Quit()
'Close the connection
Recordset.Close()
con.Close()
End Sub
Posted
Updated 14-Mar-11 23:17pm
v3
Comments
Sandeep Mewara 15-Mar-11 5:13am    
Next time onward use PRE tags to format code part. It makes your question readable.

1 solution

Have a sample on how to create a connection and recordset in ADO here:
http://www.webcheatsheet.com/ASP/database_connection_to_MSSQL.php[^]
http://www.w3schools.com/ado/ado_recordset.asp[^]

BTW, based on similar issues faced and resoled suggests the object type to be strongly declared, look here:
Similar discussion[^]
 
Share this answer
 

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