sqlcommand timeout using looping vb.net

296 Views Asked by At

may this code explain my problem,

dim dgv1 as new datagridview
dim dgv2 as new datagridview
dim dgv3 as new datagridview
dim dgv4 as new datagridview

sub processData()
    conn = new sqlconnection(strcon)
    conn1 = new sqlconnection(byconn)

    conn.open
    conn1.open
    dim trans as sqltransaction = conn.begintransaction
    dim trans1 as sqltransaction = conn1.begintransaction
    try

    dim con1 as new sqlconnection(strconn)
    con1.open
    dim cmd as new sqlcommand("some query",con1)
    cmd.commandtype = commandtype.storedprocedure
    dim da as new sqldataadapter(cmd)
    dim dt as new datatable
    da.fill(dt)
    con1.close
    dgv1.datasource = dt

    for i as integer = 0 to dgv1.rowcount -1
       getData2(i)
       for j as integer = 0 to dgv2.rowcount -1
          getData34()
       end for

       saveData1(i,conn,trans)
       saveData2(conn1,trans1)

    next
    trans.commit
    trans1.commit
    conn.close
    conn1.close
    catch ex as exception
       trans.rollback
       trans1.rollback
       conn.close
       conn1.close
    end try
end sub

sub getData2(i as integer)
    dim con1 as new sqlconnection(strconn)
    con1.open
    dim cmd as new sqlcommand("some query",con1)
    cmd.commandtype = commandtype.storedprocedure
    cmd.parameters.addwithvalue("@PARAM",dgv1.item(0,i).value)
    dim da as new sqldataadapter(cmd)
    dim dt as new datatable
    da.fill(dt)
    con1.close
    dgv2.datasource = dt
end sub

sub getdata34(i as integer)
    dim con1 as new sqlconnection(strconn)
    con1.open
    dim cmd as new sqlcommand("some query",con1)
    cmd.commandtype = commandtype.storedprocedure
    cmd.parameters.addwithvalue("@PARAM",dgv2.item(0,i).value)
    cmd.parameters.addwithvalue("@TYPE","LEFT")
    dim da as new sqldataadapter(cmd)
    dim dt as new datatable
    da.fill(dt)

    dgv3.datasource = dt

    cmd =new sqlcommand("some query",con1)
    cmd.commandtype = commandtype.storedprocedure
    cmd.parameters.addwithvalue("@PARAM",dgv2.item(0,i).value)
    cmd.parameters.addwithvalue("@TYPE","RIGHT")
    da =new sqldataadapter(cmd)
    dt =new datatable
    da.fill(dt)

    dgv4.datasource = dt
    con1.close
end sub

sub saveData1(i as integer,cn as sqlconnection, trans as sqltransaction)
    'some query with transaction
end sub

sub saveData2(cn as sqlconnection, trans as sqltransaction)
    'some query with transaction
end sub

While processData called, in function getData2 and getData34 always give me timeout error message. adding timeout in sqlcommand not help me. adding OPTION (RECOMPILE) in stored procedure also not help. Perhaps, I miss something.

0

There are 0 best solutions below