I had the following code, how do i actually pass the value from the select statement to the Contactlbl.Text Thanks
Protected Sub GoBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GoBtn.Click
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim Value As String = EventCmb.SelectedItem.ToString()
objConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\HP\My Documents\Visual Studio 2005\WebSites\FYP2\App_Data\Event.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
objCmd.Connection = objConn
objCmd.CommandType = CommandType.Text
objCmd.CommandText =
"SELECT EventTel FROM Event WHERE (EventID = Value)"objConn.Open()
ContactLbl.Text =
objConn.Close()
End SubEnd
Class
Help In Coding
mabster
if you change
Dim RetVal As Object = objCmd.ExecuteScalar() '
to
Dim RetVal As Object' = objCmd.ExecuteScalar() '
and instead do a
objCmd.ExecuteNonQuery()
do you still get the same exception
Nicolas Mueggler
abuck
Maybe i shd show you my Database Table, and data stored (Currrently). The "Dim RetVal As Object = objCmd.ExecuteScalar()" error had been debugged.
However, another error arise, the "Finally" just before "objconn.close()" had this error, "Conversion failed when converting the nvarchar value 'LTA' to data type int."
Any idea how to resolve Thks again!
Event
EventID int -> Primary Key 3
EventName nvarchar(50) LTA
EventTel nvarchar(50) 12345678
EventEmail nvarchar(50) LTA@lta.com
EventStartDate nvarchar(50) 6/30/2006
EventEndDate nvarchar(50) 6/30/2006
EventStartTime varchar(10) 01:00
EventEndTime varchar(50) 02:00
EventVenue nvarchar(50) LTA
EventDescription nvarchar(4000) LtA Testing
Wicket
Thanks MarcD! I learn something new!
However, there's an error for in line "Dim RetVal As Object = objCmd.ExecuteScalar() "
Error Message is as follow "Conversion failed when converting the nvarchar value 'LTA' to data type int."
It is due to "ExecuteScalar() " can only store int I just need to display one data, and Value data comes form a combo box "EventCmb", which i wanted to find the selected String, compared to the "Event" database to get the "EventTel" data How do i solved this problem, any advice Thanks!
pukla
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim Value As String = EventCmb.SelectedItem.ToString()
objConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\HP\My Documents\Visual Studio 2005\WebSites\FYP2\App_Data\Event.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
Try
objConn.Open()
objCmd.Connection = objConn
objCmd.CommandType = CommandType.Text
objCmd.CommandText = "SELECT EventTel FROM Event WHERE (EventID = @Value)" ' See how i changed Value to @Value. This is called a Named Parameter
objCmd.Parameters.AddWithValue("@Value", Value) ' Add the @value withthe actual value that should be put. This makes it securer
Dim RetVal As Object = objCmd.ExecuteScalar() ' This returns the First Column of the first row regardless of how much data is returned.
If Not ((RetVal Is Nothing) Or (RetVal Is DBNull.Value)) Then
ContactLbl.Text = RetVal.ToString()
Else
' noting was returned
End If
Catch ex As Exception
Throw ex
Finally
objConn.Close()
End Try
Hope this helps.
©2008 Software Development Network
powered by phorum