我正在使用ASP.NET 4.0开发一个Web应用程序,使用VB.NET作为代码隐藏。 我使用SQL Server 2008 R2作为后端。
我在项目的页面上遇到了非常罕见的问题。
我在Telerik Radgrid的文本框中输入了大约200个数据。
所有这些信息都将保存在数据库表中。当它第一次保存时,它工作正常并以与网格中显示的顺序相同的顺序保存。
但是当我尝试稍后更新它时,网格的最后一行,有时(不是每次都)保存在表格的最后一行。我不知道为什么,这是我想要回答的问题。
我希望数据的存储顺序与网格中显示的顺序完全相同。
但是在更新时,我发现最后一行网格存在问题,当我提交事务时,它在表中保存到最后一行,应该保存到最后一行。
我在下面提供VB.NET代码。
Protected Sub SaveBothGridData()
Dim sb1 As New StringBuilder
Dim tran1 As SqlTransaction
Dim tran2 As SqlTransaction
Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("DPMTConnectionString").ConnectionString)
Try
'get the existing datatable
Dim rowIndex As Integer = 0
If ViewState("DT_SR1_section1") IsNot Nothing Then
Dim dtCurrentTable1 As DataTable = DirectCast(ViewState("DT_SR1_section1"), DataTable)
If dtCurrentTable1.Rows.Count > 0 Then
For i As Integer = 0 To dtCurrentTable1.Rows.Count - 1
Dim box1 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox2"), TextBox)
Dim box3 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox3"), TextBox)
Dim box4 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox4"), TextBox)
Dim box5 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox5"), TextBox)
Dim box6 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox6"), TextBox)
Dim box7 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox7"), TextBox)
Dim box8 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox8"), TextBox)
Dim box9 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox9"), TextBox)
Dim box10 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox10"), TextBox)
Dim box11 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox11"), TextBox)
Dim box12 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox12"), TextBox)
Dim box13 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox13"), TextBox)
Dim box14 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("TextBox14"), TextBox)
dtCurrentTable1.Rows(i)("CAT") = box1.Text
dtCurrentTable1.Rows(i)("PTTL") = box2.Text
dtCurrentTable1.Rows(i)("C1") = box3.Text
dtCurrentTable1.Rows(i)("C2") = box4.Text
dtCurrentTable1.Rows(i)("C3") = box5.Text
dtCurrentTable1.Rows(i)("C4") = box6.Text
dtCurrentTable1.Rows(i)("C5") = box7.Text
dtCurrentTable1.Rows(i)("C6") = box8.Text
dtCurrentTable1.Rows(i)("C7") = box9.Text
dtCurrentTable1.Rows(i)("C8") = box10.Text
dtCurrentTable1.Rows(i)("C9") = box11.Text
dtCurrentTable1.Rows(i)("C10") = box12.Text
dtCurrentTable1.Rows(i)("CTTL") = box13.Text
dtCurrentTable1.Rows(i)("DIFF") = box14.Text
rowIndex += 1
sb1.Append(box1.Text & " " & box2.Text & " " & box3.Text & " " & box4.Text & " " & box5.Text & " " & box6.Text & " " & box7.Text & " " & box8.Text & " " & box9.Text & " " & box10.Text & " " & box11.Text & " " & box12.Text & " " & box13.Text & " " & box14.Text & " " & Now.ToString & vbCrLf)
Next
End If
'save datatable to database
Dim shadingno As Integer = 0
shadingno = Me.hdnShadingNo_section1.Value
cn.Open()
tran1 = cn.BeginTransaction
Dim mode As String = String.Empty
If Not Request.QueryString("shadingno") Is Nothing Then
Dim delcmd As New SqlCommand
delcmd.Connection = cn
delcmd.Transaction = tran1
delcmd.CommandType = CommandType.StoredProcedure
delcmd.CommandText = "sp_delete_shading_return_1_entry"
delcmd.Parameters.Add("@shadingno", SqlDbType.Int).Value = Request.QueryString("shadingno")
delcmd.Parameters.Add("@section", SqlDbType.NVarChar).Value = "section1"
delcmd.ExecuteNonQuery()
End If
'If Not Request.QueryString("shadingno") Is Nothing Then
' Dim obj As New db
' Dim str As String = "delete from shading_return_entry_general where shadingno=" & Request.QueryString("shadingno")
' str = str & ";delete from shading_return_entry_details where shadingno=" & Request.QueryString("shadingno")
' obj.insertAll(str)
' obj = Nothing
'End If
'Dim cmd1 As New SqlCommand
'cmd1.Transaction = tran
'cmd1.Connection = cn
'cmd1.CommandType = CommandType.StoredProcedure
'cmd1.CommandText = "sp_add_update_delete_shading_return_entry_general"
'cmd1.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "ADD"
'cmd1.Parameters.Add("@srno", SqlDbType.Int).Value = 0
'cmd1.Parameters.Add("@shadingno", SqlDbType.Int).Value = shadingno
'cmd1.Parameters.Add("@entrytimestamp", SqlDbType.DateTime).Value = Now
'cmd1.Parameters.Add("@approvalstatus", SqlDbType.NVarChar).Value = "NOT APPROVED"
'cmd1.Parameters.Add("@approvaltimestamp", SqlDbType.DateTime).Value = System.Data.SqlTypes.SqlDateTime.Null
'cmd1.ExecuteNonQuery()
Dim cmd2 As New SqlCommand
cmd2.Transaction = tran1
cmd2.Connection = cn
cmd2.CommandType = CommandType.StoredProcedure
cmd2.CommandText = "sp_add_update_delete_shading_return_1_heading_details"
cmd2.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "ADD"
cmd2.Parameters.Add("@shadingno", SqlDbType.Int).Value = shadingno
cmd2.Parameters.Add("@section", SqlDbType.NVarChar).Value = "section1"
cmd2.Parameters.Add("@col1heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol1Heading_section1.Value)
cmd2.Parameters.Add("@col2heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol2Heading_section1.Value)
cmd2.Parameters.Add("@col3heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol3Heading_section1.Value)
cmd2.Parameters.Add("@col4heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol4Heading_section1.Value)
cmd2.Parameters.Add("@col5heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol5Heading_section1.Value)
cmd2.Parameters.Add("@col6heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol6Heading_section1.Value)
cmd2.Parameters.Add("@col7heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol7Heading_section1.Value)
cmd2.Parameters.Add("@col8heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol8Heading_section1.Value)
cmd2.Parameters.Add("@col9heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol9Heading_section1.Value)
cmd2.Parameters.Add("@col10heading", SqlDbType.NVarChar).Value = UCase(Me.hdnCol10Heading_section1.Value)
cmd2.ExecuteNonQuery()
Dim sw As StreamWriter = IO.File.CreateText(Server.MapPath("../ADMIN/") & Now.Day & Now.Month & Now.Year & Now.Hour & Now.Minute & Now.Second & ".txt")
sw.Write(sb1.ToString)
sw.Flush()
sw.Close()
For i = 0 To dtCurrentTable1.Rows.Count - 1
Dim cmd As New SqlCommand
cmd.Transaction = tran1
cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "sp_add_update_delete_shading_return_1_entry_details"
cmd.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "ADD"
cmd.Parameters.Add("@shadingno", SqlDbType.Int).Value = shadingno
cmd.Parameters.Add("@section", SqlDbType.NVarChar).Value = "section1"
cmd.Parameters.Add("@cat", SqlDbType.NVarChar).Value = dtCurrentTable1.Rows(i).Item(0)
cmd.Parameters.Add("@pttl", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(1)
cmd.Parameters.Add("@col1", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(2)
cmd.Parameters.Add("@col2", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(3)
cmd.Parameters.Add("@col3", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(4)
cmd.Parameters.Add("@col4", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(5)
cmd.Parameters.Add("@col5", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(6)
cmd.Parameters.Add("@col6", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(7)
cmd.Parameters.Add("@col7", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(8)
cmd.Parameters.Add("@col8", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(9)
cmd.Parameters.Add("@col9", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(10)
cmd.Parameters.Add("@col10", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(11)
cmd.Parameters.Add("@cttl", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(12)
cmd.Parameters.Add("@diff", SqlDbType.Float).Value = dtCurrentTable1.Rows(i).Item(13)
cmd.ExecuteNonQuery()
Next
tran1.Commit()
'Dim eventMsg As String = "Shading Return entry section1 with shading no " & shadingno & " created"
'Dim obj1 As New com.hemalrathod.dpmt.DatabaseHandling
'obj1.SaveToUserEventLog(Request.Cookies.Item("loginname").Value, "Shading return entry 1 create", eventMsg, Now, Request.Cookies.Item("clientipaddress").Value)
'obj1 = Nothing
'Response.Redirect("Shading_View.aspx")
Else
Response.Write("ViewState is null")
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
cn.Close()
End Try
End Sub
在上面的代码中,我创建了一个保存数据的事务,在更新时,我首先删除原始数据,然后保存新数据。
问题在于只有第一笔交易'tran1'。我没有发现'tran2'有任何问题。
我认为问题在于提交交易。当它以某种方式提交时,最后一行网格,在表格中排在倒数第二位。
以下是我使用的SQL Server程序
在输入新数据之前删除现有数据的步骤
ALTER procedure [dbo].[sp_delete_shading_return_1_entry]
(
@shadingno int,
@section nvarchar(50)
)
as
delete from shading_return_1_heading_details
where shadingno = @shadingno and section = @section
delete from shading_return_1_entry_details
where shadingno = @shadingno and section = @section
delete from shading_return_entry_general
where shadingno = @shadingno
输入新标题数据的步骤(这不会产生任何问题)
ALTER procedure [dbo].[sp_add_update_delete_shading_return_1_heading_details]
(
@mode nvarchar(50),
@shadingno int,
@section nvarchar(50),
@col1heading nvarchar(50),
@col2heading nvarchar(50),
@col3heading nvarchar(50),
@col4heading nvarchar(50),
@col5heading nvarchar(50),
@col6heading nvarchar(50),
@col7heading nvarchar(50),
@col8heading nvarchar(50),
@col9heading nvarchar(50),
@col10heading nvarchar(50)
)
as
if @mode='ADD'
begin
insert into shading_return_1_heading_details values(@shadingno, @section, @col1heading, @col2heading, @col3heading, @col4heading, @col5heading, @col6heading, @col7heading, @col8heading, @col9heading, @col10heading)
end
将数据从radgrid存储到表中的过程(这实际上可能存在错误)
ALTER procedure [dbo].[sp_add_update_delete_shading_return_1_entry_details]
(
@mode nvarchar(50),
@shadingno int,
@section nvarchar(50),
@cat nvarchar(50),
@pttl float,
@col1 float,
@col2 float,
@col3 float,
@col4 float,
@col5 float,
@col6 float,
@col7 float,
@col8 float,
@col9 float,
@col10 float,
@cttl float,
@diff float
)
as
if @mode='ADD'
begin
insert into shading_return_1_entry_details
values(@shadingno, @section, @cat, @pttl, @col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @cttl, @diff)
end
在此之后,根据我的VB.NET代码,我正在提交'tran1'事务,然后有时不常出现问题。
简而言之,我希望radgrid中的所有行都以完全相同的顺序存储,但是在提交事务之后,有时候,最后一行网格存储在表的最后一行。
我该如何解决这个问题?
提前谢谢