我该如何修复此代码 [英] How do I fix this code
本文介绍了我该如何修复此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Configuration;
使用System.Data.SqlClient;
namespace WebApplication1.Resource_Type
{
public partial class编辑:System.Web.UI.Page
{
int id = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
string connectionString = null;
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
SqlDataReader dr = null;
connectionString = ConfigurationManager.ConnectionStrings [FacilityCS]。ConnectionString;
conn = new SqlConnection(connectionString);
string sql =SELECT * from ResourceType;
尝试
{
cmd = new SqlCommand(sql,conn);
conn.Open();
dr = cmd.ExecuteReader();
}
catch(例外情况)
{
lblOutput.Text =错误消息:+ ex.Message;
}
最后
{
if(conn!= null)
conn.Close();
}
if(Request.Params [Id]!= null)
{
id = Convert.ToInt32(Request.Params [Id]] );
sql =SELECT * from ResourceType WHERE id = @ Id;
尝试
{
cmd = new SqlCommand(sql,conn);
cmd.Parameters.AddWithValue(@ Id,id);
conn.Open();
dr = cmd.ExecuteReader();
dr.Read();
tbType.Text = dr [Type]。ToString();
tbDesc.Text = dr [Description]。ToString();
}
catch(例外情况)
{
lblOutput.Text =错误消息:+ ex.Message;
}
最后
{
if(conn!= null)
conn.Close();
}
}
其他
{
lblOutput.Text =错误。无法从数据库中检索ID。 ;
}
}
}
protected void btnSubmit_Click(object sender,EventArgs e)
{
connectionString = ConfigurationManager.ConnectionStrings [FacilityCS ] .ConnectionString;
conn = new SqlConnection(connectionString);
string sql =UPDATE ResourceType SET Type = @ Type,Description = @ Description;
sql + =WHERE Id = @ Id;
尝试
{
cmd = new SqlCommand(sql,conn);
cmd.Parameters.AddWithValue(@ Type,tbType.Text);
cmd.Parameters.AddWithValue(@ Description,tbDesc.Text);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows> 0)
{
lblOutput.Text =记录更新成功;
}
}
catch(例外情况)
{
lblOutput.Text =错误消息:+ ex.Message;
}
最后
{
if(conn!= null)
conn.Close();
}
}
}
}
这是我的代码。基本上这是编辑数据库的数据,但是当我点击按钮时,错误给了我
Quote:错误消息:必须声明标量变量 @DescriptionWHERE。
解决方案
在where之前添加一个空格:
< span class =code-keyword> string sql = UPDATE ResourceType SET Type = @ Type,Description = @描述跨度>;
sql + = WHERE Id = @ Id;
你忘了在 WHERE 之前放一个空格。
string sql = UPDATE ResourceType SET Type = @ Type,Description = @ Description 跨度>;
sql + = WHERE Id = @ Id;
它应该是 -
string sql = UPDATE ResourceType SET Type = @ Type,Description = @ Description;
sql + = WHERE Id = @ Id;
希望,它有帮助:)
更新
您已经忘记在 btnSubmit_Click 中为参数@Id提供值。
cmd .Parameters.AddWithValue( @ Type,tbType.Text);
cmd.Parameters.AddWithValue( @ Description,tbDesc.Text);
cmd.Parameters.AddWithValue( @ Id,Id); // ID是id的值
你可以这样写;
string sql = @UPDATE ResourceType SET Type = @ Type,Description = @ Description WHERE Id = @ Id;
cmd.Parameters.AddWithValue( @ Type,tbType.Text );
cmd.Parameters.AddWithValue( @ Description,tbDesc.Text);
cmd.Parameters.AddWithValue( @ Id,id );
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; namespace WebApplication1.Resource_Type { public partial class Edit : System.Web.UI.Page { int id = 0; SqlConnection conn = null; SqlCommand cmd = null; string connectionString = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlDataReader dr = null; connectionString = ConfigurationManager.ConnectionStrings["FacilityCS"].ConnectionString; conn = new SqlConnection(connectionString); string sql = "SELECT * from ResourceType"; try { cmd = new SqlCommand(sql, conn); conn.Open(); dr = cmd.ExecuteReader(); } catch (Exception ex) { lblOutput.Text = "Error Message: " + ex.Message; } finally { if (conn != null) conn.Close(); } if (Request.Params["Id"] != null) { id = Convert.ToInt32(Request.Params["Id"]); sql = "SELECT * from ResourceType WHERE id=@Id"; try { cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Id", id); conn.Open(); dr = cmd.ExecuteReader(); dr.Read(); tbType.Text = dr["Type"].ToString(); tbDesc.Text = dr["Description"].ToString(); } catch (Exception ex) { lblOutput.Text = "Error Message: " + ex.Message; } finally { if (conn != null) conn.Close(); } } else { lblOutput.Text = "Error. There is no ID to retrieve from the DB."; } } } protected void btnSubmit_Click(object sender, EventArgs e) { connectionString = ConfigurationManager.ConnectionStrings["FacilityCS"].ConnectionString; conn = new SqlConnection(connectionString); string sql = "UPDATE ResourceType SET Type=@Type, Description=@Description"; sql += "WHERE Id=@Id"; try { cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Type", tbType.Text); cmd.Parameters.AddWithValue("@Description", tbDesc.Text); conn.Open(); int rows = cmd.ExecuteNonQuery(); if(rows > 0) { lblOutput.Text = "Record updated successfully"; } } catch(Exception ex) { lblOutput.Text = "Error Message: " + ex.Message; } finally { if (conn != null) conn.Close(); } } } }
Hi,this is my code. Basically this is so edit a database's data but when i click the button the error gives me this
Quote:Error Message: Must declare the scalar variable "@DescriptionWHERE".
解决方案
Add a space before "where" :
string sql = "UPDATE ResourceType SET Type=@Type, Description=@Description"; sql += " WHERE Id=@Id";
You forgot a to put a blank space before WHERE.
string sql = "UPDATE ResourceType SET Type=@Type, Description=@Description"; sql += "WHERE Id=@Id";
It should be -
string sql = "UPDATE ResourceType SET Type=@Type, Description=@Description"; sql += " WHERE Id=@Id";
Hope, it helps :)
Update
You have aslo forgotten to provide value for parameter @Id in btnSubmit_Click.
cmd.Parameters.AddWithValue("@Type", tbType.Text); cmd.Parameters.AddWithValue("@Description", tbDesc.Text); cmd.Parameters.AddWithValue("@Id", Id); //Id is the value for id
you can write this way;
string sql = @"UPDATE ResourceType SET Type=@Type, Description=@Description WHERE Id=@Id";
cmd.Parameters.AddWithValue("@Type", tbType.Text); cmd.Parameters.AddWithValue("@Description", tbDesc.Text); cmd.Parameters.AddWithValue("@Id", id);
这篇关于我该如何修复此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文