问题描述
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;
}
}
推荐答案
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',这是未提供的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!