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 8010959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:53:28+00:00 2026-06-04T18:53:28+00:00

I have searched and found out this code to save data to database, connection

  • 0

I have searched and found out this code to save data to database, connection string is ok, and no exceptions/errors are thrown, but i dont know why this code is not saving data into my database..

    string query = "Insert Into BookConfiguration (BookNum, x_axis, y_axis, BookName) Values (@BookNum, @x_axis, @y_axis, @BookName)";
    string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\BookShelf.mdf;Integrated Security=True;User Instance=True";


    try
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {                   
                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = new SqlCommand(query, conn);
                da.InsertCommand.Parameters.Add("@BookNum", SqlDbType.Int).Value = quantity;
                da.InsertCommand.Parameters.Add("@x_axis", SqlDbType.Int).Value = x;
                da.InsertCommand.Parameters.Add("@y_axis", SqlDbType.Int).Value = y;
                da.InsertCommand.Parameters.Add("@BookName", SqlDbType.Text).Value = openFileDialog1.FileName;
            conn.Open();    

            da.InsertCommand.ExecuteNonQuery();

            conn.Close();
        }
    }

    catch (SqlException ex)
    {
        MessageBox.Show("Error Occured " + ex.Message);
    }

EDIT
i have now changed the code same problem

code:

        try
        {
            using (TransactionScope scope = new TransactionScope())
            {

                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.Add(new SqlParameter("@BookNum", quantity));
                        cmd.Parameters.Add(new SqlParameter("@x_axis", x));
                        cmd.Parameters.Add(new SqlParameter("@y_axis", y));
                        cmd.Parameters.Add(new SqlParameter("@BookName", openFileDialog1.FileName));
                        cmd.ExecuteNonQuery();
                    }
                    conn.Close();
        }

                scope.Complete();
            }
        }

i hope it is clear not vague…

coded in c# visual studio 2010 sql server 2008…

  • 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-04T18:53:30+00:00Added an answer on June 4, 2026 at 6:53 pm

    I can almost guarantee that because you are using the deprecated User Instances / AttachDbFileName features, that you are looking at a different copy of your database than your program is.

    Please see the answer from @marc_s here for how to proceed:

    https://stackoverflow.com/a/10740026/61305

    Essentially, stop using this useless feature. Attach your database to a real instance of SQL Server, then your database is fixed, and you can connect to the same copy of it as your program…

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

Sidebar

Related Questions

I have searched but have not found my answer. Disclaimer: I am brand new
i have searched Xcode controls but doesn't found anything that makes a GridView like
I have searched far and wide on the Internet but have not found anything
I have searched the documentation and tried various techniques but have't found a suitable
I have searched a bit on google, but didn't really found an answer to
I searched SO and found this question , but it's not quite what I'm
I searched for this and found Maudite's question about text editors but they were
i have a code like this, but giving error can not access protected member.
First, yes I have searched already and found this answer already: GWT JSNI -
I have searched for this answer and have not found it. I have music

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.