更改了Web表单详细信息而未保存到数据库 [英] Changed web form details not saving to database
问题描述
我下面的代码没有将详细信息从我的webform保存到数据库。用户使用已填充的数据修改文本框,然后单击保存按钮。单击该按钮时会触发以下代码。我相信它不起作用,因为我已经在外部代码中打开了一个连接,然后我建立另一个连接来运行代码内部括号中的过程
< pre lang =c#> protected void SaveChanges( object sender,EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
字符串 connectionString = ConfigurationManager.ConnectionStrings [ ApplicationServices跨度>]的ConnectionString。
conn = new SqlConnection(connectionString);
comm = new SqlCommand( SELECT *来自Business a,User_Acc c其中c.Username = @Username AND c.Business_ID = a.Business_ID,conn);
comm.Parameters.Add( @ Username,System.Data.SqlDbType.VarChar ).Value = Session [ User];
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
if (txtOldPassword.Text == reader [ 密码]。ToString())
{
SqlConnection connWrite;
connWrite = new SqlConnection(connectionString);
SqlCommand comm1 = new SqlCommand( exec SaveBusinessChanges @ Business_Name,@ Email,@ Telephone,@ Address_Line_1,@ Address_Line_2,@ Address_Line_3,@ Provence,@ County,@ Username,@ PasswordNew,connWrite);
comm1.Parameters.AddWithValue( @ Business_Name ,txtChangeBusinessName.Text);
comm1.Parameters.AddWithValue( @ Email,txtChangeBusinessEmail.Text);
comm1.Parameters.AddWithValue( @ Telephone,txtChangeBusinessTelephone.Text);
comm1.Parameters.AddWithValue( @ Address_Line_1,txtChangeBusinessAddress.Text);
comm1.Parameters.AddWithValue( @ Address_Line_2,txtChangeBusinessAddress2.Text);
comm1.Parameters.AddWithValue( @ Address_Line_3,txtChangeBusinessAddress3.Text);
comm1.Parameters.AddWithValue( @ Provence,DDLProvince.Text);
comm1.Parameters.AddWithValue( @ County,DDLCounty.Text);
comm1.Parameters.AddWithValue( @ Username,会话[ 用户]);
comm1.Parameters.AddWithValue( @ PasswordNew,txtChangeBusinessPassword.Text);
connWrite.Open();
comm1.ExecuteNonQuery();
connWrite.Close();
}
}
reader.Close();
conn.Close();
Response.Redirect( Welcome.aspx);
}
}
这是我的SQL Proc
ALTER PROCEDURE dbo.SaveBusinessChanges
( 50 ),
(
@ Business_Name varchar ( 50 ),
@ Address_Line_1 varchar ( 50 ),
@ Address_Line_2 varchar ( 50 ),
@ Address_Line_3 varchar ( 50 ),
@County varchar ( 50 ),
@ Provence varchar ( 50 ),
@ Telephone varchar ( 50 ),
@ Username < span class =code-keyword> varchar
@ PasswordNew varchar ( 50 ),
@电子邮件 varchar ( 50 )
)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @ Business_ID INT = 0
SET 跨度> @ Business_ID =( SELECT Business_ID FROM User_Acc WHERE 用户名= @用户名)
更新业务
SET Business_Name = @ Business_Name
,Address_Line_1 = @ Address_Line_1
,Address_Line_2 = @ Address_Line_2
,Address_Line_3 = < span class =code-sdkkeyword> @ Address_Line_3
,County = @ County
,普罗旺斯= @Provence
,Telephone = @ Telephone
WHERE Business_ID = @ Business_ID
更新 User_Acc
SET 电子邮件= @Email
,密码= @ PasswordNew
WHERE 用户名= @ Username
COMMIT
结束
检查Tadit的解决方案。另外,建议您查看以下链接
配置参数和参数数据类型 [ ^ ]
data-providers/cs-procedure with parameter [ ^ ]
尝试此问题如果问题让我知道
SqlConnection connWrite;
connWrite = new SqlConnection(connectionString);
SqlCommand comm1 = new SqlCommand(SaveBusinessChanges,connWrite);
comm1.CommandType = CommandType.StoredPr ocedure;
comm1.Parameters.AddWithValue(@ Business_Name,txtChangeBusinessName.Text);
comm1.Parameters.AddWithValue(@ Email,txtChangeBusinessEmail.Text);
comm1.Parameters.AddWithValue(@ Telephone,txtChangeBusinessTelephone.Text);
comm1.Parameters.AddWithValue(@ Address_Line_1,txtChangeBusinessAddress.Text);
comm1.Parameters.AddWithValue(@ Address_Line_2,txtChangeBusinessAddress2.Text);
comm1.Parameters.AddWithValue(@ Address_Line_3,txtChangeBusinessAddress3.Text);
comm1.Parameters.AddWithValue(@ Provence,DDLProvince.Text);
comm1.Parameters.AddWithValue(@ County,DDLCounty.Text);
comm1.Parameters.AddWithValue(@ Username,Session [User]);
comm1.Parameters.AddWithValue(@ PasswordNew,txtChangeBusinessPassword.Text);
connWrite.Open();
comm1.ExecuteNonQuery();
connWrite.Close();
快乐编码.. :)
更新你的代码如下......
SqlCommand comm1 = new SqlCommand( SaveBusinessChanges,connWrite);
comm1.CommandType = CommandType.StoredProcedure;
// 然后定义参数及其值。
The code I have below is not saving details from my webform to the database. The user modifies textboxes with already populated data and clicks the save button. The below code is fired when that button is clicked. I believe it is not working because I have a connection already open in the outer code and then I make another connection to run the procedure in the inner bracket of code
protected void SaveChanges(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * from Business a, User_Acc c Where c.Username = @Username AND c.Business_ID = a.Business_ID", conn);
comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
if (txtOldPassword.Text == reader["Password"].ToString())
{
SqlConnection connWrite;
connWrite = new SqlConnection(connectionString);
SqlCommand comm1 = new SqlCommand("exec SaveBusinessChanges @Business_Name, @Email, @Telephone, @Address_Line_1, @Address_Line_2, @Address_Line_3, @Provence, @County, @Username, @PasswordNew ",connWrite);
comm1.Parameters.AddWithValue("@Business_Name", txtChangeBusinessName.Text);
comm1.Parameters.AddWithValue("@Email", txtChangeBusinessEmail.Text);
comm1.Parameters.AddWithValue("@Telephone", txtChangeBusinessTelephone.Text);
comm1.Parameters.AddWithValue("@Address_Line_1", txtChangeBusinessAddress.Text);
comm1.Parameters.AddWithValue("@Address_Line_2", txtChangeBusinessAddress2.Text);
comm1.Parameters.AddWithValue("@Address_Line_3", txtChangeBusinessAddress3.Text);
comm1.Parameters.AddWithValue("@Provence", DDLProvince.Text);
comm1.Parameters.AddWithValue("@County", DDLCounty.Text);
comm1.Parameters.AddWithValue("@Username", Session["User"]);
comm1.Parameters.AddWithValue("@PasswordNew", txtChangeBusinessPassword.Text);
connWrite.Open();
comm1.ExecuteNonQuery();
connWrite.Close();
}
}
reader.Close();
conn.Close();
Response.Redirect("Welcome.aspx");
}
}
And here is my SQL Proc
ALTER PROCEDURE dbo.SaveBusinessChanges
(
@Business_Name varchar(50),
@Address_Line_1 varchar(50),
@Address_Line_2 varchar(50),
@Address_Line_3 varchar(50),
@County varchar(50),
@Provence varchar(50),
@Telephone varchar(50),
@Username varchar(50),
@PasswordNew varchar(50),
@Email varchar(50)
)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @Business_ID INT = 0
SET @Business_ID = (SELECT Business_ID FROM User_Acc WHERE Username = @Username)
UPDATE Business
SET Business_Name = @Business_Name
, Address_Line_1 = @Address_Line_1
, Address_Line_2 = @Address_Line_2
, Address_Line_3 = @Address_Line_3
, County = @County
, Provence = @Provence
, Telephone = @Telephone
WHERE Business_ID = @Business_ID
UPDATE User_Acc
SET Email = @Email
,Password = @PasswordNew
WHERE Username = @Username
COMMIT
END
Check Tadit's solution . Also , suggest you to have a look on below links
Configuring Parameters and Parameter Data Types[^]
data-providers/cs-procedure with parameter[^]
Try this if issue let me know
SqlConnection connWrite;
connWrite = new SqlConnection(connectionString);
SqlCommand comm1 = new SqlCommand("SaveBusinessChanges", connWrite);
comm1.CommandType = CommandType.StoredProcedure;
comm1.Parameters.AddWithValue("@Business_Name", txtChangeBusinessName.Text);
comm1.Parameters.AddWithValue("@Email", txtChangeBusinessEmail.Text);
comm1.Parameters.AddWithValue("@Telephone", txtChangeBusinessTelephone.Text);
comm1.Parameters.AddWithValue("@Address_Line_1", txtChangeBusinessAddress.Text);
comm1.Parameters.AddWithValue("@Address_Line_2", txtChangeBusinessAddress2.Text);
comm1.Parameters.AddWithValue("@Address_Line_3", txtChangeBusinessAddress3.Text);
comm1.Parameters.AddWithValue("@Provence", DDLProvince.Text);
comm1.Parameters.AddWithValue("@County", DDLCounty.Text);
comm1.Parameters.AddWithValue("@Username", Session["User"]);
comm1.Parameters.AddWithValue("@PasswordNew", txtChangeBusinessPassword.Text);
connWrite.Open();
comm1.ExecuteNonQuery();
connWrite.Close();
Happy coding.. :)
Update your code like below...
SqlCommand comm1 = new SqlCommand("SaveBusinessChanges", connWrite); comm1.CommandType = CommandType.StoredProcedure; // Then define the Parameters and their values.
这篇关于更改了Web表单详细信息而未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!