<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %><script runat="server">    protected void Button1_Click(object sender, EventArgs e)
    {        SqlDataSource1.InsertParameters["tilte"].DefaultValue = TextBox1.Text;
        SqlDataSource1.InsertParameters["content"].DefaultValue = TextBox2.Text;
        SqlDataSource1.InsertParameters["atime"].DefaultValue = DateTime.Now.ToString();
        SqlDataSource1.Insert();
    }
</script><asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
    <div class="content">   <table cellspacing="1" 
        style="width: 100%; border-style: solid; border-width: 1px">
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="标题:"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="491px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="内容:"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server" Height="321px" TextMode="MultiLine" 
                    Width="606px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <table class="style1">
                    <tr>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
                        </td>
                        <td>
                            <asp:Button ID="Button2" runat="server" Text="返回" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:conn1 %>" 
            SelectCommand="SELECT * FROM [article]"
            InsertCommand="INSERT INTO [article] (title,content,atime) values (@title,@content,@atime)">
            
            <InsertParameters>
                <asp:Parameter Name="title" />
                <asp:Parameter Name="content" />
                <asp:Parameter Name="atime" />
            </InsertParameters>            </asp:SqlDataSource>
       
</div>
</asp:Content>
求解,谢谢各位!在线等

解决方案 »

  1.   

    提示:未将对象引用设置到对象的实例
    SqlDataSource1.InsertParameters["tilte"].DefaultValue = TextBox1.Text;
      

  2.   

    try
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:conn1 %>" 
                SelectCommand="SELECT * FROM [article]"
                InsertCommand="INSERT INTO [article] (title,content,atime) values (@title,@content,@atime)">
                
                <InsertParameters>
                    <asp:Parameter Name="title"  formfield="TextBox1" />
                    <asp:Parameter Name="content" formfield="TextBox2" />
                    <asp:Parameter Name="atime" />
                </InsertParameters>            </asp:SqlDataSource>
    <script runat="server">    protected void Button1_Click(object sender, EventArgs e)
        {
            SqlDataSource1.InsertParameters["atime"].DefaultValue = DateTime.Now.ToString();
            SqlDataSource1.Insert();
        }
    </script>
      

  3.   

    <%@Page  Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
    private void InsertShipper (object source, EventArgs e) {
      SqlDataSource1.Insert();
    }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
        <title>ASP.NET Example</title>
    </head>
    <body>
        <form id="form1" runat="server">      <asp:dropdownlist
            id="DropDownList1"
            runat="server"
            datasourceid="SqlDataSource1"
            datatextfield="CompanyName"
            datavaluefield="ShipperID" /><!-- Security Note: The SqlDataSource uses a FormParameter,
         Security Note: which does not perform validation of input from the client.
         Security Note: To validate the value of the FormParameter, handle the Inserting event. -->      <asp:sqldatasource
            id="SqlDataSource1"
            runat="server"
            connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
            selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
            insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
              <insertparameters>
                <asp:formparameter name="CoName" formfield="CompanyNameBox" />
                <asp:formparameter name="Phone"  formfield="PhoneBox" />
              </insertparameters>
          </asp:sqldatasource>      <br /><asp:textbox
               id="CompanyNameBox"
               runat="server" />      <asp:RequiredFieldValidator
            id="RequiredFieldValidator1"
            runat="server"
            ControlToValidate="CompanyNameBox"
            Display="Static"
            ErrorMessage="Please enter a company name." />      <br /><asp:textbox
               id="PhoneBox"
               runat="server" />      <asp:RequiredFieldValidator
            id="RequiredFieldValidator2"
            runat="server"
            ControlToValidate="PhoneBox"
            Display="Static"
            ErrorMessage="Please enter a phone number." />      <br /><asp:button
               id="Button1"
               runat="server"
               text="Insert New Shipper"
               onclick="InsertShipper" />    </form>
      </body>
    </html>
      

  4.   

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
      ConnectionString="<%$ ConnectionStrings:DBaseConnectionString %>"  
    InsertCommand="INSERT INTO Tb(a, b) VALUES (@a, @b)" 
      SelectCommand="SELECT tb.* FROM tb">
      <InsertParameters>
    <asp:Parameter Name="a" />
    <asp:Parameter Name="b" />
      </InsertParameters>
      </asp:SqlDataSource>
    对a,b赋值并保存的代码:
      SqlDataSource1.InsertParameters["a"].DefaultValue = "值1";  SqlDataSource1.InsertParameters["b"].DefaultValue = "值2";
      SqlDataSource1.Insert();
    看看scott教程SqlDataSource 操作
      

  5.   

    看看你的字段是否正确,或者看看是不是页面回传了加if(!Ispostback)试试看。
      

  6.   

    看看你两个地方的字段名!拼错啦!!
    SqlDataSource1.InsertParameters["tilte"].DefaultValue = TextBox1.Text;<asp:Parameter Name="title" />