Click here to Skip to main content
15,908,626 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB



Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
Dim str As [String] = "Data Source=localhost;User ID=sa;Password=unf"
Dim con As New SqlConnection(str)
con.Open()
Dim query As [String] = "insert into Tutor values('" & txttutornnumber.Text & "','" & txtstudentnnumber.Text & "','" & Int32.Parse(txtcaldatetime.Text) & & "','" & Int32.Parse(txttimespent.Text) & "','" & txtnotes.Text & "')"
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.ExecuteNonQuery()
con.Close()
End Sub

Posted
Comments
[no name] 19-Mar-15 23:25pm    
From your code it's not obvious to see which type conversion you're asking about. The Int32.Parse(..) could work. What's the actual problem here?
Member 8155568 19-Mar-15 23:33pm    
Hi Thanks for the reply

i would like to insert txtcaldatetime(text box name ) into the database where the database column datatype is datetime

You can convert a String to DateTime with DateTime.Parse(..) (which will throw an Exception if the input string isn't in a correct format) or with DateTime.TryParse(..) (which will return true or false indicating the success of the parsing). Please refer to IntelliSense for the signature of the latter, or MSDN.

But I would suggest you use a DateTimePicker-Control instead of a TextBox-Control to let the user select a date/time, which would spare you having to convert it and make it more convenient for the user.

Also there are some "beginners flaws" in your code ;) , please have a look at my answer to a previous question which covers all of that:
How to insert datetime in sql server 2008 using VB 2010?[^]
 
Share this answer
 
v2
Okay...

C#
var x = DateTime.MinValue;
var b = DateTime.TryParse(txtcaldatetime.Text,out x);


x will now contain a valid datetime or DateTime.MinValue... Both of these you can write to your database.

C#
C#
string sql = "insert into Tutor values(@tutorNumber, @studentNumber, @calDateTime, @timeSpent, @notes)";
SqlConnection connection = new SqlConnection(/* connection info */);
SqlCommand command = new SqlCommand(sql, connection);

//Repeat this bit for each parameter you need to pass
command.Parameters.Add(new SqlParameter {
        ParameterName = "@Notes",
        SqlDbType = SqlDbType.NVarChar,
        Direction = ParameterDirection.Input,
        Value = txtNotes.Text,
});

command.ExecuteNonQuery();

VB.NET
VB
Dim sql As String = "insert into Tutor values(@tutorNumber, @studentNumber, @calDateTime, @timeSpent, @notes)"
	' connection info 
Dim connection As New SqlConnection()
Dim command As New SqlCommand(sql, connection)

'Repeat this bit for each parameter you need to pass
command.Parameters.Add(New SqlParameter() With { _
	Key .ParameterName = "@Notes", _
	Key .SqlDbType = SqlDbType.NVarChar, _
	Key .Direction = ParameterDirection.Input, _
	Key .Value = txtNotes.Text _
})

command.ExecuteNonQuery()
 
Share this answer
 
Convert.ToDateTime(txtcaldatetime.Text.Trim())
 
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