首页 > 解决方案 > 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>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </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();
        }
    }
}

标签: c#asp.netgridview

解决方案


我会说问题出在您的存储过程中:

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 代码中。这是基本调试;缩小问题的原因,以便确定问题发生的位置


推荐阅读