Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Probs with datareader and Validator

  Asked By: Bakir    Date: Dec 10    Category: Asp.net    Views: 755
  

I have got 2 Problems which I floated in the morning
but then no one replied let me put it again to you
Please help me

1.When I am using a datareader and say while
datareader.read() inside this loop I am again using
another datareader so its giving a error to close the
first datareader,but I cant do that as I have to have
both the values as the inner datareader is dependednt
on the data of the outer one, can any one help me
where I am going Wrong I will paste the code


Dim Conn As Object ' SqlConnection
Dim Comm As Object ' SqlCommand
Dim DataRdr As Object 'SqlDataReader
Dim ConnString As String
Dim NoOfQuestions As Integer
Dim mQry As String

InitializeDBObjects(Conn, Comm)

Conn.Open()
Comm.Connection = Conn

Dim Conn1 As Object ' SqlConnection
Dim Comm1 As Object 'SqlCommand
Dim DataRdr1 As Object 'SqlDataReader

InitializeDBObjects(Conn1, Comm1)

Conn1.Open()
Comm1.Connection = Conn1

Dim Conn2 As Object 'SqlConnection
Dim Comm2 As SqlCommand

InitializeDBObjects(Conn2, Comm2)

Conn2.Open()
Comm2.Connection = Conn2

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()

Comm.CommandText = "select * From
eForm_Table_Master where eForm_Id =" &
txtTemplateName.Text & " and eField_ID in(" &
Request("cbx") & ")"
DataRdr =
Comm.ExecuteReader(CommandBehavior.SequentialAccess)

NoOfQuestions = 0
While DataRdr.Read
Dim QuestionText As String
Dim QuestionType As Integer
Dim QuestionTypeText As String
Dim QuestionNoOfChoices As Integer
Dim QuestionFieldID As Integer
Dim tempQry As String
Dim validornot As Boolean


validornot = False

fieldId = fieldId + 1
QuestionFieldID =
CInt(DataRdr.Item(1))
QuestionText =
DataRdr.Item(2).ToString()
QuestionType =
CInt(DataRdr.Item(3).ToString())
QuestionNoOfChoices =
CInt(DataRdr.Item(4).ToString())

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"))


The second preoblem is withthe validator ehich I am
generating Dynamically but then the ControlToValidate
Property gives and err.

For Cntr = 1 To CInt(noOfChoices)
Dim tr As New TableRow()
Dim tc As New TableCell()
Dim txt As New TextBox()

txt.ID = "Text" & CStr(Cntr)
txt.Text = Request("Text" &
CStr(Cntr))

Dim txtValid As
RequiredFieldValidator
txtValid.ID = "r1"
txtValid.ControlToValidate =
txt.ID
txtValid.ErrorMessage =
"Fields Cannot be Blank"
txtValid.Display =
ValidatorDisplay.Dynamic

tc.Text = " Choice" & Cntr & "
: "

tc.Font.Name = "Verdana"
tc.Font.Size = New
System.Web.UI.WebControls.FontUnit(10)

tr.Cells.Add(tc)

tc = New TableCell()
tc.Controls.Add(txt)
tr.Cells.Add(tc)

dynTable1.Rows.Add(tr)
Next

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Tommy Thompson     Answered On: Dec 10

Try initialising "all the 2's" inside  the loop.

I know it seems like a waste of resources and it would be better to make things global but I have been told in the past that it is better practice to create and dispose of these as and when you need them, the overheads are minimal.

if you look at any daya layer which wraps up the functionality you will see that that is what is done, the connection  is created and destroyed every time the reader does anything at all.

 
Answer #2    Answered By: Adelmo Fischer     Answered On: Dec 10

The suggestion of

making
Dim txtValid As RequiredFieldValidator = New
RequiredFieldValidator()

is working without giving any error  but then the
Problem of validating is not solved it doesnt validate
the Dynamically generated Text Boxes.

 
Answer #3    Answered By: Mansur Bashara     Answered On: Dec 10

on your second one  you're doing this

Dim txtValid As RequiredFieldValidator
I think you need this, else you won't have an instance of this class

Dim txtValid As RequiredFieldValidator = new RequiredFieldValidator()

 
Didn't find what you were looking for? Find more on Probs with datareader and Validator Or get search suggestion and latest updates.




Tagged: