本文介绍了数据不是最新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Timetracker.Web.Account
{
    public partial class EmployeeMgt : System.Web.UI.Page
    {
        string conSting = System.Configuration.ConfigurationManager.ConnectionStrings["timetrackerConnection"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {

            BindGridView();
        }

        private void BindGridView()
        {

            SqlConnection con = new SqlConnection(conSting);
            SqlCommand cmd = new SqlCommand(" SELECT  USR.Id,USR.EmailId,"
                                                                + "USR.Password, "
                                                                + "USR.FirstName, "
                                                                + "USR.LastName,"
                                                                + "USR.Gender,"
                                                                + "USR.UserRole,"
                                                                + "USR.DateOfBirth,"
                                                                + "USR.DateOfJoin,"
                                                                + "USR.ContactNo,"
                                                                + "ADRS.AddressLine1,"
                                                                + "ADRS.AddressLine2,"
                                                                + "ADRS.Zip_code,"
                                                                + "ADRS.City,"
                                                                + "ADRS.State,"
                                                                + "ADRS.Country"
                                                                + " FROM [dbo].[UserTabel] USR"
                                                                + " JOIN [dbo].[Address] ADRS ON ADRS.Id = USR.AddressId", con);

            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;
            GridView1.DataBind();
            //GridView1.Columns[0].Visible = false;
            con.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddEmployee.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGridView();
        }
        protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGridView();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            TextBox EmailId = (TextBox)GridView1.Rows[e.RowIndex].FindControl("EmaiId");
            TextBox Password = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Password");
            TextBox FirstName=(TextBox)GridView1.Rows[e.RowIndex].FindControl("Firstname");
            TextBox LastName=(TextBox)GridView1.Rows[e.RowIndex].FindControl("LastName");
            RadioButton GenderMale =(RadioButton)GridView1.Rows[e.RowIndex].FindControl("rbtnmale");
            RadioButton GenderFemale = (RadioButton)GridView1.Rows[e.RowIndex].FindControl("rbtnfemale");

            //RadioButton Gender = (RadioButton)GridView1.Rows[e.RowIndex].FindControl("Gender");
            if (GenderMale.Text == "Male")
            {
                GenderMale.Checked = true;
            }
            if (GenderFemale.Text == "Female")
            {
                GenderFemale.Checked = true;
             }
            RadioButton UserRole = (RadioButton)GridView1.Rows[e.RowIndex].FindControl("rbtnmanager");
            if(UserRole.Text=="Manager")
            {
                UserRole.Checked=true;
            }
            else if(UserRole.Text=="Employee")
            {
                UserRole.Checked=true;
            }
            TextBox ContactNo = (TextBox)GridView1.Rows[e.RowIndex].FindControl("ContacNo");
            //TextBox AddressLine1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("AddressLine1");
            //TextBox AddressLine2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("AddressLine2");
            //TextBox Zip_code = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Zip_code");
            //TextBox City = (TextBox)GridView1.Rows[e.RowIndex].FindControl("City");
            //TextBox State = (TextBox)GridView1.Rows[e.RowIndex].FindControl("State");
            //TextBox Country = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Country");
            SqlConnection con = new SqlConnection(conSting);
            con.Open();
            string UpdateQuery1 = "UPDATE UserTabel set EmailId=@EmailId,Password=@Password,FirstName=@FirstName,LastName=@LastName,Gender=@Gender,UserRole=@UserRole,ContactNo=@ContactNo where Id=@Id";
            SqlCommand cmd = new SqlCommand(UpdateQuery1,con);
            cmd.Parameters.AddWithValue("@EmailId", EmailId.Text);
            cmd.Parameters.AddWithValue("@Password", Password.Text);
            cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", LastName.Text);
            cmd.Parameters.AddWithValue("@Gender",GenderMale.Text);
            cmd.Parameters.AddWithValue("@Gender", GenderFemale.Text);
            cmd.Parameters.AddWithValue("@UserRole", UserRole.Text);
            cmd.Parameters.AddWithValue("@ContactNo", ContactNo.Text);
            cmd.ExecuteNonQuery();

            con.Close();









        }

    }
}

推荐答案

string UpdateQuery1 = "UPDATE UserTabel set EmailId=@EmailId,Password=@Password,FirstName=@FirstName,LastName=@LastName,Gender=@Gender,UserRole=@UserRole,ContactNo=@ContactNo where Id=@Id";
            SqlCommand cmd = new SqlCommand(UpdateQuery1,con);
            cmd.Parameters.AddWithValue("@EmailId", EmailId.Text);
            cmd.Parameters.AddWithValue("@Password", Password.Text);
            cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", LastName.Text);
            cmd.Parameters.AddWithValue("@Gender",GenderMale.Text);
            cmd.Parameters.AddWithValue("@Gender", GenderFemale.Text);
            cmd.Parameters.AddWithValue("@UserRole", UserRole.Text);
            cmd.Parameters.AddWithValue("@ContactNo", ContactNo.Text);
            cmd.ExecuteNonQuery();





你没有为@Id参数提供一个值,所以它没有找到正确记录更新。



You're not supplying a value for the @Id parameter so it's not finding the right record to update.


这篇关于数据不是最新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:22