4 Replies - 660 Views - Last Post: 14 May 2012 - 02:00 PM Rate Topic: -----

#1 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Using a variable like it's static in a webform

Posted 14 May 2012 - 05:07 AM

Hello, I have a variable Upperbound as integer and an array of two dimensions PartsArray(,). What I want to do is to store the name of a selected car part in the first part of the array and its price in the second part.

My code will work if I solve the problem of the upperbound variable, I need to make it increment like it was declared as static.

    Dim Sum As Double
    Dim UpperBound As Integer
    Dim PartsArray(UpperBound, UpperBound) As Object

    Protected Sub btnAdd_Click(sender As Object, e As System.EventArgs) Handles btnAdd.Click
        If ddlChosenParts.Items.Contains(New ListItem(lblPart.Text)) Then
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = " - Part '" & lblPart.Text & "' already added"
            Exit Sub
        End If

        connString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")
        Dim con As New SqlConnection(connString.ConnectionString)

        If ddlLevel.SelectedItem.Text = "Low" Then
            Dim Query As String
            Query = "SELECT Price FROM Parts WHERE Type = '" & lblPart.Text & "' AND Continent_ID = '" & ddlContinent.SelectedValue & "'"
            Dim cmdSelect As New SqlCommand
            cmdSelect.CommandText = Query
            cmdSelect.Connection = con
            Dim ds As New DataSet()
            Dim dt As DataTable
            Dim read As SqlDataReader
            con.Open()
            read = cmdSelect.ExecuteReader()
            While read.Read
                ReDim Preserve PartsArray(UpperBound, UpperBound + 1)
                PartsArray(UpperBound, 0) = lblPart.Text
                PartsArray(UpperBound, 1) = (read("Price") * ddlContinent.SelectedValue * 0.3)
            End While
            dt = New DataTable("Car")
            ds.Tables.Add(dt)
            ds.Load(read, LoadOption.PreserveChanges, ds.Tables(0))
            con.Close()
            UpperBound += 1
        ElseIf ddlLevel.SelectedItem.Text = "Medium" Then
            Dim Query As String
            Query = "SELECT Price FROM Parts WHERE Type = '" & lblPart.Text & "' AND Continent_ID = '" & ddlContinent.SelectedValue & "'"
            Dim cmdSelect As New SqlCommand
            cmdSelect.CommandText = Query
            cmdSelect.Connection = con
            Dim ds As New DataSet()
            Dim dt As DataTable
            Dim read As SqlDataReader
            con.Open()
            read = cmdSelect.ExecuteReader()
            While read.Read
                ReDim Preserve PartsArray(UpperBound, UpperBound + 1)
                PartsArray(UpperBound, 0) = lblPart.Text
                PartsArray(UpperBound, 1) = (read("Price") * ddlContinent.SelectedValue * 0.6)
            End While
            dt = New DataTable("Car")
            ds.Tables.Add(dt)
            ds.Load(read, LoadOption.PreserveChanges, ds.Tables(0))
            con.Close()
            UpperBound += 1
        ElseIf ddlLevel.SelectedItem.Text = "High" Then
            Dim Query As String
            Query = "SELECT Price FROM Parts WHERE Type = '" & lblPart.Text & "' AND Continent_ID = '" & ddlContinent.SelectedValue & "'"
            Dim cmdSelect As New SqlCommand
            cmdSelect.CommandText = Query
            cmdSelect.Connection = con
            Dim ds As New DataSet()
            Dim dt As DataTable
            Dim read As SqlDataReader
            con.Open()
            read = cmdSelect.ExecuteReader()
            While read.Read
                ReDim Preserve PartsArray(UpperBound, UpperBound + 1)
                PartsArray(UpperBound, 0) = lblPart.Text
                PartsArray(UpperBound, 1) = (read("Price") * ddlContinent.SelectedValue)
            End While
            dt = New DataTable("Car")
            ds.Tables.Add(dt)
            ds.Load(read, LoadOption.PreserveChanges, ds.Tables(0))
            con.Close()
            UpperBound += 1
        Else
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = "Must choose level"
            Exit Sub
        End If

        ddlChosenParts.Items.Add(lblPart.Text)
        lblStatus.ForeColor = Drawing.Color.Blue
        lblStatus.Text = " - Part '" & lblPart.Text & "' added to list of damaged parts"
    End Sub



I just want to know how to increment the variable Upperbound.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Using a variable like it's static in a webform

#2 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Using a variable like it's static in a webform

Posted 14 May 2012 - 05:23 AM

I modified the code a bit, but didn't change anything.

    Dim UpperBound As Integer
    Dim PartsArray(UpperBound, UpperBound) As Object

    Protected Sub btnAdd_Click(sender As Object, e As System.EventArgs) Handles btnAdd.Click
        If ddlChosenParts.Items.Contains(New ListItem(lblPart.Text)) Then
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = " - Part '" & lblPart.Text & "' already added"
            Exit Sub
        End If

        Dim Percentage As Double

        If ddlLevel.SelectedItem.Text = "Low" Then
            Percentage = 0.3
        ElseIf ddlLevel.SelectedItem.Text = "Medium" Then
            Percentage = 0.6
        ElseIf ddlLevel.SelectedItem.Text = "High" Then
            Percentage = 1
        Else
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = "Must choose level"
            Exit Sub
        End If

        connString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")
        Dim con As New SqlConnection(connString.ConnectionString)

        Dim Query As String
        Query = "SELECT Price FROM Parts WHERE Type = '" & lblPart.Text & "' AND Continent_ID = '" & ddlContinent.SelectedValue & "'"
        Dim cmdSelect As New SqlCommand
        cmdSelect.CommandText = Query
        cmdSelect.Connection = con
        Dim ds As New DataSet()
        Dim dt As DataTable
        Dim read As SqlDataReader
        con.Open()
        read = cmdSelect.ExecuteReader()
        While read.Read
            ReDim Preserve PartsArray(UpperBound, UpperBound + 1)
            PartsArray(UpperBound, 0) = lblPart.Text
            PartsArray(UpperBound, 1) = (read("Price") * ddlContinent.SelectedValue * Percentage)
        End While
        dt = New DataTable("Car")
        ds.Tables.Add(dt)
        ds.Load(read, LoadOption.PreserveChanges, ds.Tables(0))
        con.Close()

        UpperBound += 1
        ddlChosenParts.Items.Add(lblPart.Text)
        lblStatus.ForeColor = Drawing.Color.Blue
        lblStatus.Text = " - Part '" & lblPart.Text & "' added to list of damaged parts"
    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: Using a variable like it's static in a webform

Posted 14 May 2012 - 07:01 AM

Moving to ASP.NET
Was This Post Helpful? 0
  • +
  • -

#4 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Using a variable like it's static in a webform

Posted 14 May 2012 - 08:45 AM

I'm using ASP.NET with VB.NET
Was This Post Helpful? 0
  • +
  • -

#5 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Using a variable like it's static in a webform

Posted 14 May 2012 - 02:00 PM

Never mind, Problem solved by declaring the variable upperbound as shared.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1