我没有多次使用gridview数据控件。我有一个gridview,当用户点击编辑时,他们有文本框来填写(完成者,完成日期,用户创建,页数)。当他们单击“更新”时,我需要将数据插入到数据库中,并将更新的记录从gridview中隐藏(或删除)。我无法工作的唯一部分是更新数据库。
这是我的gridview代码:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Job_No" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
CssClass="label">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<%-- <asp:CheckBoxField DataField="Iscomplete" HeaderText="Complete"
SortExpression="Iscomplete" />--%>
<%-- <asp:TemplateField HeaderText="Completed Date" SortExpression="Completed_Date" >
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ReadOnly="True" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Job_No" HeaderText="Job Number" InsertVisible="False"
ReadOnly="True" SortExpression="Job_No" />
<asp:CheckBoxField DataField="Report" HeaderText="Report"
SortExpression="Report" />
<asp:TemplateField HeaderText="Complete" SortExpression="Iscomplete">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Enabled="false"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Completed By" SortExpression="Completed_By">
<EditItemTemplate>
<asp:TextBox ID="txtCompletedBy" runat="server" Text='<%# Bind("Completed_By") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Completed_By") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Completed Date" SortExpression="Completed_Date">
<EditItemTemplate>
<asp:TextBox ID="txtCompletedDate" runat="server" Text='<%# Bind("Completed_Date") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Completed_Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Created" SortExpression="User_Created">
<EditItemTemplate>
<asp:TextBox ID="txtUserCreated" runat="server" Text='<%# Eval("User_Created") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("User_Created") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number of Pages"
SortExpression="Number_of_Pages">
<EditItemTemplate>
<asp:TextBox ID="txtNumberofPages" runat="server" Text='<%# Bind("Number_of_Pages") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Number_of_Pages") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Total" HeaderText="Total Pages"
SortExpression="Total" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
<asp:CheckBoxField DataField="Confidential" HeaderText="Confidential"
SortExpression="Confidential" ReadOnly="True" />
<asp:BoundField DataField="Email" HeaderText="Email"
SortExpression="Email" ReadOnly="True" />
<asp:BoundField DataField="Date_Created" HeaderText="Date Requested"
SortExpression="Date_Created" DataFormatString="{0:d}" ReadOnly="True" />
<asp:BoundField DataField="Date_Needed" HeaderText="Date Needed"
SortExpression="Date_Needed" DataFormatString="{0:d}" ReadOnly="True" />
<asp:BoundField DataField="Time_Needed" HeaderText="Time Needed"
SortExpression="Time_Needed" ReadOnly="True" />
<asp:BoundField DataField="Building_Location" HeaderText="Building Location"
SortExpression="Building_Location" ReadOnly="True" />
<asp:BoundField DataField="Number_of_Copies" HeaderText="Number of Copies"
SortExpression="Number_of_Copies" ReadOnly="True" />
<asp:BoundField DataField="Paper_Size" HeaderText="Paper Size"
SortExpression="Paper_Size" ReadOnly="True" />
<asp:BoundField DataField="Account_Charged" HeaderText="Account Charged"
SortExpression="Account_Charged" ReadOnly="True" />
<asp:CheckBoxField DataField="Two_Part_Paper" HeaderText="Two Part Paper"
SortExpression="Two_Part_Paper" ReadOnly="True" />
<asp:CheckBoxField DataField="Three_Part_Paper" HeaderText="Three Part Paper"
SortExpression="Three_Part_Paper" ReadOnly="True" />
<asp:CheckBoxField DataField="Cut_and_Pad" HeaderText="Cut and Pad"
SortExpression="Cut_and_Pad" ReadOnly="True" />
<asp:CheckBoxField DataField="Collate_Only" HeaderText="Collate Only"
SortExpression="Collate_Only" ReadOnly="True" />
<asp:CheckBoxField DataField="Single" HeaderText="Single"
SortExpression="Single" ReadOnly="True" />
<asp:CheckBoxField DataField="Front_and_Back" HeaderText="Front and Back"
SortExpression="Front_and_Back" ReadOnly="True" />
<asp:CheckBoxField DataField="Staple" HeaderText="Staple"
SortExpression="Staple" ReadOnly="True" />
<asp:CheckBoxField DataField="Saddle_Stapling" HeaderText="Saddle Stapling"
SortExpression="Saddle_Stapling" ReadOnly="True" />
<asp:CheckBoxField DataField="Reduction" HeaderText="Reduction"
SortExpression="Reduction" ReadOnly="True" />
<asp:CheckBoxField DataField="Laminate" HeaderText="Laminate"
SortExpression="Laminate" ReadOnly="True" />
<asp:CheckBoxField DataField="Folder_Stuffer_Sealer"
HeaderText="Folder Stuffer Sealer"
SortExpression="Folder_Stuffer_Sealer" ReadOnly="True" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" ReadOnly="True" />
<asp:BoundField DataField="File_Name1" HeaderText="File Name 1"
SortExpression="File_Name1" ReadOnly="True" />
<asp:BoundField DataField="File_Name2" HeaderText="File Name 2"
SortExpression="File_Name2" ReadOnly="True" />
<asp:BoundField DataField="File_Name3" HeaderText="File Name 3"
SortExpression="File_Name3" ReadOnly="True" />
<asp:BoundField DataField="File_Name4" HeaderText="File Name 4"
SortExpression="File_Name4" ReadOnly="True" />
<asp:BoundField DataField="File_Name5" HeaderText="File Name 5"
SortExpression="File_Name5" ReadOnly="True" />
<asp:BoundField DataField="File_Name6" HeaderText="File Name 6"
SortExpression="File_Name6" ReadOnly="True" />
<asp:BoundField DataField="File_Name7" HeaderText="File Name7"
SortExpression="File_Name7" ReadOnly="True" />
<asp:BoundField DataField="File_Name8" HeaderText="File Name 8"
SortExpression="File_Name8" ReadOnly="True" />
<asp:BoundField DataField="File_Name9" HeaderText="File Name 9"
SortExpression="File_Name9" ReadOnly="True" />
<asp:BoundField DataField="File_Name10" HeaderText="File Name 10"
SortExpression="File_Name10" ReadOnly="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:DuplicatingConnectionString %>"
DeleteCommand="DELETE FROM [Duplicating_Request_Table] WHERE [Job_No] = @original_Job_No AND (([Date_Created] = @original_Date_Created) OR ([Date_Created] IS NULL AND @original_Date_Created IS NULL)) AND (([User_Created] = @original_User_Created) OR ([User_Created] IS NULL AND @original_User_Created IS NULL)) AND (([Email] = @original_Email) OR ([Email] IS NULL AND @original_Email IS NULL)) AND (([Building_Location] = @original_Building_Location) OR ([Building_Location] IS NULL AND @original_Building_Location IS NULL)) AND (([Number_of_Copies] = @original_Number_of_Copies) OR ([Number_of_Copies] IS NULL AND @original_Number_of_Copies IS NULL)) AND (([Date_Needed] = @original_Date_Needed) OR ([Date_Needed] IS NULL AND @original_Date_Needed IS NULL)) AND (([Time_Needed] = @original_Time_Needed) OR ([Time_Needed] IS NULL AND @original_Time_Needed IS NULL)) AND (([Paper_Size] = @original_Paper_Size) OR ([Paper_Size] IS NULL AND @original_Paper_Size IS NULL)) AND (([Account_Charged] = @original_Account_Charged) OR ([Account_Charged] IS NULL AND @original_Account_Charged IS NULL)) AND (([Two_Part_Paper] = @original_Two_Part_Paper) OR ([Two_Part_Paper] IS NULL AND @original_Two_Part_Paper IS NULL)) AND (([Three_Part_Paper] = @original_Three_Part_Paper) OR ([Three_Part_Paper] IS NULL AND @original_Three_Part_Paper IS NULL)) AND (([Cut_and_Pad] = @original_Cut_and_Pad) OR ([Cut_and_Pad] IS NULL AND @original_Cut_and_Pad IS NULL)) AND (([Collate_Only] = @original_Collate_Only) OR ([Collate_Only] IS NULL AND @original_Collate_Only IS NULL)) AND (([Single] = @original_Single) OR ([Single] IS NULL AND @original_Single IS NULL)) AND (([Front_and_Back] = @original_Front_and_Back) OR ([Front_and_Back] IS NULL AND @original_Front_and_Back IS NULL)) AND (([Staple] = @original_Staple) OR ([Staple] IS NULL AND @original_Staple IS NULL)) AND (([Saddle_Stapling] = @original_Saddle_Stapling) OR ([Saddle_Stapling] IS NULL AND @original_Saddle_Stapling IS NULL)) AND (([Reduction] = @original_Reduction) OR ([Reduction] IS NULL AND @original_Reduction IS NULL)) AND (([Laminate] = @original_Laminate) OR ([Laminate] IS NULL AND @original_Laminate IS NULL)) AND (([Folder_Stuffer_Sealer] = @original_Folder_Stuffer_Sealer) OR ([Folder_Stuffer_Sealer] IS NULL AND @original_Folder_Stuffer_Sealer IS NULL)) AND (([Description] = @original_Description) OR ([Description] IS NULL AND @original_Description IS NULL)) AND (([Number_of_Pages] = @original_Number_of_Pages) OR ([Number_of_Pages] IS NULL AND @original_Number_of_Pages IS NULL)) AND (([Total] = @original_Total) OR ([Total] IS NULL AND @original_Total IS NULL)) AND (([Amount] = @original_Amount) OR ([Amount] IS NULL AND @original_Amount IS NULL)) AND (([Iscomplete] = @original_Iscomplete) OR ([Iscomplete] IS NULL AND @original_Iscomplete IS NULL)) AND (([Completed_By] = @original_Completed_By) OR ([Completed_By] IS NULL AND @original_Completed_By IS NULL)) AND (([Completed_Date] = @original_Completed_Date) OR ([Completed_Date] IS NULL AND @original_Completed_Date IS NULL)) AND (([File_Name1] = @original_File_Name1) OR ([File_Name1] IS NULL AND @original_File_Name1 IS NULL)) AND (([File_Name2] = @original_File_Name2) OR ([File_Name2] IS NULL AND @original_File_Name2 IS NULL)) AND (([File_Name3] = @original_File_Name3) OR ([File_Name3] IS NULL AND @original_File_Name3 IS NULL)) AND (([File_Name4] = @original_File_Name4) OR ([File_Name4] IS NULL AND @original_File_Name4 IS NULL)) AND (([File_Name5] = @original_File_Name5) OR ([File_Name5] IS NULL AND @original_File_Name5 IS NULL)) AND (([File_Name6] = @original_File_Name6) OR ([File_Name6] IS NULL AND @original_File_Name6 IS NULL)) AND (([File_Name7] = @original_File_Name7) OR ([File_Name7] IS NULL AND @original_File_Name7 IS NULL)) AND (([File_Name8] = @original_File_Name8) OR ([File_Name8] IS NULL AND @original_File_Name8 IS NULL)) AND (([File_Name9] = @original_File_Name9) OR ([File_Name9] IS NULL AND @original_File_Name9 IS NULL)) AND (([File_Name10] = @original_File_Name10) OR ([File_Name10] IS NULL AND @original_File_Name10 IS NULL))"
InsertCommand="INSERT INTO [Duplicating_Request_Table] ([Date_Created], [User_Created], [Email], [Building_Location], [Number_of_Copies], [Date_Needed], [Time_Needed], [Paper_Size], [Account_Charged], [Two_Part_Paper], [Three_Part_Paper], [Cut_and_Pad], [Collate_Only], [Single], [Front_and_Back], [Staple], [Saddle_Stapling], [Reduction], [Laminate], [Folder_Stuffer_Sealer], [Description], [Number_of_Pages], [Total], [Amount], [Iscomplete], [Completed_By], [Completed_Date], [File_Name1], [File_Name2], [File_Name3], [File_Name4], [File_Name5], [File_Name6], [File_Name7], [File_Name8], [File_Name9], [File_Name10]) VALUES (@Date_Created, @User_Created, @Email, @Building_Location, @Number_of_Copies, @Date_Needed, @Time_Needed, @Paper_Size, @Account_Charged, @Two_Part_Paper, @Three_Part_Paper, @Cut_and_Pad, @Collate_Only, @Single, @Front_and_Back, @Staple, @Saddle_Stapling, @Reduction, @Laminate, @Folder_Stuffer_Sealer, @Description, @Number_of_Pages, @Total, @Amount, @Iscomplete, @Completed_By, @Completed_Date, @File_Name1, @File_Name2, @File_Name3, @File_Name4, @File_Name5, @File_Name6, @File_Name7, @File_Name8, @File_Name9, @File_Name10)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT Job_No, Date_Created, User_Created, Email, Building_Location, Number_of_Copies, Date_Needed, Time_Needed, Paper_Size, Account_Charged, Two_Part_Paper, Three_Part_Paper, Cut_and_Pad, Collate_Only, Single, Front_and_Back, Staple, Saddle_Stapling, Reduction, Laminate, Folder_Stuffer_Sealer, Description, Number_of_Pages, (Number_of_Copies * Number_of_Pages) AS Total, Amount, Iscomplete, Completed_By, Completed_Date, File_Name1, File_Name2, File_Name3, File_Name4, File_Name5, File_Name6, File_Name7, File_Name8, File_Name9, File_Name10, Report, Confidential FROM Duplicating_Request_Table WHERE (Iscomplete IS NULL)"
UpdateCommand="UPDATE [Duplicating_Request_Table] SET [Number_of_Pages] = @Number_of_Pages, [Total] = @Total, [Amount] = @Amount, [Iscomplete] = @Iscomplete, [Completed_By] = @Completed_By, [Completed_Date] = @Completed_Date, report = @report WHERE [Job_No] = @original_Job_No">
<DeleteParameters>
<asp:Parameter Name="original_Job_No" Type="Int32" />
<asp:Parameter Name="original_Date_Created" Type="DateTime" />
<asp:Parameter Name="original_User_Created" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_Building_Location" Type="String" />
<asp:Parameter Name="original_Number_of_Copies" Type="Int32" />
<asp:Parameter DbType="Date" Name="original_Date_Needed" />
<asp:Parameter Name="original_Time_Needed" Type="String" />
<asp:Parameter Name="original_Paper_Size" Type="String" />
<asp:Parameter Name="original_Account_Charged" Type="String" />
<asp:Parameter Name="original_Two_Part_Paper" Type="Boolean" />
<asp:Parameter Name="original_Three_Part_Paper" Type="Boolean" />
<asp:Parameter Name="original_Cut_and_Pad" Type="Boolean" />
<asp:Parameter Name="original_Collate_Only" Type="Boolean" />
<asp:Parameter Name="original_Single" Type="Boolean" />
<asp:Parameter Name="original_Front_and_Back" Type="Boolean" />
<asp:Parameter Name="original_Staple" Type="Boolean" />
<asp:Parameter Name="original_Saddle_Stapling" Type="Boolean" />
<asp:Parameter Name="original_Reduction" Type="Boolean" />
<asp:Parameter Name="original_Laminate" Type="Boolean" />
<asp:Parameter Name="original_Folder_Stuffer_Sealer" Type="Boolean" />
<asp:Parameter Name="original_Description" Type="String" />
<asp:Parameter Name="original_Number_of_Pages" Type="Int32" />
<asp:Parameter Name="original_Total" Type="String" />
<asp:Parameter Name="original_Amount" Type="Decimal" />
<asp:Parameter Name="original_Iscomplete" Type="Boolean" />
<asp:Parameter Name="original_Completed_By" Type="String" />
<asp:Parameter Name="original_Completed_Date" Type="String" />
<asp:Parameter Name="original_File_Name1" Type="String" />
<asp:Parameter Name="original_File_Name2" Type="String" />
<asp:Parameter Name="original_File_Name3" Type="String" />
<asp:Parameter Name="original_File_Name4" Type="String" />
<asp:Parameter Name="original_File_Name5" Type="String" />
<asp:Parameter Name="original_File_Name6" Type="String" />
<asp:Parameter Name="original_File_Name7" Type="String" />
<asp:Parameter Name="original_File_Name8" Type="String" />
<asp:Parameter Name="original_File_Name9" Type="String" />
<asp:Parameter Name="original_File_Name10" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Date_Created" Type="DateTime" />
<asp:Parameter Name="User_Created" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Building_Location" Type="String" />
<asp:Parameter Name="Number_of_Copies" Type="Int32" />
<asp:Parameter DbType="Date" Name="Date_Needed" />
<asp:Parameter Name="Time_Needed" Type="String" />
<asp:Parameter Name="Paper_Size" Type="String" />
<asp:Parameter Name="Account_Charged" Type="String" />
<asp:Parameter Name="Two_Part_Paper" Type="Boolean" />
<asp:Parameter Name="Three_Part_Paper" Type="Boolean" />
<asp:Parameter Name="Cut_and_Pad" Type="Boolean" />
<asp:Parameter Name="Collate_Only" Type="Boolean" />
<asp:Parameter Name="Single" Type="Boolean" />
<asp:Parameter Name="Front_and_Back" Type="Boolean" />
<asp:Parameter Name="Staple" Type="Boolean" />
<asp:Parameter Name="Saddle_Stapling" Type="Boolean" />
<asp:Parameter Name="Reduction" Type="Boolean" />
<asp:Parameter Name="Laminate" Type="Boolean" />
<asp:Parameter Name="Folder_Stuffer_Sealer" Type="Boolean" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Number_of_Pages" Type="Int32" />
<asp:Parameter Name="Total" Type="String" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Iscomplete" Type="Boolean" />
<asp:Parameter Name="Completed_By" Type="String" />
<asp:Parameter Name="Completed_Date" Type="String" />
<asp:Parameter Name="File_Name1" Type="String" />
<asp:Parameter Name="File_Name2" Type="String" />
<asp:Parameter Name="File_Name3" Type="String" />
<asp:Parameter Name="File_Name4" Type="String" />
<asp:Parameter Name="File_Name5" Type="String" />
<asp:Parameter Name="File_Name6" Type="String" />
<asp:Parameter Name="File_Name7" Type="String" />
<asp:Parameter Name="File_Name8" Type="String" />
<asp:Parameter Name="File_Name9" Type="String" />
<asp:Parameter Name="File_Name10" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Number_of_Pages" Type="Int32" />
<asp:Parameter Name="Total" Type="String" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Iscomplete" Type="Boolean" />
<asp:Parameter Name="Completed_By" Type="String" />
<asp:Parameter Name="Completed_Date" Type="String" />
<asp:Parameter Name="report" />
<asp:Parameter Name="original_Job_No" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
这是我背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgridView();
}
}
protected void bindgridView()
{
try{
SqlConnection sqlcon = new SqlConnection("Data Source=10.50.1.42;Initial Catalog=Duplicating;User ID=sa;Password=Zanestate$");
string query = "Select * from duplicating_request_table";
DataSet ds = new DataSet();
SqlDataAdapter sqldt = new SqlDataAdapter(query, sqlcon);
sqldt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
if (ViewState["delRows"] != null)
{
int[] delIndices = (int[])ViewState["delRows"];
int itemsLength = delIndices.Count(indx => indx != 0);
//create the javascript array in the client side
//push the deleted row indexes from c# array to javascript array
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var indexArray = new Array;");
for (int i = 0; i < itemsLength; i++)
{
sb.Append("indexArray.push('" + delIndices[i] + "');");
}
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "arrayScript", sb.ToString());
//call the deleteRow function on the client side
this.ClientScript.RegisterStartupScript(typeof(Page), "del", "javascript:deletegrdvwRow()", true);
}
}
catch (Exception)
{ }
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridView1.EditIndex = e.NewEditIndex;
bindgridView();
}
catch (Exception exc)
{
Console.WriteLine(exc);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
string itemname = GridView1.DataKeys[row.RowIndex][0].ToString();
//TextBox txt_productName = (TextBox)row.FindControl("txt_updtprod");
Label JobNo = (Label)row.FindControl("Job_No");
TextBox CompletedBy = (TextBox)row.FindControl("txtCompletedby");
TextBox CompletedDate = (TextBox)row.FindControl("txtCompletedDate");
TextBox UserCreated = (TextBox)row.FindControl("txtUserCreated");
TextBox NumberofPages = (TextBox)row.FindControl("txtNumberofPages");
// TextBox TotalPages = (TextBox)row.Cells[4].Controls[0];
//write your logic here to update the data into the database
SqlCommand sqlcon = new SqlCommand("update duplicating_request_table set Completed_By='" + CompletedBy.Text + "',Completed Date='" + CompletedDate.Text + "',User_Created='" + UserCreated.Text + "',Number_of_Pages='" + NumberofPages.Text + "'where Job_No='" + JobNo + "'");
//SqlCommand sqlcon = new SqlCommand("UPDATE duplicating_request_table SET Completed_By=@Completed_By, Completed_Date=@Completed_Date, User_Created=@User_Created, Number_of_Pages=@Number_of_Pages WHERE Job_No=@JobNo");
GridView1.EditIndex = -1;
//those rows which are deleted previously while updating store it's indexs
if (ViewState["delRows"] == null)
{
int[] delIndices = new int[12];
delIndices[0] = e.RowIndex;
ViewState["delRows"] = delIndices;
}
else
{
int[] delIndices = (int[])ViewState["delRows"];
int itemsLength = delIndices.Count(indx => indx != 0);
delIndices[itemsLength] = e.RowIndex;
}
bindgridView();
}
catch (Exception exc)
{
Console.WriteLine(exc);
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
GridView1.EditIndex = -1;
bindgridView();
}
catch (Exception exc)
{
Console.WriteLine(exc);
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
//while moving to the next page, clear the viewstate
ViewState["delRows"] = null;
bindgridView();
}
catch (Exception)
{ }
}
我没有收到任何错误 - 但数据库中的数据没有更新。提前致谢。
答案 0 :(得分:0)
This C-sharpcorner article介绍如何使用SQL Server数据库和C#中的ASP.NET 2.0 GridView控件查看,更新和删除数据。
祝你好运!答案 1 :(得分:0)
您尚未执行命令。你需要这样的东西:
sqlcon.ExecuteNonQuery();
您还需要为此创建连接。
答案 2 :(得分:0)
我运行了你的代码,并添加了一个快速的异常响应(你的'try catch'例程,你没有任何东西,当它捕获异常时显示回复给你,这就是为什么没有显示。)来检查什么异常呈现......就我而言,它出现的是......
发生与网络相关或特定于实例的错误 建立与SQL Server的连接。找不到服务器或 无法访问。验证实例名称是否正确 SQL Server配置为允许远程连接。 (提供者:命名 管道提供程序,错误:40 - 无法打开与SQL Server的连接)
我希望它,因为有防火墙不让我连接到你的服务器,为什么不运行它,让我们知道什么是异常
catch (Exception ex)
{
Response.Write(ex.Message);
}