'/'应用程序中的服务器错误。参数化查询'(@ id int,@ name nvarchar(4000))从nm中选择*,其中id = @id,'期望参数'@name',这是未提供的。 [英] Server error in '/' application. The parameterized query '(@id int, @name nvarchar(4000))select * from nm where id=@id and ' expects the parameter '@name', which was not supplied.

查看:126
本文介绍了'/'应用程序中的服务器错误。参数化查询'(@ id int,@ name nvarchar(4000))从nm中选择*,其中id = @id,'期望参数'@name',这是未提供的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



name       

< asp:TextBox ID =txtnmrunat =server >

< asp:Button ID =Button1runat =serverText =submit/>




< asp:Label ID =lblmsgrunat =server>

< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataKeyNames = idOnPageIndexChanging =GridView1_PageIndexChangingOnRowCancelingEdit =GridView1_RowCancelingEditOnRowDeleting =GridView1_RowDeletingOnRowEditing =GridView1_RowEditingOnRowUpdating =GridView1_RowUpdating>

< columns>

< asp:TemplateField HeaderText =idSortExpression =id>

< edititemtemplate>

< asp:TextBox ID =txtidrunat =服务器Text ='<%#Bind(id)%>'>



< itemtemplate>

< asp:Label ID =Label1runat =serverText ='<%#Bind(id)%>'>





< asp:TemplateField HeaderText =nameSortExpression =name>

< edititemtemplate>

< asp:TextBox ID =txtnmrunat =serverText ='<%#Bind(name)%>'>



< itemtemplate>

< asp:Label ID =Label2runat =serverText ='<%#Bind(name)%>'> ;










name       
<asp:TextBox ID="txtnm" runat="server">
<asp:Button ID="Button1" runat="server" Text="submit" />


<asp:Label ID="lblmsg" runat="server" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<columns>
<asp:TemplateField HeaderText="id" SortExpression="id">
<edititemtemplate>
<asp:TextBox ID="txtid" runat="server" Text='<%# Bind("id") %>'>

<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'>


<asp:TemplateField HeaderText="name" SortExpression="name">
<edititemtemplate>
<asp:TextBox ID="txtnm" runat="server" Text='<%# Bind("name") %>'>

<itemtemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'>










.cs文件

public partial class _Default:System.Web.UI.Page

{

dal d = new dal();

bal b = new bal();

protecte d void Page_Load(object sender,EventArgs e)

{

if(!Page.IsPostBack)

{

fillgrid();

}

}



private void fillgrid()

{

GridView1.DataSource = b.display(d);

GridView1.DataBind();

}



protected void Button1_Click(object sender,EventArgs e)

{

d.name = txtnm.Text;

int i = b.insert(d);

if(i == 0)

{

lblmsg.Text = 不插入;

}

其他

{

lblmsg.Text =insert;

}

fillgrid();



}



我的尝试:



公共舱bal

{

dal d = new dal();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [db] .ConnectionString);

SqlCommand cmd;

public bal()

{

//

// TODO:在这里添加构造函数逻辑

//

}



公共DataTable显示(dal d)

{

cmd =新SqlCommand(select * from nm where id = @ id和name = @ name,con);

cmd.Parameters.AddWithValue(@ id,d.id);

cmd.Parameters.AddWithValue (@ name,d.name);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

da.Fill(dt);

返回dt;

}



public int insert(dal d)

{

cmd = new SqlCommand(insert into nm where(name)values(@name),con);

cmd.Parameters.AddWithValue(@ name ,d.name);

con.Open();

int i = cmd.ExecuteNonQuery();

con.Close( );

返回i;



}

}




.cs file
public partial class _Default : System.Web.UI.Page
{
dal d = new dal();
bal b = new bal();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fillgrid();
}
}

private void fillgrid()
{
GridView1.DataSource = b.display(d);
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
d.name = txtnm.Text;
int i = b.insert(d);
if (i == 0)
{
lblmsg.Text = "not insert";
}
else
{
lblmsg.Text = "insert";
}
fillgrid();

}

What I have tried:

public class bal
{
dal d = new dal();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlCommand cmd;
public bal()
{
//
// TODO: Add constructor logic here
//
}

public DataTable display(dal d)
{
cmd = new SqlCommand("select * from nm where id=@id and name=@name", con);
cmd.Parameters.AddWithValue("@id", d.id);
cmd.Parameters.AddWithValue("@name", d.name);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}

public int insert(dal d)
{
cmd = new SqlCommand("insert into nm where (name)values(@name)", con);
cmd.Parameters.AddWithValue("@name", d.name);
con.Open();
int i= cmd.ExecuteNonQuery();
con.Close();
return i;

}
}

推荐答案

只是将值传递给参数可能是不够的:如果传递的值是.NET null 那么它计数为未提供给很多情况。



使用调试器(或添加日志代码,如果这只发生在生产中)来检查传递给SQL的实际值,并添加尝试... catch 块,以便确定发生的位置。
Just passing a value to a parameter may not be enough: if the value you pass is a .NET null then it "counts" as not supplied for many cases.

Use the debugger (or add logging code if this only occurs in production) to check the actual values you pass to SQL, and add try ... catch blocks so that you can be sure of where the occurs.


不要使用Parameters.AddWithValue。创建一个参数对象并将其添加到SQLCommand对象。当你这样做时,你需要指定类型。 Parameters.AddWithValue将尝试推断类型,这可能是导致错误的原因。



例如:



Don't use Parameters.AddWithValue. Create a parameter object and add that to the SQLCommand object. And when you do you need to specify the type. Parameters.AddWithValue will try to infer the type and this might be what is causing your error.

For example:

qlParameter param = new SqlParameter
            {
                ParameterName = "@parent_id",
                DbType = DbType.Int32,
                Value = parentID
            };



然后




and then

cmd.Parameters.Add(param);


这篇关于'/'应用程序中的服务器错误。参数化查询'(@ id int,@ name nvarchar(4000))从nm中选择*,其中id = @id,'期望参数'@name',这是未提供的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