..........................................................................
App_Code/db.cs类中的代码如下:public static void Insert(string Ntitle, string Ncontent)
    {
        SqlConnection con = new SqlConnection(ConnString());
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("NewInsertCommand", con);
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;        SqlParameter para = new SqlParameter("@Ntitle",Ntitle);
        SqlParameter para1 = new SqlParameter("@Ncontent",Ncontent);
        sda.SelectCommand.Parameters.Add(para);
        sda.SelectCommand.Parameters.Add(para1);
        
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {        }
        finally
        {
            con.Close();
            sda.Dispose();
            cmd.Dispose();
        }
    }
..........................................................................
按钮页代码如下:
protected void Button1_Click(object sender, EventArgs e)
    {
        db.Insert(this.TextBox1.Text,this.TextBox2.Text);
    }
..........................................................................
存储过程代码:
ALTER PROCEDURE dbo.NewInsertCommand
@Ntitle varchar(50),
@Ncontent ntext
AS
SET NOCOUNT OFF
    insert into [zNotice] ([Ntitle],[Ncontent]) values(@Ntitle,@Ncontent);
..........................................................................错误提示:Insert方法没有采用2个参数的重载,我是菜鸟,请高手帮忙,最好附改正后的代码,先谢谢了。

解决方案 »

  1.   

    帖一下db.cs和button1所在页面的完整代码看看
      

  2.   

    App_Code/db.cs类中的代码如下:
    ....................................................................................
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;/// <summary>
    /// db 的摘要说明
    /// </summary>
    public class db
    {
        private SqlConnection con;
        private SqlCommand cmd;
        private SqlDataAdapter sda;
        private DataSet ds;
        private SqlDataReader sdr;//定义5个私有变量 public db()
    {//初始化全部私有变量
            con = new SqlConnection(ConnString());
            cmd = new SqlCommand();
            sda = new SqlDataAdapter();
            ds = new DataSet();
    }
        public static string ConnString()
        {//返回数据库连接
            return ConfigurationManager.ConnectionStrings["con"].ConnectionString ;
        }
        public void clear()
        {//判断数据组件是否活动或者存在,否则释放或者断开            清理垃圾
            if (con.State != ConnectionState.Closed)
            {
                con.Close();
                con.Dispose();
            }
            if (cmd != null)
                cmd.Dispose();
            if (sda != null)
                sda.Dispose();
            if (ds != null)
                ds.Dispose();
            if (sdr != null)
            {
                sdr.Dispose();
            }
        }
        public static DataTable dt()
        {//返回一个数据表
            SqlConnection con = new SqlConnection(ConnString());
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand("NewSelectCommand", con);//给出即将使用到的存储过程的名称,并给出数据库连接
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;//指出它使用的是存储过程
            DataSet ds = new DataSet();
            sda.Fill(ds,"Notice");//用fill方法填充到dataset的notice字段
            return ds.Tables["Notice"];
            con.Close();
            sda.Dispose();
            ds.Clear();
        }    public static void Insert(string Ntitle, string Ncontent)
        {
            SqlConnection con = new SqlConnection(ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand("NewInsertCommand", con);
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;        SqlParameter para = new SqlParameter("@Ntitle",Ntitle);
            SqlParameter para1 = new SqlParameter("@Ncontent",Ncontent);
            sda.SelectCommand.Parameters.Add(para);
            sda.SelectCommand.Parameters.Add(para1);        cmd.Connection = con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {        }
            finally
            {
                con.Close();
                sda.Dispose();
                cmd.Dispose();
            }
        }
        public void Delete(Int32 Nid)
        {
            cmd.CommandText = "NewDeleteCommand";
            cmd.CommandType = CommandType.StoredProcedure;        cmd.Parameters.AddWithValue("@Original_Nid", Nid);
            cmd.Connection = con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            {        }
            finally
            {
                con.Close();
            }
        }
    }
    ...................................................................................
    AddNotice.aspx页面代码如下:using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;public partial class zphtManagement_AddNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            db.Insert(this.TextBox1.Text,this.TextBox2.Text);
        }
    }
    ....................................................................................
    AddNotice.aspx页面代码如下:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddNotice.aspx.cs" Inherits="zphtManagement_AddNotice" %><%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <link href="style.css" type="text/css" rel="stylesheet" media="all" />
    </head>
    <body>
        <form id="form1" runat="server">
            <cc1:scriptmanager id="ScriptManager1" runat="server" enablepartialrendering="True"><ErrorTemplate>
    <div style="padding: 12px; width: 400px; height: 140px; border: #000000 1px solid; background-color: white; text-align: left">
        An error has occurred:<br />
        <span runat="server" id="errorMessageLabel"></span>
        <br />
        <br />
        <input runat="server" id="okButton" type="button" value="OK" />
    </div>
    </ErrorTemplate>
    </cc1:scriptmanager>
        <div>
            <table cellpadding="0" cellspacing="0" style="width:814px; margin-top:10px;" class="b_solid">
                <tr>
                    <td colspan="2" style="background:#39867B; height: 30px; text-indent:20px; font-size:12px; color:#ffffff; font-weight:bold;" class="td_solid">
                        添加公告
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px; height:30px; background:#E1F4EE; text-align:right; font-size:12px; font-weight:bold;" class="td1_solid">
                        公告标题:
                    </td>
                    <td style="height:30px; background:#E1F4EE;" class="td1_solid"> 
                        &nbsp;<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" Height="18px" Width="620px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="height:420px; background:#E1F4EE; text-align:right; font-size:12px; font-weight:bold;" class="td1_solid">
                        公告内容:
                    </td>
                    <td style="background:#E1F4EE; margin-bottom: 0px; margin-left: 0px;" class="td1_solid">
                        &nbsp;<asp:TextBox ID="TextBox2" runat="server" CssClass="textbox" Height="400px" Width="620px" TextMode="MultiLine"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="height:30px; background:#E1F4EE; text-align:center;" class="td1_solid">
                        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label></td>
                </tr>
                <tr>
                    <td colspan="2" style="height:40px; background:#E1F4EE; text-align:center;" class="td1_solid">
                        &nbsp;<cc1:updatepanel id="UpdatePanel1" runat="server"><ContentTemplate>
    <asp:Button id="Button1" runat="server" Text="确定添加" OnClick="Button1_Click"></asp:Button>
    </ContentTemplate>
    </cc1:updatepanel></td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>
      

  3.   

    从语法的角度来看db.cs和db.Insert(this.TextBox1.Text,this.TextBox2.Text);好像没有什么问题,再把完整的错误信息贴上来看一下
      

  4.   

    问题已解决:
    ...................................................................
    将:
    cmd.Connection = con;
    cmd.ExecuteNonQuery();换成:
    sda.SelectCommand.Connection = con;
    sda.SelectCommand.ExecuteNonQuery();
    就OK了
    ...................................................................
    同样问题的朋友注意了。在问个问题,如何对上边存储过程进行判断是否插入成功呢?
    请热心人帮忙帖上代码,谢谢了。解决这个问题结贴