im new to c# and i m trying to insert into a table so i made this custom code:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
string insertString = "INSERT INTO Users (Name) VALUES ('n')";
// Define the connection string we will use. This one is specific for Microsoft
// Access
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("magazine.mdb");
bool connectionOpen = false;
OleDbConnection connection = null;
try
{
// Define a new connection to the database and open the connection
connection = new OleDbConnection(connectionString);
connection.Open();
connectionOpen = true;
}
catch (Exception ex1)
{
// Connection failed. Put out an appropriate error message
%> <h2>Unable to Connect to the Database</h2>
<p> could not be added to the publishers table since I was unable to
connect to the books database.</p>
<% }
if (connectionOpen)
{
try
{
// Create a new command for this connection.
OleDbCommand cmd = new OleDbCommand(insertString, connection);
// Execute the insert
cmd.ExecuteNonQuery();
%>
<h2>Insert Successful</h2>
<p> has been added to the publishers table in the books database.</p>
<% }
catch (Exception ex2)
{
%>
<h2>Insert Failed</h2>
<p> could not be added to the publishers table in the books database.</p>
<p>The error that occured was: <%= ex2.Message %></p>
<% }
finally
{
// Close the connection
connection.Close();
}
}
%>
</body>
</html>
and i get the following error
Insert Failed
could not be added to the publishers table in the books database.
The error that occured was: Operation must use an updateable query.
what i m doing wrong. also if a have a field in my DB that is ID autoincrement i have to do something special?
Thanx for ur help
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
string insertString = "INSERT INTO Users (Name) VALUES ('n')";
// Define the connection string we will use. This one is specific for Microsoft
// Access
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("magazine.mdb");
bool connectionOpen = false;
OleDbConnection connection = null;
try
{
// Define a new connection to the database and open the connection
connection = new OleDbConnection(connectionString);
connection.Open();
connectionOpen = true;
}
catch (Exception ex1)
{
// Connection failed. Put out an appropriate error message
%> <h2>Unable to Connect to the Database</h2>
<p> could not be added to the publishers table since I was unable to
connect to the books database.</p>
<% }
if (connectionOpen)
{
try
{
// Create a new command for this connection.
OleDbCommand cmd = new OleDbCommand(insertString, connection);
// Execute the insert
cmd.ExecuteNonQuery();
%>
<h2>Insert Successful</h2>
<p> has been added to the publishers table in the books database.</p>
<% }
catch (Exception ex2)
{
%>
<h2>Insert Failed</h2>
<p> could not be added to the publishers table in the books database.</p>
<p>The error that occured was: <%= ex2.Message %></p>
<% }
finally
{
// Close the connection
connection.Close();
}
}
%>
</body>
</html>
and i get the following error
Insert Failed
could not be added to the publishers table in the books database.
The error that occured was: Operation must use an updateable query.
what i m doing wrong. also if a have a field in my DB that is ID autoincrement i have to do something special?
Thanx for ur help