protected void btnSubmit_Click(object sender, EventArgs e)
{
connectionString = ConfigurationManager.ConnectionStrings["LeaveMangementSystemCS"].ConnectionString;
conn = new SqlConnection(connectionString);
string sql = "UPDATE LeaveType SET LeaveType.Type=@Type, LeaveType.Description=@Description, LeaveType.NumOfDays=@NumOfDays, LeaveCategory.Category=@Category FROM LeaveType INNER JOIN LeaveCategory on LeaveType.LeaveCategoryId = LeaveCategory.Id WHERE LeaveType.Id=@id";
try
{
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Type", tbType.Text);
cmd.Parameters.AddWithValue("@Description", tbDescription.Text);
cmd.Parameters.AddWithValue("@NumOfDays",tbNumOfDays.Text);
cmd.Parameters.AddWithValue("@Category", ddlLeaveCategory.Text);
cmd.Parameters.AddWithValue("@id", lblIdOut.Text);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
lblOutput.Text = " Updated successfully.";
}
}
catch (Exception ex)
{
lblOutput.Text = "Error Message : " + ex.Message;
}
finally
{
if (conn != null)
conn.Close();
}
}
我的 SQL 查询有一个错误:
The multi-part identifier "LeaveCategory.Category" could not be bound.
我曾尝试使用 leavetype 作为 a 和 leavecategory 作为 b 但仍然是这个错误。
最佳答案
您不能在一个语句中更新多个表!查看您的 sql
变量:
UPDATE LeaveType
SET LeaveType.Type=@Type
, LeaveType.Description=@Description
, LeaveType.NumOfDays=@NumOfDays
, LeaveCategory.Category=@Category
FROM LeaveType
INNER JOIN LeaveCategory on LeaveType.LeaveCategoryId = LeaveCategory.Id
WHERE LeaveType.Id=@id";
在这里,您尝试更新表 LeaveType
和表 LeaveCategory
中的一列
请参阅this回答
关于C# & SQL Server 查询错误 : The multi-part identifier cannot be bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38324132/