Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Seth Ford   on Mar 04 In Asp.net Category.

  
Question Answered By: Hubba Akhtar   on Mar 04

Some suggestions:

You can eliminate 2 DataReaders and speed up your program a lot.

#1
The segment below is best replaced with ExecuteScalar. see:
http://www.learnasp.com/freebook/learn/executescalar.aspx

ExecuteScalar is lightning fast at grabbing 1 value.


Dim fieldId As Integer

Comm.CommandText = "select max(eField_ID)
>From eForm_Table_Master where eForm_ID = " &
Request("tID")
DataRdr =
Comm.ExecuteReader(CommandBehavior.SequentialAccess)

If DataRdr.Read() Then
If DataRdr.IsDBNull(0) = True Then
fieldId = 0
Else
fieldId = CInt(DataRdr.Item(0))
End If
Else
fieldId = 0
End If

DataRdr.Close()
Comm.Dispose()


In this segment below why are you fetching * you are only using Field 0. Replace it with executeScalar as well.

tempQry = "Select * From
eForm_Table_Master where eForm_Id = " & Request("tID")
& " and DE_Caption = '" & QuestionText & "'"

'On Error Resume Next
Comm2.CommandText = tempQry
Dim DataRdr2 As SqlDataReader
DataRdr2 =
Comm2.ExecuteReader(CommandBehavior.SequentialAccess)

If DataRdr2.Read() Then
If DataRdr2.IsDBNull(0) = True
Then
validornot = True
Else
validornot = False
End If
Else
validOrNot = True
End If

'Here is the place
'DataRdr2.Close()
'Comm2.Dispose()

If validornot = True Then
mQry += "Insert into
eForm_Table_Master values(" & Request("tID") & "," &
fieldId & ",'" & QuestionText & "'," & QuestionType &
"," & QuestionNoOfChoices & ")" & "$"

tempQry = "Alter table  " & Chr(34)
& lblTemplateName.Text & Chr(34) & " Add "
If QuestionType = 5 Then
tempQry = tempQry & Chr(34) &
QuestionText & Chr(34) & " numeric  "
Else
tempQry = tempQry & Chr(34) &
QuestionText & Chr(34) & " varchar(255)"
End If
mQry += tempQry & "$"

If QuestionNoOfChoices > 1 Then
Comm1.CommandText = "select *
>From eForm_Table_Choice_Master where eForm_Id =" &
txtTemplateName.Text & " and eField_Id = " &
QuestionFieldID
DataRdr1 =
Comm1.ExecuteReader(CommandBehavior.SequentialAccess)

While DataRdr1.Read
mQry += "Insert into
eForm_Table_Choice_Master Values(" & Request("tID") &
"," & fieldId & ",'" & DataRdr1.Item(2).ToString() &
"')" & "$"
End While
DataRdr1.Close()
Comm1.Dispose()
End If
End If
End While
DataRdr.Close()
Comm.Dispose()

Comm.CommandText = "executeSetOfStatement"
Comm.CommandType =
CommandType.StoredProcedure

Dim PMThreadDesc As SqlParameter
PMThreadDesc = New
SqlParameter("@whichStatement", SqlDbType.VarChar,
2555)
PMThreadDesc.Value = Trim(mQry)
Comm.Parameters.Add(PMThreadDesc)

Dim PMEmpID As SqlParameter
PMEmpID = New SqlParameter("@delimeter",
SqlDbType.VarChar, 1)
PMEmpID.Value = "$"
Comm.Parameters.Add(PMEmpID)

Comm.ExecuteNonQuery()
Comm.Dispose()

Response.Redirect("viewAllTemplateQuestions.aspx?tID="
& Request("tID"))

I personally think you should learn about DataTables as well particularly for your outermost loop, see:
http://www.learnasp.com/freebook/learn/datatable.aspx

datareaders need 1 connection  each. 6 datareaders in a loop, 6 connections needed. As long as they have their own connections you can have as many as you want. DataTables fill and ditch their connection, so you could have 6 datatables all from 1 connection.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Can Datareader be used inside a datareader Or get search suggestion and latest updates.


Tagged: