数据库没有在数据库asp.net中更新

时间:2017-01-29 05:58:56

标签: sql asp.net .net vb.net

未找到任何错误,但数据未在数据库中更新,并且首次加载时显示错误

  

位置0没有行

以及在gridview下面显示的数据。

但是当菜单数据的加载显示在文本框中时。

更新按钮代码

Protected Sub taxsubmit_Click(sender As Object, e As ImageClickEventArgs) Handles taxsubmit.Click
    Using con As SqlConnection = New SqlConnection(strConnString)
        Using cmd As SqlCommand = New SqlCommand
            cmd.Connection = con
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "UPDATE [Deduction] SET [IncomeTax] = @IncomeTax, [SalesTax] = @SalesTax, [ServiceTax] = @ServiceTax, [LabourCess] = @LabourCess, [SocityTax] = @SocityTax, [ESIC] = @ESIC, [EPF] = @EPF, [Security] = @Security, [FinYear] = @FinYear, [Condition1] = @Condition1, [Condition2] = @Condition2, [Condition3] = @Condition3, [CompID] = " + Session("Companydetl") + " WHERE [DedID] = " + dedid + ""
            cmd.Parameters.AddWithValue("@IncomeTax", Income_Tax.Text)
            cmd.Parameters.AddWithValue("@SalesTax", Sales.Text)
            cmd.Parameters.AddWithValue("@ServiceTax", Service.Text)
            cmd.Parameters.AddWithValue("@LabourCess", Labour_Cess.Text)
            cmd.Parameters.AddWithValue("@SocityTax", Society.Text)
            cmd.Parameters.AddWithValue("@ESIC", ESIC.Text)
            cmd.Parameters.AddWithValue("@EPF", EPF.Text)
            cmd.Parameters.AddWithValue("@Security", Security.Text)
            cmd.Parameters.AddWithValue("@FinYear", Fin_Year.SelectedValue)
            cmd.Parameters.AddWithValue("@Condition1", Cond1.Text)
            cmd.Parameters.AddWithValue("@Condition2", Cond2.Text)
            cmd.Parameters.AddWithValue("@Condition3", Cond3.Text)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
End Sub

数据显示代码

Using con As SqlConnection = New SqlConnection(strConnString)
            Using cmd As SqlCommand = New SqlCommand
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "Select DedID, IncomeTax, SalesTax, ServiceTax, LabourCess, SocityTax, ESIC, EPF, 
                                    Security, FinYear, Condition1, Condition2, Condition3 from Deduction where CompID = '" + Session("Companydetl") + "'"
                Dim dt As New DataTable()
                con.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                Try
                    dt.Load(reader)
                    Income_Tax.Text = dt.Rows(0).Item("IncomeTax").ToString.Trim()
                    Labour_Cess.Text = dt.Rows(0).Item("LabourCess").ToString.Trim()
                    ESIC.Text = dt.Rows(0).Item("ESIC").ToString.Trim()
                    EPF.Text = dt.Rows(0).Item("EPF").ToString.Trim()
                    Society.Text = dt.Rows(0).Item("SocityTax").ToString.Trim()
                    Service.Text = dt.Rows(0).Item("ServiceTax").ToString.Trim()
                    Sales.Text = dt.Rows(0).Item("SalesTax").ToString.Trim()
                    Security.Text = dt.Rows(0).Item("Security").ToString.Trim()
                    Fin_Year.SelectedValue = dt.Rows(0).Item("FinYear").ToString.Trim()
                    Cond1.Text = dt.Rows(0).Item("Condition1").ToString.Trim()
                    Cond2.Text = dt.Rows(0).Item("Condition2").ToString.Trim()
                    Cond3.Text = dt.Rows(0).Item("Condition3").ToString.Trim()
                    dedid = dt.Rows(0).Item("DedID").ToString.Trim()
                    con.Close()
                Catch ex As Exception
                    MsgBox(ex.Message, vbOKOnly, "Error")
                End Try
            End Using
        End Using

Image on direct load of page after login.

Image After loading page from the above menu.

作为新手,请帮助。

3 个答案:

答案 0 :(得分:1)

您似乎已将dedid创建为班级中的字段或属性。您的问题可能会发生,因为那些不会在页面加载之间持续存在的值。

