我无法理解这个错误 [英] I can't understand this error
问题描述
Input string was not in a correct format.
Line 44: Session("LoadData") = ds.Tables("tabel1")
Line 45: con.Open()
Line 46: Adapter.Fill(ds, "tabel1")
Line 47: con.Close()
Line 48: dt = ds.Tables(0)
我的尝试:
What I have tried:
Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
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 myDate As Date = Date.ParseExact(ToString, "dd/MM/yyyy", Nothing)
'Dim myDate As DateTime = DateTime.ParseExact(CType(e.FindControl("TextHiring"), TextBox).Text(), "MM/dd/yy", New DateTimeFormatInfo)
con.ConnectionString = "Server=.;Database=log;User Id=sa; Password=123;"
cmd.Connection = con
Adapter.SelectCommand = cmd
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.Int)
cmd.Parameters("@sites").Value = DropSites.Text
cmd.Parameters.Add("@department", SqlDbType.Int)
cmd.Parameters("@department").Value = DropDep.Text
cmd.Parameters.Add("@Nationalty", SqlDbType.Int)
cmd.Parameters("@Nationalty").Value = DropNat.Text
cmd.Parameters.Add("@w", SqlDbType.Int)
cmd.Parameters("@w").Value = 1
cmd.Parameters.Add("@Religions", SqlDbType.Int)
cmd.Parameters("@Religions").Value = DDLRel.Text
cmd.Parameters.Add("@Birth_Date", SqlDbType.DateTime)
cmd.Parameters("@Birth_Date").Value = Convert.ToDateTime(TextBitrh.Text)
cmd.Parameters.Add("@hiring_date", SqlDbType.DateTime)
cmd.Parameters("@hiring_date").Value = Convert.ToDateTime(TextHiring.Text)
Session("LoadData") = ds.Tables("tabel1")
con.Open()
Adapter.Fill(ds, "tabel1")
con.Close()
dt = ds.Tables(0)
If dt.Rows.Count > 0 Then
TextName.Text = dt.Rows(0)("Name")
DropSites.SelectedValue = dt.Rows(0)("sites")
DropDep.SelectedValue = dt.Rows(0)("department")
DropNat.SelectedValue = dt.Rows(0)("Nationalty")
DDLRel.SelectedValue = dt.Rows(0)("Religions")
TextBitrh.Text = dt.Rows(0)("Birth_Date")
TextHiring.Text = dt.Rows(0)("hiring_date")
End If
' loadgrid1()
loadgrid2()
autoid()
'grid1()
End Sub
推荐答案
首先在调试器中查看参数:
Start by looking at your parameters in the debugger:
cmd.Parameters.Add("@ID", SqlDbType.Int)
cmd.Parameters("@ID").Value = TextID.Text
如果文本框没有;包含一个数字,你会得到错误。
我强烈建议两件事:
1)使用int.TryParse检查方法顶部的用户输入(和类似的)以确保它们都是有效的。如果不是,请报告问题,不要继续。 int.TryParse将字符串转换为整数,因此您可以将整数直接传递给SQL。
2)使用Parameters.AddWithValue而不是Add:
If the textbox doesn;t contain a number, you will get the error.
I'd strongly suggest two things:
1) Check your user inputs at the top of the method, using int.TryParse (and similar) to make sure they are all valid. If they aren't, report a problem and don't continue. int.TryParse converts the string to an integer, so you can then pass the integer directly to SQL.
2) Use Parameters.AddWithValue instead of Add:
cmd.Parameters.AddWithValue("@ID", idValueAsAnInteger)
发生了什么事情(更少打字)
It's a lot clearer what is going on (and less typing)
这篇关于我无法理解这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!