[RESOLVED] Problem with SQL query and RowFilter
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Problem with SQL query and RowFilter

Threaded View

  1. #1
    Join Date
    Jan 2013

    [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:

    // 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");
    datatable.Load(new SqlCommand("select * from players", connection).ExecuteReader());
    dataGridView1.DataSource = view = datatable.DefaultView;
    // 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 :-)
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)