How to create instance of class in different methods - asp.net

Do I have to instantiate description every time for different method? Or should I use static? Here's how I'm doing this now: What is the best way of handling this kind of situations. it seems that I repeat this line:Dim description As BLLDescription = New BLLDescription() without any good reasn.
Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
Dim description As BLLDescription = New BLLDescription()
List<String> = description.GetDescriptionWithoutNotes()
.....
End Sub
Protected Sub Button9_Click(sender As Object, e As System.EventArgs) Handles Button9.Click
Dim description As BLLDescription = New BLLDescription()
List<String> = description.GetDescriptionWithNotes()
.....
End Sub
Protected Sub Button10_Click(sender As Object, e As System.EventArgs) Handles Button10.Click
Dim description As BLLDescription = New BLLDescription()
List<String> = description.GetAllDescriptions()
.....
End Sub

IF you define BLLDescription as a static class, you can call the GetAllDescriptions() method without having to instantiate:
Protected Sub Button8_Click(sender As Object, e As System.EventArgs) Handles Button8.Click
List<String> = BLLDescription.GetDescriptionWithoutNotes()
.....
End Sub
Protected Sub Button9_Click(sender As Object, e As System.EventArgs) Handles Button9.Click
List<String> = BLLDescription.GetDescriptionWithNotes()
.....
End Sub
Protected Sub Button10_Click(sender As Object, e As System.EventArgs) Handles Button10.Click
List<String> = BLLDescription.GetAllDescriptions()
.....
End Sub

It depends on what the instance of BLLDescription does and how it gets the data.
If it is accessing the same data again and again, you can declare it as static. If it gets same data per request, then have it as a property at the class level.

Related

asp.net (with vb.net) control array at run time giving error : Object reference not set to an instance of an object

asp.net (with vb.net) control array at run time giving error :
Object reference not set to an instance of an object.
I am getting this error.
My Code is :
Partial Class _Default
Inherits System.Web.UI.Page
Dim chk(10) As CheckBox
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
MsgBox("loading")
chk(0) = New CheckBox
With chk(0)
.ID = "chk(0)"
.Text = .ID
End With
Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If chk(0).Checked = True Then
MsgBox("Yes")
Else
MsgBox("No")
End If
Response.Redirect("Page1.aspx")
End Sub
End Class
Use this instead:
Dim chk As CheckBox
chk = New CheckBox
Solved !
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
MsgBox("loading")
chk(0) = New CheckBox
With chk(0)
.ID = "chk(0)"
.Text = .ID
End With
Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text
End Sub

Run after Init, before Page_Load

After a PostBack caused by ddlPlant_SelectedIndexChanged, I need to set set HttpContext.Current.Session("PlantNumber"). This needs to happen after ddlPlant loads in Site.Master, but before the code in Default.aspx needs the value.
Public Class SiteMaster Inherits MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
If (Not Page.IsPostBack) Then
ddlPlant.DataSource = myDataSource
ddlPlant.DataBind()
ddlPlant.SelectedValue = "1"
End If
HttpContext.Current.Session("PlantNumber") = ddlPlant.SelectedValue
End Sub
Protected Sub ddlPlant_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPlant.SelectedIndexChanged
End Sub
End Class
Public Class _Default Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim units As New List(Of EquipmentModel)
For Each unit As EquipmentModel In getUnits.Out.Results
If (CStr(unit.Plant_ID) = HttpContext.Current.Session("PlantNumber")) Then
units.Add(unit)
End If
Next
gvEquipmentUnit.DataSource = units.OrderBy(Function(n) n.Equipment_ID)
gvEquipmentUnit.DataBind()
End Sub
With the code above, when Session("PlantNumber") is set after PostBack, ddlPlant.SelectedIndex = Nothing, and ddlPlant.SelectedValue is an empty string.
I've tried moving the Session("PlantNumber") = ddlPlant.SelectedValue line to Site.Master's Page_Load instead, but that runs after it is needed in Default.aspx.vb
I looked up PreLoad, but apparently it doesn't work for the Master page.
Ultimately, I decided not to use a Session variable, and instead call the control directly from any page that needs that value on load (almost all of them):
DirectCast(Master.FindControl("ddlPlant"), DropDownList).SelectedValue

