none
Getting info from a sql server database when empty RRS feed

  • Question

  • What I'm trying to do is load in a part of what I call is "bases". These relate to games. where users can add bases and then add missions from their games to that base. How ever the database is empty and I keep getting a "invalid object name : bases" bases is the name of the table where this table has 2 columns. I'm trying to load them in as I said the table is empty. My code is below. can some one tell me what I'm doing wrong. If the table is empty it should change a label to state that the table is empty and that there are no bases.

    private void loadBases()
            {
    
                var uR = MLHSetting.Default.currGame;
                SqlConnection conn = new SqlConnection();
    
                conn.ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog=master;Trusted_Connection = true;";
    
    
                conn.Open();
                m_MapBases.Items.Clear();
    
                try
                {
                    string sql = "select * from bases where shortname = '"+ uR +"' order by id asc";
    
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataReader reader = command.ExecuteReader();
                    //int count = 1;
    
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            object rowid = reader[0];
    
                            ListViewItem item = new ListViewItem(rowid.ToString());
                            item.SubItems.Add(reader["basename"].ToString());
                            item.SubItems.Add(reader["shortname"].ToString());
                            //count++;
                            CurrGame.Text = uR;
    
    
                        }
                    }
                    else
                    {
                        Map_BaseLabel.Text = "No bases available"; //m_MapBases.Items.Add("No Bases Available");
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Oops - " + ee.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
                finally
                {
                    conn.Close();
                }
            }
    UPDATE: I had to make some changes to this code to relate to the base name and the games short name. So when the load in. The short name of the game is use to id what game is loaded so it will load on the bases for that game.

    • Edited by Joesoft11a Saturday, March 16, 2019 4:21 PM UPDATED Code
    Saturday, March 16, 2019 4:02 PM

Answers

  • Note that you are connecting to the database "master". Your "base" table should not be there. That's why you get an error saying that base does not exist, because base does not exist in master. Instead of master, write in the connection string the name of the database that actually contains your base table.
    • Marked as answer by Joesoft11a Saturday, March 16, 2019 4:42 PM
    Saturday, March 16, 2019 4:34 PM
    Moderator

All replies

  • it looks like the error point to this line;

     string sql = "select * from bases "+ uR +" order by id asc";

    I'm not sure of the content of uR

    Should be something like;

    string sql = "Select * from bases where basename = " + uR + " order by id asc

    or 

    string sql = "Select * from bases where cname = " + uR + " order by id asc

    Saturday, March 16, 2019 4:20 PM
  • Make sure that “Initial Catalog=master” is correct and the table exists.

    Saturday, March 16, 2019 4:23 PM
  • Yes I updated the code above. If you look I add what you posted. I had to make some changes to it. How ever I'm still getting "invalid object name : bases". So It has me stump.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Saturday, March 16, 2019 4:24 PM
  • Note that you are connecting to the database "master". Your "base" table should not be there. That's why you get an error saying that base does not exist, because base does not exist in master. Instead of master, write in the connection string the name of the database that actually contains your base table.
    • Marked as answer by Joesoft11a Saturday, March 16, 2019 4:42 PM
    Saturday, March 16, 2019 4:34 PM
    Moderator
  • Thank you. I didn't think to change to the right database when I opened to connection. Now it's not giving me that error any more. Thank you VERY much. I had to add the code "Conn.changedatabase("mlhelper"); and that worked.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Saturday, March 16, 2019 4:42 PM