c# - GridView 无法将数据更新到数据库中
问题描述
我正在尝试通过使用 n 层应用程序使用 gridview 来更新记录。我的插入和删除正在工作,但是当我尝试从 gridview 更新按钮更新记录时,它没有更新记录并检索旧记录。
这是存储过程
CREATE PROCEDURE [dbo].[UpdateJob]
(
@Job_ID int ,
@Title [varchar](50) ,
@Location [varchar](50),
@Exprience [varchar](500),
@Type_Contract [varchar](50) ,
@Posted_Date [varchar](50),
@Salary [varchar](50)
)
AS
update Job_Profile
set
@Title = @Title,
@Location = @Location,
@Exprience = @Exprience,
@Type_Contract = @Type_Contract,
@Posted_Date = @Posted_Date,
@Salary = @Salary
where
@Job_ID = @Job_ID
RETURN
GO
这是我的方法。
public void Job_update(int Job_ID, string title, string location, string exprience, string type_Contract, string posted_Date, string salary)
{
SqlConnection con = new SqlConnection(@"Data Source=NIRJOR\SQLEXPRESS;Initial Catalog=StudentJobPortal;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UpdateJob", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("Job_Id", Job_ID);
cmd.Parameters.AddWithValue("Title", title);
cmd.Parameters.AddWithValue("Location", location);
cmd.Parameters.AddWithValue("Exprience", exprience);
cmd.Parameters.AddWithValue("Type_Contract", type_Contract);
cmd.Parameters.AddWithValue("Posted_Date", posted_Date);
cmd.Parameters.AddWithValue("Salary", salary);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
}
这是 html 。
<%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="Joblist.aspx.cs" Inherits="StudentJobSite.Pages.Joblist" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" DataKeyNames="Job_ID" ForeColor="Black" GridLines="None"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:TemplateField HeaderText="Job ID">
<EditItemTemplate>
<asp:Label ID="lbljobID" runat="server" Text='<%# Eval("Job_ID")%>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Job_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("Title")%>' Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Title")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<EditItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" Text='<%# Bind("Location")%>' Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Location")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exprience">
<EditItemTemplate>
<asp:TextBox ID="txtExprience" runat="server" Text='<%# Bind("Exprience")%>' Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Exprience")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type Contract">
<EditItemTemplate>
<asp:TextBox ID="txtType_Contract" runat="server" Text='<%# Bind("Type_Contract")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Type_Contract")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted Date">
<EditItemTemplate>
<asp:TextBox ID="txtPosted_Date" runat="server" Text='<%# Bind("Posted_Date")%>' Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Posted_Date")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="txtSalary" runat="server" Text='<%# Bind("Salary")%>' Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Salary")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</div>
</asp:Content>
这是后面的代码。
using BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace StudentJobSite.Pages
{
public partial class Joblist : System.Web.UI.Page
{
JobHandler jobHandler = new JobHandler();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
GridView1.DataSource = jobHandler.GetJobList();
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Job_ID = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["Job_ID"].ToString());
jobHandler.job_delete(Job_ID);
refreshdata();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
refreshdata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Label lblID = GridView1.Rows[e.RowIndex].FindControl("Job_ID") as Label;
TextBox txtTitle = GridView1.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;
TextBox txtLocation = GridView1.Rows[e.RowIndex].FindControl("txtLocation") as TextBox;
TextBox txtExprience = GridView1.Rows[e.RowIndex].FindControl("txtExprience") as TextBox;
TextBox txtType_Contract = GridView1.Rows[e.RowIndex].FindControl("txtType_Contract") as TextBox;
TextBox txtPosted_Date = GridView1.Rows[e.RowIndex].FindControl("txtPosted_Date") as TextBox;
TextBox txtSalary = GridView1.Rows[e.RowIndex].FindControl("txtSalary") as TextBox;
int Job_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Job_ID"].ToString());
jobHandler.job_update(Job_ID, txtTitle.Text, txtLocation.Text, txtExprience.Text, txtType_Contract.Text, txtPosted_Date.Text, txtSalary.Text);
GridView1.EditIndex = -1;
refreshdata();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
refreshdata();
}
}
}
解决方案
我会说问题出在您的存储过程中:
update Job_Profile
set
@Title = @Title,
@Location = @Location,
@Exprience = @Exprience,
@Type_Contract = @Type_Contract,
@Posted_Date = @Posted_Date,
@Salary = @Salary
where
@Job_ID = @Job_ID
RETURN
所有这个查询,如果它在语法上是有效的,就是将输入参数更新为它们已经是相同的值
使用参数值更新表列的更新命令如下所示:
update Job_Profile
set
Title = @Title,
Location = @Location,
Exprience = @Exprience,
Type_Contract = @Type_Contract,
Posted_Date = @Posted_Date,
Salary = @Salary
where
Job_ID = @Job_ID
RETURN
= 左边的东西需要是列名。如果它们以 @ 开头,则它们不是列名。@ 用于变量名的开头
UPDATE tablename
SET columnName = @variableName
WHERE ...
当您遇到此类问题时,请将其分解为多个步骤 - 使用 SSMS 调用此存储过程。它要么起作用,要么不起作用。如果它有效,则问题出在 C# 代码中。如果不是,则问题出在 SP 代码中。这是基本调试;缩小问题的原因,以便确定问题发生的位置
推荐阅读
- algorithm - 如何找到最佳项目以匹配尽可能多的集合
- python - 在这种情况下如何使用切片表示法?
- winapi - 如何找到位于 UNC 路径上的驱动器的磁盘扇区大小
- javascript - 在javascript中的循环内部和外部有条件地链接承诺
- c++ - 如何在没有数组的情况下找到由给定数字的数字组成的最大数字
- javascript - Rails 6 无法导入纱线依赖项:未捕获错误:模块解析失败:意外字符 '#' (1:0)
- r - 如何管理 R 中的颜色对比度以标记图和表?
- django - 如果我们使用会话身份验证,如果他在前端更改信息,用户是否可以作为另一个用户闯入 django 服务器并执行操作?
- google-apps-script - Google Apps Script getText + replaceText 从 Google Docs 中的绘图中的文本框不可能?
- shopify - 我们可以使用 Shopify Storefront GraphQL API(非管理员)按 SKU 获取产品吗?