[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

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    [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 :-)
    Attached Images Attached Images  

  2. #2
    Join Date
    Jan 2013
    Posts
    1

    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());

  3. #3
    Join Date
    Jan 2013
    Posts
    2

    Re: Problem with SQL query and RowFilter

    Quote Originally Posted by ElizabethV View Post
    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);

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center