尝试将Session变量存储在CompID变量中。

答案 1 :(得分:0)

如果切换到Entity Framework,您的代码将如下所示:

Using oDb As New MyDbContext(ConnectionString)
  Dim oDeduction as Deduction

  oDeduction = oDb.Deductions.Where(Function(D) D.DedID = dedid).Single
  oDeduction.IncomeTax = Income_Tax.Text
  oDeduction.SalesTax = Sales.Text
  oDeduction.ServiceTax = Service.Text
  oDeduction.LabourCess = Labour_Cess.Text
  oDeduction.SocityTax = Society.Text
  oDeduction.ESIC = ESIC.Text
  oDeduction.EPF = EPF.Text
  oDeduction.Security = Security.Text
  oDeduction.FinYear = Fin_Year.SelectedValue
  oDeduction.Condition1 = Cond1.Text
  oDeduction.Condition2 = Cond2.Text
  oDeduction.Condition3 = Cond3.Text

  oDb.SaveChanges()
End Using

请注意,我只是通过TextPad中的内存一起敲打它 - 它未经测试,语法可能不完全正确。它只是概念的一个样本。

答案 2 :(得分:0)

谢谢大家的支持,但我发现了我的观点

If Session("login") = Nothing Then
        FormsAuthentication.SignOut()
        Session.Abandon()
        Session.RemoveAll()
        FormsAuthentication.RedirectToLoginPage()
    ElseIf Not IsPostBack Then
        Using con As SqlConnection = New SqlConnection(strConnString)
            Using cmd As SqlCommand = New SqlCommand
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "Select * from Deduction where CompID = '" + Session("Companydetl") + "'"
                Dim dt As New DataTable()
                con.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                Try
                    dt.Load(reader)
                    Income_Tax.Text = dt.Rows(0).Item("IncomeTax").ToString.Trim()
                    Labour_Cess.Text = dt.Rows(0).Item("LabourCess").ToString.Trim()
                    ESIC.Text = dt.Rows(0).Item("ESIC").ToString.Trim()
                    EPF.Text = dt.Rows(0).Item("EPF").ToString.Trim()
                    Society.Text = dt.Rows(0).Item("SocityTax").ToString.Trim()
                    Service.Text = dt.Rows(0).Item("ServiceTax").ToString.Trim()
                    Sales.Text = dt.Rows(0).Item("SalesTax").ToString.Trim()
                    Security.Text = dt.Rows(0).Item("Security").ToString.Trim()
                    Fin_Year.SelectedValue = dt.Rows(0).Item("FinYear").ToString.Trim()
                    Cond1.Text = dt.Rows(0).Item("Condition1").ToString.Trim()
                    Cond2.Text = dt.Rows(0).Item("Condition2").ToString.Trim()
                    Cond3.Text = dt.Rows(0).Item("Condition3").ToString.Trim()
                    'Session("deductionid") = dt.Rows(0).Item("DedID").ToString.Trim()
                    con.Close()
                Catch ex As Exception
                    MsgBox(ex.Message, vbOKOnly, "Error")
                End Try
            End Using
        End Using
    End If

当我添加" Not IsPostBack"然后所有数据都在更新,工作感谢所有人的支持 发现于Post Back Error Same

谢谢大家

更新按钮

Using con As SqlConnection = New SqlConnection(strConnString)
        con.Open()
        Using cmd As SqlCommand = New SqlCommand
            cmd.Connection = con
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "UPDATE Deduction SET IncomeTax=" + Income_Tax.Text + ", SalesTax=" + Sales.Text + ", ServiceTax=" + Service.Text + ", LabourCess=" + Labour_Cess.Text + ", 
              SocityTax=" + Society.Text + ", ESIC=" + ESIC.Text + ", EPF=" + EPF.Text + ", Security=" + Security.Text + ", FinYear=" + Fin_Year.SelectedValue + ", 
              Condition1='" + Cond1.Text + "', Condition2='" + Cond2.Text + "', Condition3='" + Cond3.Text + "' WHERE CompID = " + Session("Companydetl") + ""
            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message, vbOKOnly, "Error")
            End Try
        End Using
        con.Close()
    End Using