Aaron Hurst
Aaron Hurst

Reputation: 21

Passing multiple Query Parameters to TableAdaper Fill Method C#

I am attempting to create a program in Visual Studio that uses a Query with multiple parameters to fill a datagrid view using a TableAdapter method in C#. The database is a SQL server express Database managed with server management studio. I have the TableAdapter query made using the DataSet Designer and it works when testing it out from within there. This is the Query:

SELECT Customer, Department, Brand, Model, TotalTime, AMPM, InvoiceNumber, Day, TechName, EntryID, Remarks, Date FROM Entries WHERE (TechName = @TechName) AND Date BETWEEN @Date1 AND @Date2

The program is supposed to have the user select a name in the database from a combobox and enter two dates from datetimepicker boxes then they click a button and it runs the query and updates the datagridview with the results. Below is the code for the button.

    private void searchButton_Click(object sender, EventArgs e)
    {

        if ((techNameComboBox.Text.Length > 0)
        && (dateDateTimePicker.Text.Length > 0)
        && (dateDateTimePicker1.Text.Length > 0))
        {
            try
            {
                this.entriesTableAdapter.FillByDateRange(
                    this.timeSheetEntriesDataSet.Entries, (techNameComboBox.Text, dateDateTimePicker.Text, dateDateTimePicker1.Text));
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

I am getting a "There is no argument given that corresponds to the required formal parameter 'Date1' error... when using table adapter alot of code is automatically generated for you and i don't know if there's something in the background that isn't getting generated in order to pass multiple parameters to that method or if my code for the button click is missing something but it won't pass multiple parameters to the query, only the first one. And if i design a query that just uses one parameter and use the same code above (minus the extra two parameters) it works just fine.

What am i missing in order to get this to work? I only have a vague understanding of C# so i can't for the life of me figure it out.

Upvotes: 1

Views: 1575

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

According to your error, you should use dateTimePicker.Value instead of

dateTimePicker.Text.

Because dateTimePicker.Text just a type of string and

dateTimePicker.Value is a type of datetime.

We need to make them correspond.

Here is my tested code.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void entriesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.entriesBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.testDBDataSet);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'testDBDataSet.Entries' table. You can move, or remove it, as needed.
        this.entriesTableAdapter.Fill(this.testDBDataSet.Entries);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        TestDBDataSet dBDataSet = new TestDBDataSet();
        entriesTableAdapter.FillBy(dBDataSet.Entries, textBox1.Text, dateTimePicker1.Value, dateTimePicker2.Value);
        entriesDataGridView.DataSource = dBDataSet.Entries;

    }
}

Result:

enter image description here

Upvotes: 1

Related Questions