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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:33:35+00:00 2026-05-27T04:33:35+00:00

I am trying to have a function that can search for an item (in

  • 0

I am trying to have a function that can search for an item (in this case, a motherboard) in a database when given parameters, but I want to be able to get all motherboards if no parameters are given.

The query is:

SELECT * FROM motherboard WHERE slot_type_cpu LIKE @cpu_slot AND hdd_num > @hdd_num AND sata_gen LIKE @sata_gen AND ram_pins LIKE @ram_pins AND ram_type LIKE @ram_gen AND ram_num > @ram_num AND gpu_num > @gpu_num AND gpu_slot LIKE @gpu_slot AND usb_num > @usb_num AND usb_gen LIKE @usb_gen;

Here is the current function:

    public Motherboard[] getMBs(string cpu_slot = "%", string gpu_slot = "%", string hdd_gen = "%", string sata_gen = "%", string ram_gen = "%", string usb_gen = "%", int hdd_num = 0, int ram_pins = 0, int ram_num = 0, int gpu_num = 0, int usb_num = 0)
            {
                try
                {
                    Motherboard[] mbList = new Motherboard[96];
                    Motherboard[] finishedList = new Motherboard[96];
                    MySqlConnection connect = new MySqlConnection(CONNECTION_STRING);

                    MySqlCommand useCmd = connect.CreateCommand();
                    MySqlCommand getMBCmd = connect.CreateCommand();
                    MySqlCommand getItemInfoCmd = connect.CreateCommand();

                    useCmd.CommandText = "use " + DEFAULT_DB + ";";
                    getMBCmd.CommandText = GET_MB_CMD;
                    getItemInfoCmd.CommandText = GET_ITEM_INFO_CMD;

                    connect.Open();

                    useCmd.ExecuteNonQuery();

                    getMBCmd.Parameters.AddWithValue("@cpu_slot", cpu_slot);
                    getMBCmd.Parameters.AddWithValue("@hdd_num", hdd_num);
                    getMBCmd.Parameters.AddWithValue("@sata_gen", sata_gen);
                    getMBCmd.Parameters.AddWithValue("@gpu_slot", gpu_slot);
                    getMBCmd.Parameters.AddWithValue("@hdd_gen", hdd_gen);
                    getMBCmd.Parameters.AddWithValue("@ram_gen", ram_gen);
                    getMBCmd.Parameters.AddWithValue("@usb_gen", usb_gen);
                    getMBCmd.Parameters.AddWithValue("@ram_pins", ram_pins);
                    getMBCmd.Parameters.AddWithValue("@ram_num", ram_num);
                    getMBCmd.Parameters.AddWithValue("@gpu_num", gpu_num);
                    getMBCmd.Parameters.AddWithValue("@usb_num", usb_num);

                    MySqlDataReader reader = getMBCmd.ExecuteReader();
                    reader.Read();

    //Here comes the trouble part.
                    if (reader.HasRows)
                    {
                        int i = 0;
                        while (reader.HasRows)
                        {
                            mbList[i] = new Motherboard(reader.GetString(0), "", "", 0.00, 0, reader.GetString(1), reader.GetString(3), new RAM("", "", "", 0, 0, reader.GetString(5), reader.GetInt32(4), 0), reader.GetString(8), reader.GetInt32(2), reader.GetInt32(6), reader.GetInt32(7), reader.GetInt32(10), reader.GetInt32(11), reader.GetInt32(12));
                            i++;
                            reader.Read();
                        }
                    }
                    reader.Close();

//It doesn't retrieve any items, even though I can retrieve them manually using the same query...

                    int y = 0;
                    foreach (Motherboard m in mbList)
                    {
                        getItemInfoCmd.Parameters.AddWithValue("@id", m.id()); //This gives an Object reference not set to an instance of an object error

                        reader = getItemInfoCmd.ExecuteReader();
                        reader.Read();

                        if (reader.HasRows)
                            finishedList[y] = new Motherboard(m.id(), reader.GetString(3), reader.GetString(4), reader.GetDouble(1), reader.GetFloat(2), m.CPU_Slot(), m.HDD_Conn(), m.RAM(), m.GPU_Slot(), m.HDD_num(), m.RAM_num(), m.GPU_num(), m.USB_num(), m.USB_gen(), m.CPU_num());

                        reader.Close();
                        getItemInfoCmd.Parameters.Clear();
                    }
                    connect.Close();

                    return finishedList;
                }
                catch (Exception e)
                {
                    Error error = new Error();

                    error.reportError(e.Message.ToString() + " " + e.StackTrace.ToString(), DateTime.Now.ToString(), "ItemRetrieval.getMBs: { } ");

                    return null;
                }

EDIT: (HOW I FIXED IT)
What I ended up doing and it works fine (this might have been what aleroot meant, but I couldn’t deciper what they said… sorry.):

                if (cpu_slot == "%")
                    getMBCmd.CommandText = getMBCmd.CommandText.Replace("@cpu_slot", "\'%\'");
                else
                    getMBCmd.Parameters.AddWithValue("@cpu_slot", cpu_slot);

                if(gpu_slot == "%")
                    getMBCmd.CommandText = getMBCmd.CommandText.Replace("@gpu_slot", "\'%\'");
                else
                    getMBCmd.Parameters.AddWithValue("@gpu_slot", gpu_slot);

                if(hdd_gen == "%")
                    getMBCmd.CommandText = getMBCmd.CommandText.Replace("@hdd_gen", "\'%\'");
                else
                    getMBCmd.Parameters.AddWithValue("@hdd_gen", hdd_gen);

                if(sata_gen == "%")
                    getMBCmd.CommandText = getMBCmd.CommandText.Replace("@sata_gen", "\'%\'");
                else
                    getMBCmd.Parameters.AddWithValue("@sata_gen", sata_gen);

                if (ram_gen == "%")
                    getMBCmd.CommandText = getMBCmd.CommandText.Replace("@ram_gen", "\'%\'");
                else
                    getMBCmd.Parameters.AddWithValue("@ram_gen", ram_gen);

                getMBCmd.Parameters.AddWithValue("@usb_gen", usb_gen);
                getMBCmd.Parameters.AddWithValue("@hdd_num", hdd_num);
                getMBCmd.Parameters.AddWithValue("@ram_pins", ram_pins);
                getMBCmd.Parameters.AddWithValue("@ram_num", ram_num);
                getMBCmd.Parameters.AddWithValue("@gpu_num", gpu_num);
                getMBCmd.Parameters.AddWithValue("@usb_num", usb_num);
  • 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-27T04:33:35+00:00Added an answer on May 27, 2026 at 4:33 am

    You can pass % wildcard as a default parameter for the mainboard , so if you set as default % and filter with a like, in the case of missing mainboard parameter you will get all of your mainboard …

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

Sidebar

Related Questions

I have a function that takes a struct, and I'm trying to store its
At www.euroworker.no/order I have been trying to add a tooltip function but it seems
I'm trying to find out where this function comes from. Any one have any
I'm trying to make search terms bold in this search script that I'm making.
This is a homework question,I'm trying to do a Depth-first search function in Scheme,Here's
I am trying to integrate this logic in my script but I can't really
I have a simple function that I want to call in the code behind
this is my situation, I have a Post model that can belong to multiple
I'm trying to write a function that does the following: Given a text file,
I have functions that contribute to small parts of a figure generation. I'm trying

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.