数据库中表有四个字段userid(char,2)code(char,4)bigp(decimal,6,2)smlp(decimal,6,2)
有一存储过程对它进行更新或追加:
CREATE PROCEDURE [a06091731229].[addfixedorder]
@userid char(2),@code char(4),@bigp decimal,@smlp decimal
 AS
if exists(select * from fixedorder where userid=@userid and code=@code)
   update fixedorder set userid=@userid,code=@code,bigp=@bigp,smlp=@smlp where userid=@userid and code=@code
else
   insert into fixedorder(userid,code,bigp,smlp) values(@userid,@code,@bigp,@smlp)
GO
在ASP.net(vb)中有一页面供用户输入资料,有多行文本框,调用存储过程更新或追加:
Private Sub input()
        Dim uid As String
        If Session.Item("userid") <> "" Then
            uid = Session.Item("userid")
        Else
            Exit Sub
        End If
        If tb11.Text <> "" And Val(tb12.Text) >= 0 And Val(tb13.Text) >= 0 Then
            Call mydatabase.addfixedorder(uid, tb11.Text.ToString, tb12.Text, tb13.Text)
            'Call mydatabase.addfixedorder(uid, tb11.Text.ToString, 888, 66)
        End If
        If tb21.Text <> "" And Val(tb22.Text) >= 0 And Val(tb23.Text) >= 0 Then
            Call mydatabase.addfixedorder(uid, tb21.Text, tb22.Text, tb23.Text)
        End If
        .....
现在的问题是,这样调用不能更新,经调试,是数据类型不符导致,将文本框内输入的数字,直接传递给存储过程不行,调用的时候直接给数字就可以,请问大家,这个问题怎么办?
奇怪的是好像刚写的时候可以的,过了段时间就不行了,这个可以先不管,可能是我的错觉

解决方案 »

  1.   

    Private Sub input()
            Dim uid As String
            Dim a As Decimal, b As Decimal
            If Session.Item("userid") <> "" Then
                uid = Session.Item("userid")
            Else
                Exit Sub
            End If
            If tb11.Text <> "" And Val(tb12.Text) >= 0 And Val(tb13.Text) >= 0 Then
                a = Convert.ToDecimal(tb12.Text)
                b = Convert.ToDecimal(tb13.Text)
                Call mydatabase.addfixedorder(uid, tb11.Text.ToString, a, b)
                'Call mydatabase.addfixedorder(uid, tb11.Text.ToString, 888, 66)
            End If
    还是不行,怎么回事?
      

  2.   

    Call mydatabase.addfixedorder(uid, tb11.Text.ToString, a, b)
                'Call mydatabase.addfixedorder(uid, tb11.Text.ToString, 888, 66)
    换下面的注释的那条就行,请大家帮帮我~
      

  3.   

    用下面的代码看看:        Dim uid As String
            Dim a As Decimal, b As Decimal
            If Session.Item("userid") <> "" Then
                uid = Session.Item("userid")
            Else
                Exit Sub
            End If
            a = val(tb12.Text)
            b = val(tb13.Text)
            Call mydatabase.addfixedorder(uid.trim(), tb11.Text.trim(), a, b)
      

  4.   

    还是不行,数字没变,也没有报错!
    忘了贴这个,大家看看有没有问题:
    Public Sub addfixedorder(ByVal userid As String, ByVal code As String, ByVal bigp As Decimal, ByVal smlp As Decimal)
                Dim mycon As SqlConnection
                Dim mcmd As SqlCommand
                mycon = New SqlConnection(ConnStr)
                mycon.Open()
                mcmd = New SqlCommand("addfixedorder", mycon)
                mcmd.CommandType = CommandType.StoredProcedure
                mcmd.Parameters.Add("@userid", userid)
                mcmd.Parameters.Add("@code", code)
                mcmd.Parameters.Add("@bigp", bigp)
                mcmd.Parameters.Add("@smlp", smlp)
                mcmd.ExecuteNonQuery()
                mycon.Close()
                mycon.Dispose()
            End Sub
      

  5.   

    问一下楼主tb12.Text 和 tb13.Text 是不是有一个为空串?
      

  6.   

    http://support.microsoft.com/default.aspx?scid=kb;zh-cn;818805
      

  7.   

    加上数据类型一试:
    .....
    mcmd = New SqlCommand("addfixedorder", mycon)
    mcmd.CommandType = CommandType.StoredProcedure
    mcmd.Parameters.Add("@userid", userid)
    mcmd.Parameters.Add("@code", code)Dim p As New SqlClient.SqlParameter("@bigp", SqlDbType.Decimal)
    p.Value = bigp
    mcmd.Parameters.Add(p)p =  New SqlClient.SqlParameter("@smlp", SqlDbType.Decimal)
    p.Value = smlp
    mcmd.Parameters.Add(p)
    ......还有你的判断:
    If tb11.Text <> "" And Val(tb12.Text) >= 0 And Val(tb13.Text) >= 0 Then
    最好改成:
    If tb11.Text <> "" AndAlso IsNumeric(tb12.Text) AndAlso IsNumeric(tb13.Text) Then
     a = Convert.ToDecimal(tb12.Text)
     b = Convert.ToDecimal(tb13.Text)
     Console.WriteLine(a.ToString())'看一下a,b是否为有效值?
     Console.WriteLine(b.ToString())
     Call mydatabase.addfixedorder(uid, tb11.Text, a, b)end if
      

  8.   

    没有空串,测试时也只输入数字
    3tzjq(永不言弃):你的提醒太棒了!~Thank you
      

  9.   

    根据大家特别是3tzjq(永不言弃)的提示,我找到问题出在哪了,我是在Page_load里面将数据库内容显示在文本框中,然后等待用户输入或更改,然后调用存储过程更新回数据库。
    问题出在:当在文本框中更改原来的数字,比如从100改为3,然后点UPDATE按钮,调用上面代码Console.WriteLine(a.ToString())
     Console.WriteLine(b.ToString())
     Call mydatabase.addfixedorder(uid, tb11.Text, a, b)
    但是这时a,b的值仍然为100,不是我在文本框里输入的3,怎么会这样?
      

  10.   

    可能是你为这个文本框是绑定了某个数据字段,修改b 它的值后直接更新,但这时它还没有结束编辑,所以返回的仍然是原来值。
    如果是这样,在更新前先: 
    Me.BindingContext(datasource,datamember).EndCurrentEdit()
    Call mydatabase.addfixedorder.....
      

  11.   

    dtfixedorder = New DataSet
            dtfixedorder = mydatabase.RunProc("select * from fixedorder where userid='" + Session.Item("userid") + "'", dtfixedorder, "fixedorder")tb11.Text = dtfixedorder.Tables(0).Rows(r)("code") : tb12.Text = dtfixedorder.Tables(0).Rows(r)("bigp") : tb13.Text = dtfixedorder.Tables(0).Rows(r)("smlp")
    这样的方式应该不是绑定啊,对吗?请赐教
      

  12.   

    Dim mydatabase As mydb.DataBase
        Dim dtfixedorder As DataSet
        Private Sub bondtextbox(ByVal startrowindex As Int16)
            Dim r As Int16
            Dim mcode As String, mbigp As Decimal, msmlp As Decimal
            r = startrowindex
            If dtfixedorder.Tables(0).Rows.Count - r = 1 Then            mcode = dtfixedorder.Tables(0).Rows(r)("code") : mbigp = dtfixedorder.Tables(0).Rows(r)("bigp") : msmlp = dtfixedorder.Tables(0).Rows(r)("smlp")
                tb11.Text = mcode : tb12.Text = mbigp : tb13.Text = msmlp
            End If
            ...
            end sub
    '-----------------------
    Private Sub input()
            Dim uid As String
            Dim a As Decimal, b As Decimal
            If Session.Item("userid") <> "" Then
                uid = Session.Item("userid")
            Else
                Exit Sub
            End If
            If tb11.Text <> "" AndAlso IsNumeric(tb12.Text) AndAlso IsNumeric(tb13.Text) Then
                a = Convert.ToDecimal(tb12.Text)
                b = Convert.ToDecimal(tb13.Text)
                Response.Write(a.ToString) '看一下a,b是否为有效值?
                Response.Write(b.ToString)
                Call mydatabase.addfixedorder(uid, tb11.Text, a, b)        End If
            If tb21.Text <> "" And Val(tb22.Text) >= 0 And Val(tb23.Text) >= 0 Then
                Call mydatabase.addfixedorder(uid, tb21.Text, tb22.Text, tb23.Text)
            End If
       ...
            end sub
    '---------
       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            mydatabase = New mydb.DataBase
            dtfixedorder = New DataSet
            dtfixedorder = mydatabase.RunProc("select * from fixedorder where userid='" + Session.Item("userid") + "'", dtfixedorder, "fixedorder")
            Call bondtextbox(0)
        End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            tb21.Text = tb11.Text : tb22.Text = tb12.Text : tb23.Text = tb13.Text
            '===到这就不对了,我在文本框输了新内容,可这还是还原为旧的啦
            Call input()
        End Sub
      

  13.   

    tb21.Text = tb11.Text : tb22.Text = tb12.Text : tb23.Text = tb13.Text
            '===到这就不对了,我在文本框输了新内容,可这还是还原为旧的啦
    这句是更新之前的测试。
      

  14.   

    刚看,看不出问题所在。
     If tb11.Text <> "" AndAlso IsNumeric(tb12.Text) AndAlso IsNumeric(tb13.Text) Then
    后面为什么还要个:
     If tb21.Text <> "" And Val(tb22.Text) >= 0 And Val(tb23.Text) >= 0 Then.....?你用代码的方式追加内容试试:
    tb12.Text = "789.4"
    tb13.Text = "1542"
    Update......
      

  15.   

    回复人: 3tzjq(永不言弃)
    刚看,看不出问题所在。
     If tb11.Text <> "" AndAlso IsNumeric(tb12.Text) AndAlso IsNumeric(tb13.Text) Then
    后面为什么还要个:
     If tb21.Text <> "" And Val(tb22.Text) >= 0 And Val(tb23.Text) >= 0 Then.....?这个只不过是第二行文本框呀,相当于第二条记录,可以先不管它
    一般的像这样把数据库记录显示在一行行的文本框中,再用修改后的数据更新回数据库是怎么做的?
      

  16.   

    If tb11.Text <> "" AndAlso IsNumeric(tb12.Text) AndAlso IsNumeric(tb13.Text) Then
                tb12.Text = "9.9"
                tb13.Text = "8.8"
                a = Convert.ToDecimal(tb12.Text)
                b = Convert.ToDecimal(tb13.Text)
                Call mydatabase.addfixedorder(uid, tb11.Text, a, b)
            End If
    我在这按你说的改了一下,这样是可以的,更新成功
      

  17.   

    在设计数据库的时候,字段为什么不用varchar
    我觉得这样更好,比char的方便多了。
      

  18.   

    那应该是文本框内容验证的问题,你写它的Validated事件看一下:
    Private Sub tb12_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb12.Validated
      MessageBox.Show(tb12.Text)'文本是否已更改?
    End Sub
      

  19.   

    3tzjq(永不言弃):多谢你的帮助,问题已解决,是回发问题,将
    If (Not IsPostBack) Then
                
    End If
    加在文本框绑定数据前即可。
    祝你身体健康,学业有成,事业进步!