Here's my code
I seem to be getting an error SQL command cannot be converted to string
with this code
Dim LogData2 As sterm.markdata = New sterm.markdata()
Dim query As New SqlCommand("Select * from openquery (db, 'SELECT * FROM table WHERE person=@person')")
query.Parameters.AddWithValue("@person", Session("number"))
Dim drCode2a As DataSet = LogData2.StermQ3(query)
dgBookings.DataSource = drCode2a.Tables(0).DefaultView
dgBookings.DataBind()
I tried adding in a Convert.ToString
like this
Dim drCode2a As DataSet = LogData2.StermQ3(Convert.ToString(query))
And now I get this error
Could not find server 'System' in sysservers. Execute sp_addlinkedserver to add the server to sysservers
I'm obviously doing something wrong but not sure what - ASP.Net and SQL is all new to me so any help would be much appreciated.
Source code of StermQ3
StermQ3(String) As System.Data.DataSet
Public Function StermQ3(ByVal strSQL6 As String) As System.Data.DataSet
UPDATE
Source code as it is after some changes have been made
Sub Page_Load(ByVal Sender as Object, ByVal e as EventArgs)
Dim LogData2 As sterm.markdata = New sterm.markdata()
Dim query As New SqlCommand("Select * from openquery (db, 'SELECT * FROM table WHERE person=@person')")
query.Parameters.AddWithValue("@person", Session("number"))
query.CommandType = CommandType.StoredProcedure
query.CommandText = "openquery"
Dim drCode2a As DataSet = LogData2.StermQ3(Convert.ToString(query))
dgBookings.DataSource = drCode2a.Tables(0).DefaultView
dgBookings.DataBind()
End Sub
HTML is
<asp:DataGrid id="dgBookings" runat="server" AutoGenerateColumns="true" ShowHeader="true">
</asp:DataGrid>
Old way I used to do my query
Dim query As String = "Select * from openquery (db, 'SELECT * FROM table WHERE investor=''" + Session("number") + "'' ')"
That works if i replace my SQL Command but it is open to SQL Injection
UPDATE
I now have it so it work without the parameters bit here's my updated souce code any idea why it won't add the parameter value?
Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
conn.Open()
Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = @investor ') ", conn)
query.Parameters.AddWithValue("@investor", 69836)
dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()
It works like this
Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
conn.Open()
Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = 69836') ", conn)
dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()
Any ideas?
SOLUTION
Here's how i solved my issue
Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")
conn.Open()
Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn)
dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()
Thanks for all the help