C# 未在数据库中更新值
我有注册表格,我在第一页插入必填字段,然后根据用户ID更新下面第二页中的一些详细信息是我的代码C# 未在数据库中更新值,c#,sql-server,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 varc
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;
}
}
您的更新过程需要更新
where UserId=@id
但是你没有传递身份证
如果这个班
ProfileMasterBLL
具有用户id,您可以在更新其余字段时分配该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;
返回cmdd.ExecuteNonQuery();它不会抛出错误,但数据库中的值不会得到更新。请使用sql profiler并检查查询。我的系统中没有安装sql profiler…userid是一个标识列,它是自动递增的。我在UIID的第一页调用该方法。该代码是否正常工作并在数据库中创建必要的记录?它正常工作一段时间之后,我在字符串userid=ProfileMasterDAL.GetUserIdByEmailID(profileMasterBLL.EmailID)处得到空值;
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);