Question Problem with SQL query and RowFilter "SOLVED"

Moff

New member
Joined
Jan 16, 2013
Messages
2
Programming Experience
Beginner
I'm having problem with a school assignment. I'm almost done with my program but I have one problem that I can't solve. The problem is with one of my SQL query's that are supposed to show players with different scores. When running my program I get the following error: Syntax error near expression..
My code:
Code:
// Filters players. WORKS
private void button1_Click(object sender, EventArgs e)
        {
            view.RowFilter = "LastName like '%" + textBox1.Text + "%'";
            if (textBox1.Text == "") view.RowFilter = string.Empty;
        }
// Connection to the database. WORKS
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable datatable = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mattias\Dropbox\C#\Databases\Baseball.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            connection.Open();
            datatable.Load(new SqlCommand("select * from players", connection).ExecuteReader());
            dataGridView1.DataSource = view = datatable.DefaultView;
            connection.Close();
        }
// The following doesn't work
        private void button2_Click(object sender, EventArgs e)
        {
            decimal minimum = Convert.ToDecimal(textBox2.Text);
            decimal maximum = Convert.ToDecimal(textBox3.Text);


[B]// This is where the debugger throws the exception[/B]
            view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}", minimum, maximum);


            if (textBox2.Text == "") view.RowFilter = string.Empty;
            if (textBox3.Text == "") view.RowFilter = string.Empty;
        }
The picture shows the GUI and how it is supposed to work. By entering 0,3 and 0,4 in the textboxes should filter out the player "Jack Blue".

Can someone please help me and explain what I can do to solve this problem?
// Moff :)
 

Attachments

Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,880
Location
Sydney, Australia
Programming Experience
10+
It looks like you are using commas as decimal separators. I don't know for a fact but I'm going to guess that the pseudo-SQL that is supported by DataView.RowFilter is not culture-sensitive and only supports dots as decimal separators. Try using String.Replace to swap out the commas for dots and see if you get the desired result.
 

Moff

New member
Joined
Jan 16, 2013
Messages
2
Programming Experience
Beginner
It looks like you are using commas as decimal separators. I don't know for a fact but I'm going to guess that the pseudo-SQL that is supported by DataView.RowFilter is not culture-sensitive and only supports dots as decimal separators. Try using String.Replace to swap out the commas for dots and see if you get the desired result.
Thanks for the reply. It now works!! I didn't use String.Replace but since you mentioned culture-sensitive I used String.Format with rowFilters and it worked! This is what I did:

view.RowFilter = String.Format(Culture.Info.InvariantCulture.NumberFormat,("BattingAverage >= {0} AND BattingAverage <= {1}"), minimum, maximum);

Thank you so much!

Moff
 
Top Bottom