How to provide a common style for DataGridView in VB.net

How to create a class or some other stuff to make all the DataGridView inside my project of same format i:e AlternativeRowColor, ForColor,BackColor and Other properties. Currently i have to go to each of the control property to set , it sucks when user requested to change and property of Grid as i have to change in all the DataGridView.
Public Class FrmArticle
Private Sub GridFormatting(ByVal DGV As DataGridView)
DGV.ForeColor = Color.Black
DGV.BackgroundColor = Color.AliceBlue
DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown
DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue
DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue
DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod
DGV.EnableHeadersVisualStyles = False
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GridFormatting(DataGridView1)
End Sub
End Class
or
Module GridFormat
Public Sub GridFormatting(ByVal DGV As DataGridView)
DGV.ForeColor = Color.Black
DGV.BackgroundColor = Color.AliceBlue
DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown
DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue
DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue
DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod
DGV.EnableHeadersVisualStyles = False
End Sub
End Module
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GridFormatting(DataGridView1)
End Sub

ReportViewer, vb.net, and PARAMETERS on Remote Server Report

I may seem dumb but this has had me going around in circles.
The report sits on the Report Server and requires ONE parameter "GROUPNAME". my code gives me a cast error when I try to set the parameters.
Please help:
Imports Microsoft.Reporting.webforms
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ReportParameter(0)
ReportViewerMain.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
ReportViewerMain.ServerReport.ReportServerUrl = New Uri("http://localhost/ReportServer")
ReportViewerMain.ServerReport.ReportPath = "/RptTest/RptTestParm"
ReportViewerMain.ShowParameterPrompts = True
ReportViewerMain.ShowPrintButton = True
Dim rptParameters As New ReportParameter(1)
rptParameters = New ReportParameter("GROUPNAME", "Adm01")
ReportViewerMain.ServerReport.SetParameters(rptParameters)
ReportViewerMain.ZoomPercent = 100
ReportViewerMain.ServerReport.Refresh()
End Sub
End Class
This is my code so far.
Thanks
Mac
your problem lies in your code itself
you have not properly instantiated the reportparameter array
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ReportParameter(0)
ReportViewerMain.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
ReportViewerMain.ServerReport.ReportServerUrl = New Uri("http://localhost/ReportServer")
ReportViewerMain.ServerReport.ReportPath = "/RptTest/RptTestParm"
ReportViewerMain.ShowParameterPrompts = True
ReportViewerMain.ShowPrintButton = True
Dim rptParameters As New ReportParameter(1)
RptParameters(0) = New ReportParameter("GROUPNAME", "Adm01")
ReportViewerMain.ServerReport.SetParameters(rptParameters)
ReportViewerMain.ZoomPercent = 100
ReportViewerMain.ServerReport.Refresh()
End Sub
you have not instantiated the array for the report parameters properly
thanks

using session state dont work properly

i am still new to using session state, i want to convert page name into and integer according to a database table
a function then compares "X" and "Y" to check if a user have the right to view this page
i know this is not the best way of managing website security, but it is like "training on how to use the session"
what have i done wrong
Partial Class advancedsearch
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Label1.Text = Session("username").ToString
Label3.Text = Session("role").ToString
Label4.Text = System.IO.Path.GetFileName(Request.Url.ToString())
Catch ex As Exception
Response.Redirect("login.aspx")
End Try
If Label1.Text = "" Then
Response.Redirect("login.aspx")
End If
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("default.aspx")
End Sub
End Class
try putting the comparison part in pre render complete
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub

Resources