datagridview обновляет все записи вместо одной записи - PullRequest
0 голосов
/ 20 апреля 2020

Я использую n-уровневые приложения для отображения данных в элементе управления 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

Вот код ado. net.

 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.

<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>

Ниже приведен код.

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();
        }
    }
}

Вот снимок экрана.

enter image description here

...