1

I'm going to write a program that finds the score of paragraph. The database file has a score of words.
database file

    Word                       Pos_Score            

    intelligent               .987              
    allows                    .378               
    agree                     .546              
    industries                .289               
    guests                    .874        

Using SELECT query and WHERE class i compared paragraph words with database file.
paragraph:

I agree with you.  It seems an intelligent tourist industry allows its guests to either immerse fully, in part, or not, depending upon the guest.    

the above paragraph have some words that matched the database file word so the score of that word will be extracted.
Program output should be like this

Pos_score= .987+.378+.546+.289+.874=3.074     

Code

private void button1_Click(object sender, EventArgs e)
        {
            string MyConString = "server=localhost;" +"database=sentiwornet;" + "password=zia;" +"User Id=root;";
            StreamReader reader = new StreamReader("D:\\input.txt");
            float amd=0;
            string line;
            float pos1 = 0;
            while ((line = reader.ReadLine()) != null)
            {
                string[] parts = line.Split(' ');
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;

                foreach (string part in parts)
                {

                    command.CommandText = "SELECT Pos_Score FROM score WHERE Word = part";
                    try
                    {
                        amd = (float)command.ExecuteScalar();
                        pos1 = amd + pos1;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    Reader = command.ExecuteReader();
                    connection.Open();
                }
            }
            MessageBox.Show("The Positive score of Sentence is="+pos1.ToString());
            reader.Close();
        }    

but this code is not working and it gives the following error.

Error 1
Object reference not set to an instance of an object

Error 2
Connection must be valid and open.

6
  • first open connection and then fire the qury Commented Mar 20, 2013 at 8:37
  • first check that you are getting connection string value or not from MyConString and second error occur's because one of your function is close your connection.so plz check ExecuteScalar() and ExecuteReader() function carefully.. Commented Mar 20, 2013 at 8:40
  • @Prasad: if i open connection first then this problem occur:**Unknown column 'part' in 'where clause'** Commented Mar 20, 2013 at 8:42
  • @ZiaRehman Do you have a stack trace for these errors? Commented Mar 20, 2013 at 8:43
  • trust me u can't execute command if connection is not open Commented Mar 20, 2013 at 8:44

2 Answers 2

1

i think this must be 1st problem...

private void button1_Click(object sender, EventArgs e)
    {
        string MyConString = "server=localhost;" +"database=sentiwornet;" + "password=zia;" +"User Id=root;";
        StreamReader reader = new StreamReader("D:\\input.txt");
        float amd=0;
        string line;
    MySqlConnection connection = new MySqlConnection(MyConString);
    connection.Open();
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        float pos1 = 0;
        while ((line = reader.ReadLine()) != null)
        {
            string[] parts = line.Split(' ');


            foreach (string part in parts)
            {

                command.CommandText = "SELECT Pos_Score FROM score WHERE Word = @part";
                command.Parameters.Add("@part",MySqlDBtype.Varchar).Value = part;
                try
                {
                    amd = (float)command.ExecuteScalar();
                    pos1 = amd + pos1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                Reader = command.ExecuteReader();

            }
        }
        MessageBox.Show("The Positive score of Sentence is="+pos1.ToString());
        reader.Close();
        connection.Close();
    }    
Sign up to request clarification or add additional context in comments.

Comments

0
command.CommandText = "SELECT Pos_Score FROM score WHERE Word = " + part;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.