Reputation: 97
I have an Ajax Editor in my page:
<cc1:Editor ID="Editor1" runat="server" width="600px"/>
What I want is to save the content from the Editor to my database.I tried this but it won't work:
SqlCommand cmd = new SqlCommand(
"INSERT INTO titlu (descriere) Values(@descriere)",con);
cmd.Parameters.AddWithValue("@descriere", Editor1.Content);
I am using C# and it's a ASP.Net web application.. Why can't I save my data?
Upvotes: 0
Views: 1068
Reputation: 2880
Assuming your code is something like this:
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();
}
then the line cmd.ExecuteNonQuery()
will either throw an exception or return the number of affected rows - in your case it should obviously be 1.
If an exception is not being thrown then the value is being entered into the database - make sure that Editor1.Content actually contains something when it is accessed here. Also make sure you are not swallowing the exception.
Upvotes: 1
Reputation: 3012
Your code doesn't show where you execute the SQL command. If execute the command what error code or exception do you get?
see this example:
// 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);
}
}
Upvotes: 0