Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 15th, 2005, 01:38 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>








Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert an image in a (*.MDB) database thomaz C# 1 May 3rd, 2007 07:02 AM
INSERT, UPDATE, DELETE dagad ASP.NET 2.0 Professional 0 September 27th, 2006 01:22 AM
How can i Insert , delete data from datagrid ?? jay4ever_2000 ASP.NET 1.0 and 1.1 Professional 1 February 17th, 2006 03:35 PM
Views-Insert/update/delete lily611 SQL Language 2 November 29th, 2004 03:41 AM
Update, insert and delete Trigger khautinh SQL Server 2000 2 September 17th, 2003 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.