我怎么能这样做...... [英] How I can do this ...

查看:76
本文介绍了我怎么能这样做......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串未被识别为有效的DateTime。



i当我输入id时,想在文本框中从数据库中获取数据....



< b>我尝试了什么:



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:

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()

推荐答案

TextHiring.Text中的内容不能被解释为约会。在将DateTime.TryParse分配给参数之前,请使用DateTime.TryParse将其转换为正确的DateTime变量。 Google DateTime.TryParse用于使用示例,也是google错误消息。
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.


您为SqlParameters提供的值必须是您为其指定的类型。如果你有一个带有SqlDbType.DateTime的SqlParameter,那么你需要提供一个DateTime类型的值,但你提供了一个字符串。



两个选项:要么改变你的输入 - 控制日期到DateTimePicker,这样你从一开始就有一个DateTime类型的值。或者使用DateTime.TryParse(..)或DateTime.Parse(..)将TextBox中的文本解析为DateTime对象。



你有相同的排序问题与@ID和@w的参数有关。
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.


这篇关于我怎么能这样做......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