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

  • Home
  • SEARCH
  • 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 6693339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:58:18+00:00 2026-05-26T05:58:18+00:00

Guys I searched around like hell but nothing could help me so I think

  • 0

Guys I searched around like hell but nothing could help me so I think it’s time to ask. Before I write to problem, I need to say that I need it’s solution asap because it’s a project that I have to give tomorrow and I stuck on the same subject since ages and still losing time.

OK here it is;

I need to add a book to a library system, at first phase I add the standard book features which has only “one value” like (name, page number, publishing time, publisherID etc) but as wanted by me book MAY HAVE MULTIPLE WRITERS AND CATEGORIES which killed me and still I can’t resolve. I tried to add book to it’s (books) table then with the information i got from that i did an other insert op. to (bookWriters) table. While I check it, compiler does everything in order without error but when I check table from SQL Server there is nothing.

Here is what I tried to do;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace Project_
{
    public partial class addBook: Form
    {
        public addBook()
        {
            InitializeComponent();
        }

        public main refForm;

        int chosenWritersNumber; //how many writers have selected on listbox
        int[] writers= { }; // an array list that i keep writerIDs that comes from class
        int ind = 0;

        int insertedBookID; // to catch latest added book's ID
        int chosenWriterID; // writer that will be added

        private void bookAddingPreps()
        {
            chosenWritersNumber = lstWriters.SelectedItems.Count;

            Array.Resize<int>(ref writers, chosenWritersNumber );

            for (int i = 0; i < chosenWritersNumber ; i++)
            {
                writers[i] = ((X_Writers)lstWriters.SelectedItems[i]).XWriterID;
            }
        }

        private void addMainBookInfos()
        {
            SqlConnection con = new SqlConnection(Conn.Activated);

            SqlCommand com = new SqlCommand("AddBook", con);

            com.CommandType = CommandType.StoredProcedure;

            com.Parameters.AddWithValue("@BookISBN", txtISBN.Text);            

            con.Close();
        }

        private void catchAddedBookID()
        {
            SqlConnection con = new SqlConnection(Conn.Activated);
            SqlCommand com = new SqlCommand("catchBookID", con);

            com.CommandType = CommandType.StoredProcedure;

            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    insertedBookID = dr.GetInt32(0);
                }
            }
            dr.Close();
            con.Close();
        }

        private void addWritersOfTheBook()
        {
            chosenWriterID = writers[ind];

            SqlConnection con = new SqlConnection(Conn.Activated);

            SqlCommand com = new SqlCommand("addBookWriters", con);
            com.CommandType = CommandType.StoredProcedure;

            com.Parameters.AddWithValue("@BookID", insertedBookID);
            com.Parameters.AddWithValue("@WriterID", chosenWriterID);
            con.Close();
        }

I call these methods on click of a button. You see also stored procedure names but as I checked they all correct, there must be a mistake in this page that I still cant see but if it’s needed I can add what procedures writes but they all tested and seems working.

So as i said, when i do this, as ind = 0, a writer should have been added, break point shows everything is ok and compiler doesnt show any errors but when I check sql server table, its empty.

Written in C# with using Visual Studio 2010 Ultimate and SQL Server 2008 Dev.

Thanks

  • 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-05-26T05:58:18+00:00Added an answer on May 26, 2026 at 5:58 am

    You forget to execute your SqlCommand’s. Make a call to command.ExecuteNonReader(); to execute it without expecting any results. see: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

    Apart form that, dont forget to dispose the resources acquired in your methods. Something like:

    private void addMainBookInfos() 
    { 
        using (SqlConnection con = new SqlConnection(Conn.Activated))
        using (SqlCommand com = new SqlCommand("AddBook", con))
        {
            com.CommandType = CommandType.StoredProcedure; 
            com.Parameters.AddWithValue("@BookISBN", txtISBN.Text);             
    
            com.ExecuteNonQuery()
    
            // close can be omitted since you are already using the 'using' statement which automatically closes the connection
            con.Close(); 
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before you guys ask, yes I've searched online for the answer, but everything I
i've searched for days but cant find an answer, perhaps you guys can help.
I have another one for you guys. I've searched around but I can't figure
Hey Guys, I've searched around for this question but couldn't come up with anything.
Hey guys, before you guys say anything I have searched and can not find
Guys, I've got this problem that I searched almost everywhere (maybe I don't know
hey guys i searched a lot but there is a little topics discussed about
Hey, guys i searched a lot but cant get my code working. I am
Ok guys, this is my first thread, and I have searched online but with
Hello guys i'm having one hell of a time trying to parse an xml

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.