1

Here is a brief of what I try to do: there is a global temp table in SQL side that I select from it in C# wpf and I have it listed in my dataGrid DataGridIns.

I want to select some of rows by chkBox and then insert it to a table in SQL using a stored procedure.

So when I click to send SelectedRow it start as follows:

HoEntities database = new HoEntities();  //  using Entity Framework

try
{
    for (int i = 0; i < DataGridIns.Items.Count - 1; i++)
    {
        mChkBox = DataGridIns.Columns[0].GetCellContent(DataGridIns.Items[i]) as CheckBox;

        if (mChkBox.IsChecked == true)
        {
            var dataitem = DataGridIns.Items[2].ToString();
            _nullChckBox++;
            var dateSelect = DataGridIns.Columns[1].GetCellContent(DataGridIns.Items[i]) as TextBlock;
            var mainNameTBk = DataGridIns.Columns[2].GetCellContent(DataGridIns.Items[i]) as TextBlock;
            var chinCodeTBk = DataGridIns.Columns[3].GetCellContent(DataGridIns.Items[i]) as TextBlock;
            var chinNameTBk = DataGridIns.Columns[4].GetCellContent(DataGridIns.Items[i]) as TextBlock;

            var mainCodeTBk = DataGridIns.Columns[6].GetCellContent(DataGridIns.Items[i]) as TextBlock;
            var iodDescTBk = DataGridIns.Columns[7].GetCellContent(DataGridIns.Items[i]) as TextBlock;
        }

        database.sp_Ins_Main    // using stored procedure to insert into mainTable
            (mainCodeTBk.Text.Trim(), 
             mainNameTBk.Text.Trim(),
             Convert.ToInt32(chinCodeTBk.Text.Trim()),
             chinNameTBk.Text.Trim(),
             iodDescTBk.Text.Trim(),
             dateSelect.Text.Trim()
            );

            database.SaveChanges();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

As I set the breakpoint and go forward step by step it reaches

database.sp_Ins_Main

and the jumps to my DataModel.Context.cs from DataModel in Entity Framework as follows:

public virtual int sp_Ins_Main(string mainCode, string mainName,
                               Nullable<int> chinCode, string chinName,
                               string iodDesc, string date)
{
        var mainCodeParameter = mainCode != null ?
            new ObjectParameter("MainCode", mainCode) :
            new ObjectParameter("MainCode", typeof(string));

        var mainNameParameter = mainName != null ?
            new ObjectParameter("MainName", mainName) :
            new ObjectParameter("MainName", typeof(string));

        var chinCodeParameter = chinCode.HasValue ?
            new ObjectParameter("chinCode", chinCode) :
            new ObjectParameter("chinCode", typeof(int));

        var chinNameParameter = chinName != null ?
            new ObjectParameter("chinName", chinName) :
            new ObjectParameter("chinName", typeof(string));

        var iodDescParameter = iodDesc != null ?
            new ObjectParameter("iodDesc", iodDesc) :
            new ObjectParameter("iodDesc", typeof(string));

        var dateParameter = date != null ?
            new ObjectParameter("Date", date) :
            new ObjectParameter("Date", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_Ins_Main", mainCodeParameter, mainNameParameter, chinCodeParameter, chinNameParameter, iodDescParameter, dateParameter);
}

and from the line for Return ((IObjectContextAdapter)this) ... I get the error that I said in the titeld question.

System.Data.SqlClient.SqlError: New transaction is not allowed because there are other threads running in the session.

is it wrong my way that select from Temp Table and after sits in datagrid select and I just want to insert into table in Sql? how to solve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
farzad
  • 19
  • 5
  • When you are doing multithreadin with Networking (including DBConnections), the goal is usually to have 1 connection/session per thread/context. Not sharing connections between them. The Isolation offered by Threads is a important part of the design. – Christopher Dec 08 '19 at 18:32
  • is it an example of how to Isolate ? – farzad Dec 08 '19 at 18:43
  • I can not tell you if using Threads for isolation is really a good way to make Isolation. Threads have been (ab)used a *lot* for things they were never intended for or were not a good solution - simply because they were the best solution around at the time. – Christopher Dec 11 '19 at 18:19

0 Answers0