Asp.net 更新存储过程,但不更新其中一列

Asp.net 更新存储过程,但不更新其中一列,asp.net,sql-server,vb.net,stored-procedures,query-string,Asp.net,Sql Server,Vb.net,Stored Procedures,Query String,如果我在SQL Server中执行存储过程,一切都会按预期工作,但是当我使用web表单执行时,就会出现问题 除了列Estado之外,所有内容似乎都在更新中,参数Estado(葡萄牙语中的意思是Status)是通过检查checkbox1是否被选中而获得的,如果被选中,那么变量Estado将被设置为Completado(已完成)和Pendente(挂起)如果没有 然后,我将参数@Estado的值指定给变量Estado的值 下面您可以看到按钮中的代码以及存储过程 Dim ConnStr As Str

如果我在SQL Server中执行存储过程,一切都会按预期工作,但是当我使用web表单执行时,就会出现问题

除了列
Estado
之外,所有内容似乎都在更新中,参数
Estado
(葡萄牙语中的意思是
Status
)是通过检查checkbox1是否被选中而获得的,如果被选中,那么变量
Estado
将被设置为
Completado
(已完成)和
Pendente
挂起
)如果没有

然后,我将参数
@Estado
的值指定给变量
Estado
的值

下面您可以看到
按钮中的代码以及存储过程

 Dim ConnStr As String = "Data Source=MARTIM-PC\SQLEXPRESS;Initial Catalog=EnotelSuporte;Integrated Security=True"
    Dim SqlCommand As New System.Data.SqlClient.SqlCommand
    SqlCommand.CommandType = Data.CommandType.StoredProcedure
    SqlCommand.CommandText = "Pedido_Update"
    SqlCommand.Parameters.Add("@PedidoId", Data.SqlDbType.NVarChar).Value = PedidoID
    SqlCommand.Parameters.Add("@Resolucao", Data.SqlDbType.NText).Value = TextBox3.Text
    SqlCommand.Parameters.Add("@Observacoes", Data.SqlDbType.NText).Value = TextBox2.Text

    If CheckBox1.Checked = True Then
        Estado = "Completado"
    ElseIf CheckBox1.Checked = False Then
        Estado = "Pendente"
    End If

    SqlCommand.Parameters.Add("@Estado", Data.SqlDbType.NVarChar).Value = Estado
    SqlCommand.Parameters.Add("@IdDepartamento", Data.SqlDbType.Int).Value = DropDownList1.SelectedValue
    SqlCommand.Parameters.Add("@IdTipoGeral", Data.SqlDbType.Int).Value = DropDownList2.SelectedValue
    SqlCommand.Parameters.Add("@IdTipoEspecifico", Data.SqlDbType.Int).Value = DropDownList3.SelectedValue
    SqlCommand.Parameters.Add("@IdHotel", Data.SqlDbType.Int).Value = DropDownList4.SelectedValue
    SqlCommand.Parameters.Add("@Descricao", Data.SqlDbType.NText).Value = TextBox1.Text

    Dim SqlConnection As New System.Data.SqlClient.SqlConnection(ConnStr)
    SqlCommand.Connection = SqlConnection

    SqlConnection.Open()
    SqlCommand.ExecuteNonQuery()
    SqlConnection.Close()
按建议使用局部变量的已编辑代码(仍然不起作用):

我也尝试过这样做,以消除对变量的需求(这也不起作用,开始认为问题在于使用复选框1):

这是存储过程:

ALTER PROCEDURE [dbo].[Pedido_Update]  
    @PedidoId int,
    @Observacoes ntext,
    @IdDepartamento int,
    @IdHotel int,
    @IdTipoEspecifico int,
    @IdTipoGeral int,
    @Resolucao ntext,
    @Descricao ntext,
    @Estado nvarchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE Pedidos 
    SET Resolucao = @Resolucao,
        Observacoes = @Observacoes,
        IdDepartamento = @IdDepartamento, 
        IdHotel = @IdHotel, 
        IdTipoEspecifico = @IdTipoEspecifico,
        IdTipoGeral = @IdTipoGeral,
        PedidoDescricao = @Descricao,
        Estado = @Estado
    WHERE 
        IdPedido = @PedidoId
