Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:38:12+00:00 2026-06-03T21:38:12+00:00

I am developing shopping cart for my project and i’m having trouble on how

  • 0

I am developing shopping cart for my project and i’m having trouble on how to retrieve orderid and insert into another table.

I have two tables in sql server

Order
orderid (PK) | date| orderstatus| name| shippingaddress| contactnumber| totalprice| customerid|

Order_book
orderid (PK) | ISBN (PK)| quantity

I wanted to retrieve orderid from order table and insert it into order_book, whenever order is submitted to the database, orderid in order table is an identity int with increment of 1.

what are the best options to retrieve and insert at the same time from one table to another using c#?

Thanks.

codes::

protected void checkout_Click(object sender, EventArgs e)
{
    SQLInjection sql = new SQLInjection();
    String name;
    String address; 
    int contactnumber;
    if (newaddresspanel.Visible == true)
    {
       name = sql.SafeSqlLiteral(NameLabel.Text, 2);
       address = Address1.Text + " " + Address2.Text + "," + PostalCode.Text + "," + State.SelectedItem.Value;
       contactnumber =  int.Parse(Telephone1.SelectedItem.Value) + int.Parse(Telephone2.Text);
       insertDetails(name, address, contactnumber);
    }
    else if (defaultaddresspanel.Visible)
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database
        SqlCommand bindDetails = new SqlCommand("SELECT NAME, ADDRESS, TELEPHONE_NUMBER FROM CUSTOMER WHERE CUSTOMERID = @username", myConnection);

        bindDetails.Parameters.Add("@username", SqlDbType.VarChar);
        bindDetails.Parameters["@username"].Value = username;

        try
        {
            myConnection.Open();
            SqlDataReader reader = bindDetails.ExecuteReader();
            while (reader.Read())
            {
                name = reader["NAME"].ToString();
                address = reader["ADDRESS"].ToString();
                contactnumber = int.Parse(reader["TELEPHONE_NUMBER"].ToString());
                insertDetails(name, address, contactnumber);

                }
            reader.Dispose();
            myConnection.Close();
        }
        catch (SqlException se)
        {


        }
    }
    try
    {
        cart = (DataTable)HttpContext.Current.Session["Cart"];
        foreach (DataRow dr in cart.Rows)
        {
            int isbn = int.Parse(dr["ISBN"].ToString());
            int quantity = int.Parse(dr["quantity"].ToString());
            insertOrderbook(isbn, quantity);
        }


    }

    catch (Exception ae)
    {

        Response.Write(ae.Message);

    }

Insert orderbook : “Which failed”

protected void insertOrderbook(int isbn, int quantity)
{

    int orderid;
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database
    SqlCommand orderId = new SqlCommand("SELECT SCOPE_IDENTITY() FROM ORDERBOOK", myConnection);


    try
    {
        myConnection.Open();

        orderid = int.Parse(orderId.ExecuteScalar().ToString());
            SqlCommand insertOrderBook = new SqlCommand("INSERT INTO ORDERBOOK_BOOK (ISBN, ORDERID, QUANTITY) VALUES (@isbn, @orderid, @quantity)", myConnection);

            insertOrderBook.Parameters.Add("@isbn", SqlDbType.Int);
            insertOrderBook.Parameters["@isbn"].Value = isbn;

            insertOrderBook.Parameters.Add("@orderid", SqlDbType.Int);
            insertOrderBook.Parameters["@orderid"].Value = orderid;

            insertOrderBook.Parameters.Add("@quantity", SqlDbType.Int);
            insertOrderBook.Parameters["@quantity"].Value = quantity;

            insertOrderBook.ExecuteNonQuery();
            myConnection.Close();
        }
    catch (SqlException se)
    {
        Response.Write(se.Message);
    }
}

insert order details ::

protected void insertDetails(string name, string address, int contactnumber)
{
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database
    SqlCommand orderDetails = new SqlCommand("INSERT INTO ORDERBOOK (DATE, ORDERSTATUS, TOTALPRICE, SHIPPING_ADD, CUSTOMERID, NAME, CONTACTNUMBER) VALUES (@date, @orderstatus, @totalprice, @shippingadd, @username, @name, @contactnumber)", myConnection);
    orderDetails.Parameters.Add("@date", SqlDbType.DateTime);
    orderDetails.Parameters["@date"].Value = getTime();

    orderDetails.Parameters.Add("@orderstatus", SqlDbType.VarChar);
    orderDetails.Parameters["@orderstatus"].Value = "Order Processing";

    orderDetails.Parameters.Add("@totalprice", SqlDbType.Decimal);
    orderDetails.Parameters["@totalprice"].Value = totalPrice;

    orderDetails.Parameters.Add("@shippingadd", SqlDbType.VarChar);
    orderDetails.Parameters["@shippingadd"].Value = address;

    orderDetails.Parameters.Add("@name", SqlDbType.VarChar);
    orderDetails.Parameters["@name"].Value = name;

    orderDetails.Parameters.Add("@contactnumber", SqlDbType.Int);
    orderDetails.Parameters["@contactnumber"].Value = contactnumber;

    orderDetails.Parameters.Add("@username", SqlDbType.VarChar);
    orderDetails.Parameters["@username"].Value = username;

    try {
        myConnection.Open();
        orderDetails.ExecuteNonQuery();
        myConnection.Close();

    }
    catch ( SqlException se)
    {


    }
}

Error: input string was not in correct format

I tried to debug it, somehow, orderid shows 0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:38:14+00:00Added an answer on June 3, 2026 at 9:38 pm

    You are doing this select

    SELECT SCOPE_IDENTITY() FROM ORDERBOOK
    

    What you should do is add an output parameter (let’s say, @orderBookId) to your order book insert SP, and on it, right after the insert, you should do

    SELECT @orderBookId = SCOPE_IDENTITY()
    

    notice there is no FROM clause, and be sure to add the parameter on your C# code, as an output parameter.

    Then, when inserting the details, simply add @orderBookId as an input parameter and that’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are developing a shopping cart application. For every single user session i have
I am currently developing a shopping cart solution and I have a field, which
I'm developing a lightweight shopping cart but have become stumped with products that have
I am developing a custom shopping cart for which existing customers will need to
Developing a project of mine I realize I have a need for some level
I'm developing a shopping cart that uses for example Captcha to validate that a
I'm developing a website (with a shopping cart) on top of Codeigniter, and want
I am developing online Shopping Cart system. Now I need to work on adding
I am thinking of developing a shopping cart for Wordpress. I know there are
PHP newbie here. I'm currently developing a shopping cart web site using PHP and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.