ali alavi
ali alavi

Reputation: 11

The timeout period ... IN LINQ C#

While executing the down lines of my C# controller code I was getting

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

I use linq to sql in my c#.and result show my report form.my reporter is rdlc.

I tried like set the connect Timeout = 0; also, but I get the same error.

How can I solve it?
please help me...thanks.

private void bt10_Click(object sender, EventArgs e)
{
    {
        Form3 frm3 = new Form3();
        DataSet ds = new DataSet();

        var DB = new DataClasses3DataContext();
        var c1 = (from c in DB.A
                    join r in DB.A1
                    on c.DATE equals r.DATE
                    where c.B.Equals(int.Parse(txtSearch.Text))
                       && (string.Compare(c.DATE.ToString(), txtEdate.Text) <= 0 
                       && string.Compare(c.DATE.ToString(), txtFdate.Text) >= 0)
                       && (c.NO == r.NO)
                       && (c.K == 10)
                       && (c.A == 1)
                       && (r.A == 2)
                       && r.K != 10
                    select c).ToList();    

        dataGridView1.DataSource = c1;
        DataSet2.ADataTable dt = new DataSet2.ADataTable();

        foreach (A item in c1)
        {
            dt.Rows.Add(item.B, item.DATE, item.NO, item.K); 
        }

        if (c1 == null)
        {
            MessageBox.Show("null");
        }
        else
        {
            ds.Tables.Add(dt);
            ReportDataSource rds = new ReportDataSource();
            frm3.reportViewer1.LocalReport.DataSources.Clear();
            rds.Name = "DataSet3";
            rds.Value = dt;
            frm3.reportViewer1.LocalReport.DataSources.Add(rds);
            frm3.reportViewer1.LocalReport.ReportEmbeddedResource = "P10.Report5.rdlc";
            frm3.ShowDialog();
        }
    }
}

Upvotes: 0

Views: 2883

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109220

Don't "fix" timeouts by increasing them. Fix the underlying problem!

It's very likely that convertingc.DATE to string before comparing it causes the problem. This makes the expression non-sargable, i.e. any index on DATE is disabled. You should change this by parsing txtEdate.Text and txtFdate.Text first, and use the parsed values in the comparison (and also parse txtSearch first).

var edate = DateTime.Parse(txtEdate.Text);
var fdate = DateTime.Parse(txtFdate.Text);
var searchInt = int.Parse(txtSearch.Text);

...
where c.B == searchInt
   && c.DATE <= edate 
   && c.DATE >= fdate
...

If this still doesn't help you should investigate proper indexing on the database tables. For such a relatively simple query you just shouldn't get a timeout.

Upvotes: 1

Zein Makki
Zein Makki

Reputation: 30052

Use CommandTimeout Property That adds to the default time out which is 30 seconds:

DB.CommandTimeout = 1 * 60; // Adds 1 Minute to the time out, so 1:30 minutes now

If this is a long running task, you might need to consider an async method.

For More : DataContext.CommandTimeout

Upvotes: 0

Related Questions