END

刚刚注意到一些有趣的事情。
CheckBox1.Checked
的值是
True
当URL中没有查询字符串时(例如:
http://localhost:50851/Gestor/Pedidos/PedidoVisualizar.aspx
)。当存在查询字符串时(示例:
http://localhost:50851/Gestor/Pedidos/PedidoVisualizar.aspx?PedidoId=4
CheckBox1.Checked
设置为
False
,与是否选中无关。

代码似乎没问题。但如果我理解正确,您使用的是“全局”变量存储EtSADO,它依赖于Web窗体中的控件,您应该考虑在不同的会话中该表单可能会在保存SQL上的数据之后更改ESTADO中的值。 换句话说,当您为所有打开的会话共享此变量时,您不能相信您获得的值是webform设置的值

如果不是这样,您可能需要从asp.net应用程序中提供更多信息和一些代码
希望帮助

ASP.NET代码中声明的变量
Estado
在哪里?它是一个全局变量,不在任何方法中。不确定这是否与您的问题相关,但您声明了nvarchar类型的参数@peidoid,但它应该是一个整数。这项工作
SqlCommand.Parameters.Add(@Estado),Data.SqlDbType.NVarChar).Value=“Some Value”
如果这样做,问题在于
estado
变量(可能使其成为局部变量?)如果没有问题,则可能是存储过程出现了问题。我已将代码更改为使用局部变量,请检查代码。但仍然不起作用。我尝试使用此代码删除所有变量,但仍然不起作用。w
if CheckBox1.Checked然后SqlCommand.Parameters.Add(“@Estado”,Data.SqlDbType.NVarChar).Value=“Completado”ElseIf CheckBox1.Checked=False,然后是SqlCommand.Parameters.Add(“@Estado”,Data.SqlDbType.NVarChar)。Value=“Pendente”End If
你调试过吗?我的意思是,你能在调用SP之前看到CheckBox1的值吗?是的,即使我检查它,它也被设置为False。只是注意到了一些有趣的事情。
CheckBox1.Checked
的值是
True
当URL中没有查询字符串时(示例:
http://localhost:50851/Gestor/Pedidos/PedidoVisualizar.aspx
)。当存在查询字符串时(例如:
http://localhost:50851/Gestor/Pedidos/PedidoVisualizar.aspx?PedidoId=4
CheckBox1.Checked
设置为
False
,与是否选中无关。那么,在调用SP之前,您可能正在将复选框与来自DB的以前数据绑定。是否使用Page.IsPostBack进行检查?您应该在首次加载页面/用户控件时绑定或设置值以进行控制。但在之所以保存它(回发),是因为将用DB中的值覆盖表单值。
    If CheckBox1.Checked Then
        SqlCommand.Parameters.Add("@Estado", Data.SqlDbType.NVarChar).Value = "Completado"
    ElseIf CheckBox1.Checked = False Then
        SqlCommand.Parameters.Add("@Estado", Data.SqlDbType.NVarChar).Value = "Pendente"
    End If
ALTER PROCEDURE [dbo].[Pedido_Update]  
    @PedidoId int,
    @Observacoes ntext,
    @IdDepartamento int,
    @IdHotel int,
    @IdTipoEspecifico int,
    @IdTipoGeral int,
    @Resolucao ntext,
    @Descricao ntext,
    @Estado nvarchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE Pedidos 
    SET Resolucao = @Resolucao,
        Observacoes = @Observacoes,
        IdDepartamento = @IdDepartamento, 
        IdHotel = @IdHotel, 
        IdTipoEspecifico = @IdTipoEspecifico,
        IdTipoGeral = @IdTipoGeral,
        PedidoDescricao = @Descricao,
        Estado = @Estado
    WHERE 
        IdPedido = @PedidoId
END