Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

VB
Dim dat As String
        Dim dat1 As DateTime
        Dim dat2 As DateTime
        Try
            dat = "select joindate from employee where emp_id='" & TextBox1.Text & "'"
            dat1 = DateTime.Parse(dat)


i wrote the above code the unable to convert the query value to datetime..

please help me out...
Posted
Comments
Mazumder.Soumen 25-Sep-12 7:29am    
The Query seems to be an SQL Query. How will it get executed with out using any SQL Object first. Can you please eloborate your question a bit

I am not in the least surprised.
Look at what you are doing: It boils down to:
VB
dat1 = DateTime.Parse("select joindate from employee where emp_id=1")

Which (predictably enough) is not a date, or even vaguely like a date!

You need to read the data from the database itself:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("SELECT joindate FROM employee WHERE empi_id=@ID", con) 
                com.Parameters.AddWithValue("@ID", TextBox1.Text)
		Using reader As SqlDataReader = com.ExecuteReader()
			While reader.Read()
				dat1 = DirectCast(reader("joindate"), DateTime)
			End While
		End Using
	End Using
End Using
Please note that this uses a Parametrized query - you should use these at all times, as your original code (had it accessed the DB) would have left you wide open to an SQL Injection Attack, which could damage or destroy your database.
 
Share this answer
 
"select joindate from employee where emp_id='" & TextBox1.Text & "'"
is string

dat is DateTime variable

and you are assigning String in DateTime so, it will give error

"select joindate from employee where emp_id='" & TextBox1.Text & "'"
is a Sql-query
execute it using SQLConnection and SqlCommand [you have not written Connectivity & execution code]
and then assign result in to dat variable
Happy Coding!
:)
 
Share this answer
 
Comments
[no name] 25-Sep-12 7:46am    
please tell me how to assign the output to a variable..???
Aarti Meswania 25-Sep-12 7:57am    
see solution-2
or search on google for topic
"how to execute sql Queries from C#.Net"
copy paste code and change your connection-string & QueryString
Hi Nandy,

Try to handle not null validation in your code.
I hope the above code will do the thing as expected.

Thanks and regards
 
Share this answer
 
Comments
[no name] 25-Sep-12 7:47am    
@Prabakar: can u please tell me how...??
try to use CDate(textbox1.text)

convert string to date

but u need to get the date of every row..
 
Share this answer
 
v3

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