Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. access

access

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mylogics
    wrote on last edited by
    #1

    hiii i have a Product.db table in which i have columns: PID:Autonumber ProductID:indexed(yes (no duplicate)) Productname: ProductDescription: ProductPrice: VendorID: m using following code to insert data n update data on button click in my table:

    protected void Button4_Click(object sender, EventArgs e)
    {
    string str1 = "Select MAX(PID) From Product";
    OleDbCommand cmd = new OleDbCommand(str1, conn);
    OleDbDataReader dr = null;
    conn.Open();
    string max = cmd.ExecuteScalar().ToString();
    if (max != "")
    {
    max = "PID" + max;

        }
        else
        {
            max = "PID" + 1;
        }
        conn.Close();   
        string str = "Insert Into Product(ProductID,ProductName,ProductDescription,ProductPrice)Values('"+max+"','" + txtprdtname.Text + "','" + txtprdtdescrp.Text + "','" + txtprdtprice.Text + "')";
         cmd = new OleDbCommand(str, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        str = "Select \* From Product";
        cmd = new OleDbCommand(str, conn);
        OleDbDataReader dr1 = null;
        conn.Open();
        dr1 = cmd.ExecuteReader();
        DropDownList2.DataSource = dr1;
        DropDownList2.DataTextField = "ProductID";
        DropDownList2.DataValueField = "ProductID";
        DropDownList2.DataBind();
        conn.Close();
    
        string str3 = "Select Max(PID) From Product";
        cmd = new OleDbCommand(str3, conn);
        conn.Open();
        string max1 = cmd.ExecuteScalar().ToString();
        string max2 = "PID" + max1;
        conn.Close();
        string str2 = "Update Product Set ProductID='" + max2 + "' Where(PID=" + max1 + ")";
        cmd = new OleDbCommand(str2, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    
    }
    

    but when i execute it i get following error: The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again. Plz guide me where m i wrong....

    C 1 Reply Last reply
    0
    • M mylogics

      hiii i have a Product.db table in which i have columns: PID:Autonumber ProductID:indexed(yes (no duplicate)) Productname: ProductDescription: ProductPrice: VendorID: m using following code to insert data n update data on button click in my table:

      protected void Button4_Click(object sender, EventArgs e)
      {
      string str1 = "Select MAX(PID) From Product";
      OleDbCommand cmd = new OleDbCommand(str1, conn);
      OleDbDataReader dr = null;
      conn.Open();
      string max = cmd.ExecuteScalar().ToString();
      if (max != "")
      {
      max = "PID" + max;

          }
          else
          {
              max = "PID" + 1;
          }
          conn.Close();   
          string str = "Insert Into Product(ProductID,ProductName,ProductDescription,ProductPrice)Values('"+max+"','" + txtprdtname.Text + "','" + txtprdtdescrp.Text + "','" + txtprdtprice.Text + "')";
           cmd = new OleDbCommand(str, conn);
          conn.Open();
          cmd.ExecuteNonQuery();
          conn.Close();
          str = "Select \* From Product";
          cmd = new OleDbCommand(str, conn);
          OleDbDataReader dr1 = null;
          conn.Open();
          dr1 = cmd.ExecuteReader();
          DropDownList2.DataSource = dr1;
          DropDownList2.DataTextField = "ProductID";
          DropDownList2.DataValueField = "ProductID";
          DropDownList2.DataBind();
          conn.Close();
      
          string str3 = "Select Max(PID) From Product";
          cmd = new OleDbCommand(str3, conn);
          conn.Open();
          string max1 = cmd.ExecuteScalar().ToString();
          string max2 = "PID" + max1;
          conn.Close();
          string str2 = "Update Product Set ProductID='" + max2 + "' Where(PID=" + max1 + ")";
          cmd = new OleDbCommand(str2, conn);
          conn.Open();
          cmd.ExecuteNonQuery();
          conn.Close();
      
      }
      

      but when i execute it i get following error: The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again. Plz guide me where m i wrong....

      C Offline
      C Offline
      Coding C
      wrote on last edited by
      #2

      mylogics wrote:

      PID:Autonumber

      mylogics wrote:

      string str1 = "Select MAX(PID) From Product"; OleDbCommand cmd = new OleDbCommand(str1, conn); OleDbDataReader dr = null; conn.Open(); string max = cmd.ExecuteScalar().ToString(); if (max != "") { max = "PID" + max; } else { max = "PID" + 1; }

      You have set the PID:Autonumber in database? If yes then no need to include it in insert/update query. It will be automatically created by database. Just include the other fields in insert/update query except PID. HTH

      Coding C# ExciteTemplate

      M 1 Reply Last reply
      0
      • C Coding C

        mylogics wrote:

        PID:Autonumber

        mylogics wrote:

        string str1 = "Select MAX(PID) From Product"; OleDbCommand cmd = new OleDbCommand(str1, conn); OleDbDataReader dr = null; conn.Open(); string max = cmd.ExecuteScalar().ToString(); if (max != "") { max = "PID" + max; } else { max = "PID" + 1; }

        You have set the PID:Autonumber in database? If yes then no need to include it in insert/update query. It will be automatically created by database. Just include the other fields in insert/update query except PID. HTH

        Coding C# ExciteTemplate

        M Offline
        M Offline
        mylogics
        wrote on last edited by
        #3

        m not insertin PID in Product table but m inserting ProductID value and it is necessary as it is primary key and cannot be null.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups