Cannot INSERT or DELETE into/from cdstore.mdb
Folks, how is everyone doing? I hope all is well, and enjoying spring. It's cold out here in Chicago area. I have run into the problem where I cannot either INSERT or DELETE into the MS Access database. I have not worked with MS Access Database or ASP.NET(C#)before, therefore do not know what's the deal. I tried to reset permissions to "users", still no luck.
When I try to DELETE it gives the error message:
Exception Details: System.Data.OleDb.OleDbException: Could not delete from specified tables.
When I try to Insert it gives the error message:
Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
The "cdstore.mdb" database is very simple, has only one table "albums" which contains the following fields:
year, artist, album, genre, price, condition, id(AutoNumber).
Folks, please help me out if you don't mind the time. I appreciate it a lot!
Here is my code for the showalbums.aspx file:
<%@ Page Language="C#" Debug="true" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
string albumId = Request.QueryString["albumId"];
if(albumId == ""){
Response.Write("Error");
return;
}
//if (!IsPostBack) DataBind();
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myDataReader;
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../Final/cdstore.mdb"));
myConnection.Open();
myCommand = new OleDbCommand("Select * from albums WHERE id =" + albumId, myConnection);
myDataReader = myCommand.ExecuteReader();
//if (myDataReader.Read()){
//lblartist.Text = (myDataReader["artist"]).ToString();
//lblalbum.Text = (myDataReader["album"]).ToString();
//lblyear.Text = (myDataReader["year"]).ToString();
//lblprice.Text = (myDataReader["price"]).ToString();
//}
//else {
//Response.Write("No matches found");
//}
//while (myDataReader.Read()){
//Response.Write("<a href=showalbum.aspx?albumId=" +myDataReader["id"] + ">" + myDataReader["genre"] + "</a><br>");
//}
dataGrid.DataSource = myDataReader;
dataGrid.DataBind();
myDataReader.Close();
myConnection.Close();
}
protected void Add_Record(Object Src, EventArgs E)
{
OleDbConnection myConnection;
OleDbCommand myCommand;
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../Final/cdstore.mdb"));
myConnection.Open();
myCommand = new OleDbCommand("INSERT INTO albums (year, artist, album, genre, price, condition, id) VALUES ('"+ TextBox1.Text
+ "'," + "'" + TextBox2.Text + "'," + "'"+ TextBox3.Text + "'," + "'" + TextBox4.Text + "'," + "'" + TextBox5.Text + "'," + "'" + TextBox6.Text + "')", myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
protected void Delete_Record(Object Src, EventArgs E)
{
string albumId = Request.QueryString["albumId"];
if(albumId == ""){
Response.Write("Error");
return;
}
OleDbConnection myConnection;
OleDbCommand myCommand;
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../Final/cdstore.mdb"));
myConnection.Open();
myCommand = new OleDbCommand("DELETE FROM albums WHERE id =" + albumId, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
//c.Open();
//ad1 = new OleDbDataAdapter("select id, album from albums", c);
//DataSet ds = new DataSet();
//ad1.Fill(ds, "albomz");
//DataTable a = ds.Tables["companies"];
//DataRow dr = a.NewRow();
//dr["name"] = txtAdd.Text;
//a.Rows.Add(dr);
//OleDbCommandBuilder cb = new OleDbCommandBuilder(ad1);
//ad1.InsertCommand = cb.GetInsertCommand();
//ad1.UpdateCommand = cb.GetUpdateCommand();
//ad1.DeleteCommand = cb.GetDeleteCommand();
//ad1.Update(ds, "companies");
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form runat="server">
<center>
<asp:DataGrid ID="dataGrid" runat="server"
cellpadding="10"
Font-Name="arial"
Font-Size="24pt"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-ForeColor="blue"
/>
</center>
<center>
<tr>
<td><asp:button id="delete" runat = "server" onClick="Delete_Record" Text="Delete"/>
<asp:button id="add" runat = "server" onClick="Add_Record" Text="Add"/>
</td>
</tr>
<p>
<asp:TextBox id="TextBox1" runat="server" Text="enter album year here"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox2" runat="server" Text="enter artist here"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox3" runat="server" Text="enter album here"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox4" runat="server" Text="enter genre here"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox5" runat="server" Text="enter price here"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox6" runat="server" Text="enter condition here"></asp:TextBox>
</p>
</center>
</form>
</body>
</html>
|