ASP.NET中的ajax编辑器

时间:2012-03-04 16:23:40

标签: c# asp.net ajax

我的页面中有一个Ajax编辑器:

<cc1:Editor ID="Editor1" runat="server" width="600px"/>

我想要的是将内容从编辑器保存到我的数据库。我试过这个但是它不起作用:

SqlCommand cmd = new SqlCommand(
    "INSERT INTO titlu (descriere) Values(@descriere)",con);

cmd.Parameters.AddWithValue("@descriere", Editor1.Content);

我正在使用C#,它是一个ASP.Net网络应用程序..为什么我不能保存我的数据?

2 个答案:

答案 0 :(得分:1)

假设你的代码是这样的:

using (SqlConnection con = new ...)
{
    SqlCommand cmd = new SqlCommand(
                    "INSERT INTO titlu (descriere) Values(@descriere)",con);
    cmd.Parameters.AddWithValue("@descriere", Editor1.Content);
    con.Open();
    int affectedRows = cmd.ExecuteNonQuery();
}

然后行cmd.ExecuteNonQuery()将抛出异常或返回受影响的行数 - 在您的情况下,它显然应为1.

如果没有抛出异常,则将值输入数据库 - 确保Editor1.Content在此处访问时实际包含某些内容。还要确保你没有吞下这个例外。

答案 1 :(得分:0)

您的代码未显示执行SQL命令的位置。如果执行命令是什么 你得到错误代码或异常吗?

见这个例子:

 // Given command text and connection string, asynchronously execute
    // the specified command against the connection. For this example,
    // the code displays an indicator as it is working, verifying the 
    // asynchronous behavior. 
    using (SqlConnection connection = 
               new SqlConnection(connectionString))
    {
        try
        {
            int count = 0;
            SqlCommand command = new SqlCommand(commandText, connection);
            connection.Open();

            IAsyncResult result = command.BeginExecuteNonQuery();
            while (!result.IsCompleted)
            {
                Console.WriteLine("Waiting ({0})", count++);
                // Wait for 1/10 second, so the counter
                // does not consume all available resources 
                // on the main thread.
                System.Threading.Thread.Sleep(100);
            }
            Console.WriteLine("Command complete. Affected {0} rows.", 
                command.EndExecuteNonQuery(result));
        }
        catch (SqlException ex)
        {
            Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine("Error: {0}", ex.Message);
        }
        catch (Exception ex)
        {
            // You might want to pass these errors
            // back out to the caller.
            Console.WriteLine("Error: {0}", ex.Message);
        }
    }