数据库中的值未更新

时间:2012-08-07 16:09:11

标签: c# sql-server

我有注册表格,我在第一页后插入必填字段,基于用户ID我在下面的第二页更新一些细节是我的代码

ALTER procedure [dbo].[Insertreg]

( @id int output,@FirstName varchar (50),@LastName varchar(50) ,@Dob datetime,
        @Gender varchar(20) ,@MobileNo nchar(10) ,@Country varchar(50) ,
    @State varchar (50),@EmailId varchar (50),@Password nchar (15)

)
as
begin
insert into Profile_Master(FirstName,LastName,Dob,Gender,MobileNo,Country,State,EmailId,Password)
 values
(@FirstName,@LastName,@Dob,@Gender,@MobileNo,@Country,@State,@EmailId,@Password)
set @id=SCOPE_IDENTITY()
end


    ALTER procedure [dbo].[sp_update]

(@id int output,@FormFiledBy varchar (50),@MaritalStatus varchar (50),@Height varchar (50),
@Religion varchar (50),@Caste varchar (100),
 @MotherTongue varchar(50),@Education varchar (100),@Occupation varchar(50),
 @CountryofResidence varchar (50),@EducationDetails varchar (100),@AnnualIncome varchar(50),
@CountryOfBirth varchar(50),@BirthPlace varchar(50),@TimeOfBirth nchar(10),@StarSign varchar (100),
@Gothram varchar (50),@Rassi varchar(50),@HavinChildren varchar(10),@PhysicalStatus varchar (100))

as
begin

update Profile_Master
 set FormFiledBy=@FormFiledBy,MaritalStatus=@MaritalStatus,Height=@Height,
Religion=@Religion,Caste=@Caste,MotherTongue=@MotherTongue,
Education=@Education,Occupation=@Occupation,CountryofResidence=@CountryofResidence,EducationDetails=@EducationDetails,
AnnualIncome=@AnnualIncome,CountryOfBirth=@CountryOfBirth,BirthPlace=@BirthPlace,TimeOfBirth=@TimeOfBirth,
StarSign=@StarSign,Gothram=@Gothram,Rassi=@Rassi,HavinChildren=@HavinChildren,PhysicalStatus=@PhysicalStatus

where UserId=@id
end

protected void Button1_Click(object sender, EventArgs e)
    {
        // Get User ID from DAL
        int chk = 0;
        if (Session["EmailID"] != null)
        {
            emailID = Session["EmailID"].ToString();
        }
        ProfileMasterBLL prfbll =new ProfileMasterBLL();
        prfbll.EmailID = emailID;
        string userid = ProfileMasterDAL.GetUserIdByEmailID(emailID);

        // Capture remaining parameters
        if (userid != null)
        {

            prfbll.FormFiledBy = RadioButtonList1.SelectedItem.Text;
            prfbll.MaritalStatus = RadioButtonList2.SelectedItem.Text;
            prfbll.Height = DropDownList1.SelectedItem.Text;
            prfbll.PhysicalStatus = RadioButtonList4.SelectedItem.Text;
            prfbll.Religion = DropDownList2.SelectedItem.Text;
            prfbll.Caste = DropDownList3.SelectedItem.Text;
            prfbll.MotherTongue = DropDownList4.SelectedItem.Text;
            prfbll.Education = DropDownList5.SelectedItem.Text;
            prfbll.Occupation = DropDownList6.SelectedItem.Text;
            prfbll.CountryofResidence = DropDownList8.SelectedItem.Text;
            prfbll.EducationDetails = TextBox3.Text;
            prfbll.AnnualIncome = DropDownList7.SelectedItem.Text;
            prfbll.CountryOfBirth = DropDownList9.SelectedItem.Text;
            prfbll.BirthPlace = TextBox6.Text;
            prfbll.TimeOfBirth = TextBox7.Text;
            prfbll.StarSign = DropDownList10.SelectedItem.Text;
            prfbll.Gothram = TextBox8.Text;
            prfbll.Rassi = DropDownList12.Text;

            if (RadioButtonList2.SelectedItem.Text != "Single")
            {
                prfbll.HavinChildren = RadioButtonList7.SelectedItem.Text;
            }
            else
                {
                    RadioButtonList7.SelectedItem.Text = null;
                }
            }


            try
            {
                chk = ProfileMasterDAL.update(prfbll);
                if (chk >= 0)
                {
                    Response.Write("<script language='javascript'>alert('details updated');</script>");
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }


public static int update(ProfileMasterBLL profileMasterBLL)
    {
        SqlParameter uid;
        SqlConnection conn = Generic.DBConnection.OpenConnection();
        try
        {
            SqlCommand cmdd = new SqlCommand("sp_update", conn);
            cmdd.CommandType = CommandType.StoredProcedure;
            cmdd.Parameters.AddWithValue("@FormFiledBy", profileMasterBLL.FormFiledBy);
            cmdd.Parameters.AddWithValue("@MaritalStatus", profileMasterBLL.MaritalStatus);
            cmdd.Parameters.AddWithValue("@Height", profileMasterBLL.Height);
            cmdd.Parameters.AddWithValue("@Religion", profileMasterBLL.Religion);
            cmdd.Parameters.AddWithValue("@Caste", profileMasterBLL.Caste);
            cmdd.Parameters.AddWithValue("@Education", profileMasterBLL.Education);
            cmdd.Parameters.AddWithValue("@Occupation", profileMasterBLL.Occupation);
            cmdd.Parameters.AddWithValue("@CountryofResidence", profileMasterBLL.CountryofResidence);
            cmdd.Parameters.AddWithValue("@EducationDetails", profileMasterBLL.EducationDetails);
            cmdd.Parameters.AddWithValue("@CountryOfBirth", profileMasterBLL.CountryOfBirth);
            cmdd.Parameters.AddWithValue("@BirthPlace", profileMasterBLL.BirthPlace);
            cmdd.Parameters.AddWithValue("@TimeOfBirth", profileMasterBLL.TimeOfBirth);
            cmdd.Parameters.AddWithValue("@StarSign", profileMasterBLL.StarSign);
            cmdd.Parameters.AddWithValue("@Gothram", profileMasterBLL.Gothram);
            cmdd.Parameters.AddWithValue("@Rassi", profileMasterBLL.Rassi);
            cmdd.Parameters.AddWithValue("@HavinChildren", profileMasterBLL.HavinChildren);
            cmdd.Parameters.AddWithValue("@PhysicalStatus", profileMasterBLL.PhysicalStatus);
            cmdd.Parameters.AddWithValue("@MotherTongue", profileMasterBLL.MotherTongue);
            cmdd.Parameters.AddWithValue("@AnnualIncome", profileMasterBLL.AnnualIncome);
            uid = cmdd.Parameters.Add("@id", System.Data.SqlDbType.Int);
            uid.Direction = System.Data.ParameterDirection.Output;
            return cmdd.ExecuteNonQuery();


        }
        catch (Exception ex)
        {

            throw ex;
        }

    }

但问题是数据库没有在DB中更新,任何人都可以告诉我有什么问题吗?

public static int Insert(ProfileMasterBLL profileMasterBLL)
    {
        // bool flag = false;
        SqlParameter pid;
        SqlConnection con = Generic.DBConnection.OpenConnection();

        try
        {

            SqlCommand cmd1 = new SqlCommand("Insertreg", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@FirstName", profileMasterBLL.FirstName);
            cmd1.Parameters.AddWithValue("@LastName", profileMasterBLL.LastName);
            cmd1.Parameters.AddWithValue("@Dob", profileMasterBLL.Dob);
            cmd1.Parameters.AddWithValue("@Gender", profileMasterBLL.Gender);
            cmd1.Parameters.AddWithValue("@MobileNo", profileMasterBLL.MobileNo);
            cmd1.Parameters.AddWithValue("@Country", profileMasterBLL.Country);
            cmd1.Parameters.AddWithValue("@State", profileMasterBLL.State);
            cmd1.Parameters.AddWithValue("@EmailId", profileMasterBLL.EmailID);
            cmd1.Parameters.AddWithValue("@Password", profileMasterBLL.Password);
           pid = cmd1.Parameters.Add("@id", System.Data.SqlDbType.Int);
            pid.Direction = System.Data.ParameterDirection.Output;
            return cmd1.ExecuteNonQuery();

2 个答案:

答案 0 :(得分:0)

  1. 从更新sp的@id参数
  2. 中删除输出参数
  3. 在c#的更新方法中设置@id的值

答案 1 :(得分:0)

您的更新程序有望更新

where UserId=@id

但你没有传递身份证明。

如果这个班级

ProfileMasterBLL 

有一个用户ID,您可以在更新其余字段时指定它

  if (userid != null)
        {
            prfbll.UserId = userid; //Or whatever it's called
            prfbll.FormFiledBy = RadioButtonList1.SelectedItem.Text;

如果不是,则需要更新签名

public static int update(ProfileMasterBLL profileMasterBLL, string userId)

然后再传递用户ID。

这需要更改参数和存储过程。

SP不应该使用OUT参数(因此不应该以这种方式传递。

    ALTER procedure [dbo].[sp_update]

(@id int ,@FormFiledBy varchar (50),@MaritalStatus varchar (50),@Height varchar (50),
@Religion varchar (50),@Caste varchar (100),
 @MotherTongue varchar(50),@Education varchar (100),@Occupation varchar(50),
 @CountryofResidence varchar (50),@EducationDetails varchar (100),@AnnualIncome varchar(50),
@CountryOfBirth varchar(50),@BirthPlace varchar(50),@TimeOfBirth nchar(10),@StarSign varchar (100),
@Gothram varchar (50),@Rassi varchar(50),@HavinChildren varchar(10),@PhysicalStatus varchar (100))

as
begin

update Profile_Master
 set FormFiledBy=@FormFiledBy,MaritalStatus=@MaritalStatus,Height=@Height,
Religion=@Religion,Caste=@Caste,MotherTongue=@MotherTongue,
Education=@Education,Occupation=@Occupation,CountryofResidence=@CountryofResidence,EducationDetails=@EducationDetails,
AnnualIncome=@AnnualIncome,CountryOfBirth=@CountryOfBirth,BirthPlace=@BirthPlace,TimeOfBirth=@TimeOfBirth,
StarSign=@StarSign,Gothram=@Gothram,Rassi=@Rassi,HavinChildren=@HavinChildren,PhysicalStatus=@PhysicalStatus

where UserId=@id
end

并且应该添加参数

    SqlParameter uid;
    SqlConnection conn = Generic.DBConnection.OpenConnection();
    try
    {
        SqlCommand cmdd = new SqlCommand("sp_update", conn);
        cmdd.CommandType = CommandType.StoredProcedure;
        cmdd.Parameters.AddWithValue("@Id", userId ); //Or profileMasterBLL.UserId or whatever
        cmdd.Parameters.AddWithValue("@FormFiledBy", profileMasterBLL.FormFiledBy);
        cmdd.Parameters.AddWithValue("@MaritalStatus", profileMasterBLL.MaritalStatus);
        cmdd.Parameters.AddWithValue("@Height", profileMasterBLL.Height);
        cmdd.Parameters.AddWithValue("@Religion", profileMasterBLL.Religion);
        cmdd.Parameters.AddWithValue("@Caste", profileMasterBLL.Caste);
        cmdd.Parameters.AddWithValue("@Education", profileMasterBLL.Education);
        cmdd.Parameters.AddWithValue("@Occupation", profileMasterBLL.Occupation);
        cmdd.Parameters.AddWithValue("@CountryofResidence", profileMasterBLL.CountryofResidence);
        cmdd.Parameters.AddWithValue("@EducationDetails", profileMasterBLL.EducationDetails);
        cmdd.Parameters.AddWithValue("@CountryOfBirth", profileMasterBLL.CountryOfBirth);
        cmdd.Parameters.AddWithValue("@BirthPlace", profileMasterBLL.BirthPlace);
        cmdd.Parameters.AddWithValue("@TimeOfBirth", profileMasterBLL.TimeOfBirth);
        cmdd.Parameters.AddWithValue("@StarSign", profileMasterBLL.StarSign);
        cmdd.Parameters.AddWithValue("@Gothram", profileMasterBLL.Gothram);
        cmdd.Parameters.AddWithValue("@Rassi", profileMasterBLL.Rassi);
        cmdd.Parameters.AddWithValue("@HavinChildren", profileMasterBLL.HavinChildren);
        cmdd.Parameters.AddWithValue("@PhysicalStatus", profileMasterBLL.PhysicalStatus);
        cmdd.Parameters.AddWithValue("@MotherTongue", profileMasterBLL.MotherTongue);
        cmdd.Parameters.AddWithValue("@AnnualIncome", profileMasterBLL.AnnualIncome);

同时删除这些             uid = cmdd.Parameters.Add(“@ id”,System.Data.SqlDbType.Int);             uid.Direction = System.Data.ParameterDirection.Output;             return cmdd.ExecuteNonQuery();