Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
String was not recognized as a valid DateTime.

i want to get data from database in textbox when i enter the id ....

What I have tried:

VB
Dim con As New SqlConnection
        Dim cmd As New SqlCommand("reg2")
        Dim Adapter As New SqlDataAdapter
        cmd.CommandType = CommandType.StoredProcedure
        Dim ds As New DataSet
        Dim dt As New DataTable
        'Dim dr As SqlDataReader
        con.ConnectionString = "Server=.;Database=log;User Id=sa; Password=123;"
        cmd.Connection = con
        Adapter.SelectCommand = cmd
        ' dr = cmd.ExecuteReader
        cmd.Parameters.Add("@ID", SqlDbType.Int)
        cmd.Parameters("@ID").Value = TextID.Text
        cmd.Parameters.Add("@Name", SqlDbType.NVarChar)
        cmd.Parameters("@Name").Value = TextName.Text
        cmd.Parameters.Add("@sites", SqlDbType.VarChar)
        cmd.Parameters("@sites").Value = DropSites.Text
        cmd.Parameters.Add("@department", SqlDbType.NVarChar)
        cmd.Parameters("@department").Value = DropDep.Text
        cmd.Parameters.Add("@Nationalty", SqlDbType.NVarChar)
        cmd.Parameters("@Nationalty").Value = DropNat.Text
        cmd.Parameters.Add("@w", SqlDbType.VarChar)
        cmd.Parameters("@w").Value = 3
        cmd.Parameters.Add("@Religions", SqlDbType.NVarChar)
        cmd.Parameters("@Religions").Value = DropRel.Text
        cmd.Parameters.Add("@Birth_Date", SqlDbType.NVarChar)
        cmd.Parameters("@Birth_Date").Value = TextBitrh.Text
        cmd.Parameters.Add("@hiring_date", SqlDbType.DateTime)
        cmd.Parameters("@hiring_date").Value = TextHiring.Text.ToString
        Session("LoadData") = ds.Tables("tabel1")
        con.Open()
        Adapter.Fill(ds, "tabel1")
        con.Close()
        loadgrid1()
Posted
Updated 31-Mar-16 0:00am
v2
Comments
Richard Deeming 1-Apr-16 14:18pm    
Your application should not be connecting to the database as sa - that's a super-user which could be used to destroy your network. Instead, you should be connecting as a user which only has the permissions required by your application.

And if that's your real sa password that you've just posted to a public forum, you should change it immediately. And this time, chose something secure, not something that you saw in a "top 10 worst passwords ever" article!

Whatever is in "TextHiring.Text" can't be interpreted as a date. Use DateTime.TryParse to convert it into a proper DateTime variable before you assign it to your parameter. Google DateTime.TryParse for usage examples and google the error message too.
 
Share this answer
 
The value you provide for SqlParameters has to be of the type that you specified for it. If you have a SqlParameter with SqlDbType.DateTime then you need to provide a value of type DateTime, but you provided a string.

Two options: Either change your input-control for the dates to DateTimePicker, that way you have a value of type DateTime from the beginning. Or parse the text from the TextBox into a DateTime-object using DateTime.TryParse(..) or DateTime.Parse(..).

You have the same sort of issue with your parameters for @ID and @w.
 
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