Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable is used before it has been assigned a value

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
I want my code to look at each control i have, find the data in the control, and then insert it to a DB. This works, except I have only got it to work with a for each loop, which i don't want. Of course this inserts the same data 9 times which is how many rows I have. which i don't want
If I don't use for each then I get the message for my dgi.findcontrol that says "Variable is used before it has been assigned a value". I tried using a few different IF Then commands to no avail. Here is my code. Thanks
Code:
  Sub InsertNew(ByVal sender As Object, ByVal e As EventArgs)

        Dim dgi As DetailsViewRow

        For Each dgi In dviewINnew.Rows

            ' Find the Selected Class Name ID Value

            Dim ClassIDup As Integer = CType(dgi.FindControl("ddlClass"), DropDownList).SelectedValue

            Dim Titleup As String = CType(dgi.FindControl("txtTitle"), TextBox).Text
            Dim Descriptionup As String = CType(dgi.FindControl("txtDescription"), TextBox).Text

            Dim TeacherIDup As Integer = CType(dgi.FindControl("ddlTeacher"), DropDownList).SelectedValue

            Dim Totalpointsup As Decimal = CType(dgi.FindControl("txtTotalPoints"), TextBox).Text

            Dim AssignDateup As Date = CType(dgi.FindControl("calDateassign"), Calendar).SelectedDate

            Dim DueDateup As Date = CType(dgi.FindControl("calDuedate"), Calendar).SelectedDate


            'Create a New Insert Paremeter for the above values
            dsNewAssign.InsertParameters.Add("agTitle", Titleup)

            dsNewAssign.InsertParameters.Add("agDescription", Descriptionup)

            'dsClassID.InsertParameters.Add("AssignmentsID", ID)

            dsNewAssign.InsertParameters.Add("agTotalPoints", Totalpointsup)

            dsNewAssign.InsertParameters.Add("agDateAssigned", AssignDateup)

            dsNewAssign.InsertParameters.Add("agDueDate", DueDateup)
            dsNewAssign.InsertParameters.Add("agClassID", ClassIDup)
            dsNewAssign.InsertParameters.Add("agTeacherID", TeacherIDup)
            'Response.Write(dsUpdateAssign.Update)

            'Response.End()


            dsNewAssign.Insert()

            dsNewAssign.InsertParameters.Clear()



        Next

            Response.Redirect("~/Teachers/assignUPD.aspx")


    End Sub
 
this is not the asp.net forum. please post there. And perhaps you could tell them what it is you want.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
What happens if you put the insert outside the for next?
for
do stuff

next
dsNewAssign.Insert()
dsNewAssign.InsertParameters.Clear()



if it is to be it's up to me
 
Nope that didn't work, but Now it seems as if my code is working fine. It only inputs one record. My only thought is maybe when I was testing my aspx pages it was inserting data even though it was throwing me errors.

Thanks anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top