Member_2_5230414
asked on
Using cmd.ExecuteScalar() and showing 3 Selects
If i use
cmd.CommandText = "Select TOP 1 LastPoster,title,lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
how would i displayit so it looked like thisLastPoster& "Posted"& title &"On the"& lastime
Either you can use OutParameters or you can use DataReader.
ASKER
how would i use them though?
For details on using OutParameters see http://msdn.microsoft.com/en-us/library/59x02y99(v=VS.100).aspx
ASKER
my full code is:
Public Class Class1
Public Structure Results
Dim Result1 As Integer
Dim Result2 As Integer
Dim Result3 As String
End Structure
Shared Function noposts(ByVal forumname As String) As Results
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
Dim cmd As New OleDbCommand
cmd.Connection = con
cmd.Parameters.AddWithValue("@forumname", forumname)
Try
con.Open()
cmd.CommandText = "Select Count(*) From forum where Forum =@forumname"
Dim Res As Results
Res.Result1 = cmd.ExecuteScalar()
cmd.CommandText = "Select Count(*) From forum where Forum =@forumname and LastPoster <> '' "
Res.Result2 = cmd.ExecuteScalar()
cmd.CommandText = "Select TOP 1 LastPoster,title,lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
Return (Res)
Catch ex As Exception
Throw ex
Finally
con.Close()
End Try
End Function
End Class
Hi,
ExecuteScalar will only return you first row first column (single cell), so eith get DataSet or DataReader to get all columns,
alternatively use following
cmd.CommandText = "Select TOP 1 LastPoster & "" Posted "" & title & "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
thanks
ExecuteScalar will only return you first row first column (single cell), so eith get DataSet or DataReader to get all columns,
alternatively use following
cmd.CommandText = "Select TOP 1 LastPoster & "" Posted "" & title & "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
thanks
You can't use the executeScalar for that
Dim myCMD As SqlCommand = New SqlCommand(""Select TOP 1 LastPoster,title,lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC", nwindConn)
nwindConn.Open()
Dim myReader As SqlDataReader = myCMD.ExecuteReader()
Dim fNextResult As Boolean = True
Do While myReader.Read()
Console.WriteLine(vbTab & myReader.GetName(0) & " posted " & myReader.GetName(1) & " on the " & myReader.GetName(2));
Loop
myReader.Close()
nwindConn.Close()
cmd.CommandText = "Select TOP 1 LastPoster + " Posted " + title + " On the ",lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
Res.Result3 = cmd.ExecuteScalar()
ASKER
I tried
But i get the error
Conversion from string "tedtest 2 forum03/12/2011 07:50:" to type 'Double' is not valid.
cmd.CommandText = "Select TOP 1 LastPoster & title & lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = Res.Result2 = cmd.ExecuteScalar()
But i get the error
Conversion from string "tedtest 2 forum03/12/2011 07:50:" to type 'Double' is not valid.
cmd.CommandText = "Select TOP 1 LastPoster + ' Posted ' + title + ' On the ',lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
Res.Result3 = cmd.ExecuteScalar()
Make sure that Result3 is of type string then
cmd.CommandText = "Select TOP 1 LastPoster + '|' + title + '|' + lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
Res.Result3.Split("|")(0) & " Posted " & Res.Result3.Split("|")(1) & " On the " & Res.Result3.Split("|")(2)
cmd.CommandText = "Select TOP 1 LastPoster + '|' + title + '|' + lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
Res.Result3.Split("|")(0) & " Posted " & Res.Result3.Split("|")(1) & " On the " & Res.Result3.Split("|")(2)
Please use following
cmd.CommandText = "Select TOP 1 LastPoster & "" Posted "" & title & "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
or
cmd.CommandText = "Select TOP 1 LastPoster + "" Posted "" + title + "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
cmd.CommandText = "Select TOP 1 LastPoster & "" Posted "" & title & "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
or
cmd.CommandText = "Select TOP 1 LastPoster + "" Posted "" + title + "" On the "" & lastime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
ASKER
with
cmd.CommandText = "Select TOP 1 LastPoster + '|' + title + '|' + lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
i get Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
Data type mismatch in criteria expression.
Result 3 is string
cmd.CommandText = "Select TOP 1 LastPoster + '|' + title + '|' + lasttime from forum where Forum =@forumname AND lasttime <> NULL order by lasttime DESC"
Res.Result3 = cmd.ExecuteScalar()
i get Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
Data type mismatch in criteria expression.
Result 3 is string
ASKER
Is it because lasttime is within a date/time field??
>Is it because lasttime is within a date/time field??
Yes. Try this
'|' + Format(lasttime, 'dd/mm/yyyy')
Yes. Try this
'|' + Format(lasttime, 'dd/mm/yyyy')
ASKER
And when i try to split them i get
Object reference not set to an instance of an object.
Im using: Lastpost.Text = Res.Result3.Split("|")(0) & " Posted " & Res.Result3.Split("|")(1) & " On the " & Res.Result3.Split("|")(2)
Object reference not set to an instance of an object.
Im using: Lastpost.Text = Res.Result3.Split("|")(0) & " Posted " & Res.Result3.Split("|")(1) & " On the " & Res.Result3.Split("|")(2)
Is any of the fields empty in database?
Hi,
Please varify what you get in Res.Result3 if it is null or empty?
Please varify what you get in Res.Result3 if it is null or empty?
ASKER
Yes but thats why i use lasttime <> NULL
Step through the code to see what is coming back in Result3
ASKER
And yes IJZ it gets the result Res.Result3 "test 2 forum|ted|03/12/2011 07:50:13" String
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
then try following and check what you get in s in debugger
Dim s As String() = Res.Result3.Split("|")
Lastpost.Text = s(0) & " Posted " & s(1) & " On the " & s(2)
Dim s As String() = Res.Result3.Split("|")
Lastpost.Text = s(0) & " Posted " & s(1) & " On the " & s(2)
ASKER
top stuff once again