-1

I am trying to have c# look to see if the database table equals what i am asking it to equal to open a specific form here is one way i am try doing it

void getsformat()
    {
        string constring = @"server=host;userid=user;password=pass";
        string Query = "select colum from table where callsign= '" + listBox1.SelectedItem + "'";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sfName = myReader.GetString("sformat");
              }  
            if (sfName == "stworld")
            {
                sendtext = textBox1.Text;
                Form3 f3 = new Form3();
                 f3.Location = new Point(this.Top);

                f3.ShowDialog();  
            }
            else if (sfName == "seperate")
            {
                sendtext = textBox1.Text;
                Form3 f4 = new Form3();
                f4.Location = new Point(this.Top);

                f4.ShowDialog();  
            }


            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

and the othere way with the "while myreader "end quote after the "if" statement

void getsformat()
    {
        string constring = @"server=host;userid=user;password=pass";
        string Query = "select colum from table where callsign= '" + listBox1.SelectedItem + "'";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sfName = myReader.GetString("sformat");

            if (sfName == "stworld")
            {
                sendtext = textBox1.Text;
                Form3 f3 = new Form3();
                f3.Location = new Point(this.Top);

                f3.ShowDialog();  
            }
            else if (sfName == "seperate")
            {
                sendtext = textBox1.Text;
                Form3 f4 = new Form3();
                 f4.Location = new Point(this.Top);

                f4.ShowDialog();  
            }

            }
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

i cannot get either of them to work . Any help would be apreciated thank you in advanced

Asieh hojatoleslami
  • 3,240
  • 7
  • 31
  • 45
  • How do they not work? What should they do, what do they do? Are there errors, or just no data? And NEVER concatenate strings into SQL queries. Use parameters. – Sami Kuhmonen Apr 14 '15 at 10:12
  • it only opens one form trying to open different forms on the outcome of the sformat – user1625141 Apr 14 '15 at 10:19

2 Answers2

0

It doesn't look like you have the connection string setup properly. This:

string constring = @"server=host;userid=user;password=pass";

Should have the relevant info

string constring = @"server=localhost\SQL2008;userid=myusername;password=mypass";
theUser
  • 1,346
  • 3
  • 21
  • 40
0

In your select statement change it to

listbox1.selecteditem.tostring();
JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51