Сетка не смогла обновить данные - PullRequest
0 голосов
/ 17 апреля 2020

Я занимаюсь разработкой приложений уровня. Я использую вид сетки данных для выполнения операций обновления. Я использую процедуру сохранения для обновления данных, но проблема заключается в том, что когда я нажимаю изменить ссылку и пытаюсь обновить, она не обновляет данные, а также я получаю сообщение об ошибке ..

Ссылка на объект отсутствует установить экземпляр объекта.

Вот процедура хранения

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 bool UpdateJob(Job job)
        {
            SqlParameter[] parameters = new SqlParameter[]
                {

                new SqlParameter("@Job_ID", job.Job_ID),
                new SqlParameter("@Title", job.Title),
                new SqlParameter("@Location", job.Location),
                new SqlParameter("@Exprience", job.Exprience),
                new SqlParameter("@Type_Contract", job.Type_Contract),
                new SqlParameter("@Posted_Date", job.Posted_Date),
                new SqlParameter("@Salary", job.Salary)
            };

            return SqlDBHelper.ExecuteNonQuery("UpdateJob", CommandType.StoredProcedure, parameters);
        }

Вот HTML Код.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Conformations.aspx.cs" Inherits="StudentJobPortalPresentationsLayer.Pages.Conformations" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:Label ID="Label1" runat="server" Text="Conformations"> Applications is received </asp:Label>
   <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"

                        BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableModelValidation="True"

                        ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="false"  AutoGenerateEditButton="True" OnRowCancelingEdit="RowCancelingEdit"

                        OnRowDeleting="RowDeleting"    OnRowEditing="RowEditing" OnRowUpdating="RowUpdating">

                        <AlternatingRowStyle BackColor="White" />

                        <FooterStyle BackColor="#CCCC99" />

                        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

                        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

                        <RowStyle BackColor="#F7F7DE" />

                        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <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>

                    <asp:HyperLinkField Text="Delete" DataNavigateUrlFields="Job_ID" DataNavigateUrlFormatString="~/Pages/DeleteJob.aspx?id={0}" />

                </Columns>
            </asp:GridView>

            <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

    <br />
</asp:Content>

Вот код позади ..

 using StudentJobPortal_BLL_;
using StudentJobPortal_DAL_.JobObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StudentJobPortalPresentationsLayer.Pages
{
    // JobHandler empHandler = null;

    public partial class Conformations : System.Web.UI.Page
    {
        JobHandler jobHandler = new JobHandler();
        protected void Page_Load(object sender, EventArgs e)
        {
            jobHandler = new JobHandler();
            if (IsPostBack == false)
            {
                BindData();
            }
        }


        private void BindData()
        {
            GridView1.DataSource = jobHandler.GetJobList();
            GridView1.DataBind();
        }
        protected void RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;

            BindData();
        }

        protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            BindData();

        }

        protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


            try

            {
                TextBox txtId = GridView1.Rows[e.RowIndex].FindControl("txtId") as TextBox;

                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;


                JobHandler empHandler = new JobHandler();
                Job job = new Job();

                try

                {
                    job.Job_ID = Convert.ToInt32(txtId.Text.Trim());
                    job.Title = txtTitle.Text;
                    job.Location = txtLocation.Text;
                    job.Exprience = txtExprience.Text;
                    job.Type_Contract = txtType_Contract.Text;
                    job.Posted_Date = txtPosted_Date.Text;
                    job.Salary = txtSalary.Text;

                    if (jobHandler.UpdateJob(job) == true)
                    {
                        lblMessage.Text = "Record Updated Successfully";
                    }
                    else
                    {
                        lblMessage.Text = "Failed to Update record";
                    }

                }

                catch (Exception ee)

                {

                    lblMessage.Text = ee.Message.ToString();

                }


                GridView1.EditIndex = -1;

                BindData();

            }

            catch (Exception ex)

            {

                Response.Write(ex.Message);

            }

        }
    }
}

Вот снимок экрана .. enter image description here

...