1 Attachment(s)
[RESOLVED] Problem with SQL query and RowFilter
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);
// This is where the debugger throws the exception
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 :-)
Re: Problem with SQL query and RowFilter
A guess just by looking at your code that the formatting is wrong forthe maximum and minimum on the line
view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}", minimum, maximum);
try
view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}", minimum.ToString(), maximum.ToString());
Re: Problem with SQL query and RowFilter
Quote:
Originally Posted by
ElizabethV
A guess just by looking at your code that the formatting is wrong forthe maximum and minimum on the line
view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}", minimum, maximum);
try
view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}", minimum.ToString(), maximum.ToString());
I solved it by using the following code, but I guess yours will work to.
view.RowFilter = String.Format(Culture.Info.InvariantCulture.Number Format,("BattingAverage >= {0} AND BattingAverage <= {1}"), minimum, maximum);