How can I pass a variable value from one sub to another - asp.net

I am new to the programming world. I have got the chance to work and develop in a project built with vb.net and asp.net.
In my coding I have stored a value in a variable under one sub btn_Click(). I want to use that variable value from another sub - AddCategory(). Both are written under same class.
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim sale = Guid.NewGuid().ToString()
Dim SaleID As String = sale
Core.DB.Insert("insert into survey(id,title, detail) values(#id,#title, #detail);", Core.DB.SIP("title", Title), Core.DB.SIP("detail", Detail), Core.DB.SIP("id", sale))
Call AddCategory1()
Response.Redirect("profile.aspx")
End
As you can see in the above code the ID value for sale table is a GUID value and I am converting into a string to store it one variable SaleID.
And you can also see I have called AddCategory1(). Now I want to pass the value of SaleID (string) into AddCategory(). And I want to do another insert query to category table under AddCategory()
Private Sub AddQuestionCategory1()
Core.DB.Insert("insert into SaleCategory(title, detail, saleid) values(#title, #detail, #sid)", Core.DB.SIP("title", CategoryTitle), Core.DB.SIP("detail", CategoryDetail), Core.DB.SIP("sid", sale))
End Sub
To be able to do so I have to pass the value stored under the variable SaleID.
How can I do that?

The usual way to pass a value to a method (Sub or Function) is to make it an argument. Set up the method to expect the SaleID as an argument and then call the method with that argument.
Private Sub AddCategory1(SaleID as String)
'You can use SaleID in your code
End Sub
Call this sub from your Click handler like this
AddCategory1(SaleID)

Related

Calling Methods dynamically

I am trying to call methods dynamically and I have issues. Could some one please help
I have the following vb code
Module
Sub Main
if user-input = RP1 then
createRP1()
elseif user-input = RP2 then
createRP2()
end if
end sub
sub createRP1()
...
end sub
sub createRP2()
,...
end sub
End Module
The CreateRP1/CreateRP2 method does not have any arguments. There are some n number of reports. So I do not want to write all those if or switch conditions for this. I want to write some thing simple so I tried this
1 Dim type As Type = "["GetType"]"()
2 Dim method As MethodInfo = type.GetMethod("Create" + user-input)
3 If method IsNot Nothing Then
4 method.Invoke(Me, Nothing)
5 End If
Line 1 and 4 are not working
Line 4 is not working because "me" does not go with module. But how to rewrite 1? I saw this somewhere in StackOverflow site
You can get the Type of the current Module like this:
Dim myType As Type = MethodBase.GetCurrentMethod().DeclaringType
Since it's a Module, all of the methods are essentially Shared (e.g. static, non-instance methods), so you can just pass Nothing for the obj parameter of the MethodInfo.Invoke method:
Dim methodName As String = "Create" & userInput
Dim method As MethodInfo = myType.GetMethod(methodName)
method.Invoke(Nothing, Nothing)
However, rather than using reflection, you may also want to consider using a dictionary of delegates so that it would be more deterministic and type-checked at compile time. For instance:
Dim methods As New Dictionary(Of String, Action)
methods.Add("RP1", AddressOf CreateRP1)
methods.Add("RP2", AddressOf CreateRP1)
' ...
methods(userInput).Invoke()
I think the best solution would be to create a "Report" object through designing a class. Then you could easily make things on the fly.
function (byRef user-input As String) As Report
if not "s" & user-input = "s"
Dim user-report As Report = new Report(user-input)
end if
end function
Then your Report class
Private reportNumber As String
Private data As String
Public Sub New(byVal reportNumber As String)
Sub createRP(byRef repotNumber As String)
// use report # here to select the correct data...
// so if it was sql....
// "SELECT data FROM report_table where report_num =" & reportNumber
end Sub
end Sub

How to share a variable value with a webmethod?

I have this code:
Dim main_id As int
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
main_id =1
End Sub
<WebMethod()> _
Public Shared Function BindPersonnel(ByVal product_id As String) As String
Dim project_id AS int16
project_id=main_id 'this doesn't work
End Function
On page load, I set the value of variable main_id and I need a way to somehow share the main_id with webmethod function, how can this be done? I have not tried session variable and I don't want to use session variable. I found this link Accessing a common variable in WebMethod and jQuery but I can't figure out how this is going to solve my problem. I also read some posts about using hidden field, which requires me to go two trips. I would love to avoid that.
WebMethods are independent of the other variables on the page. If you want to access main_id, you could declare it as Private Shared main_id As Integer, but that would then cause all of your users to have access to the same ID value, which you probably don't want.
Perhaps the easiest way to do this is to store the value in SessionState, and enable sessionstate access in the WebMethod. On the other hand, this removes the asynchronous-like functionality that you are looking for (it may not be an issue).
SessionState will give you the ability to have a per-session value (avoiding the use of the Shared solution mentioned above).
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("main_id") = 1
End Sub
<WebMethod(EnableSession:=True)>
Public Shared Function BindPersonnel(ByVal product_id As String) As String
Dim project_id As Integer = CInt(HttpContext.Current.Session("main_id"))
End Function

Creating an comma-separated array in vb only shows last value of array

I have a problem and any help would be greatly appreciated. I have a comma-separated array I am using to obtain the selected value of a dropdown list. The problem is that that the array is only displaying the last value.
Here is my code:
Dim values As String
values = "RO,RW"
Dim sites As String() = Nothing
sites = values.Split(",")
Dim s As String
For Each s In sites
emptytext.SelectedItem.Text = s
Next
Setting the variable s always to the same item results in the behavior described.
Probably you want something like this (Supposing that emptyText is a combobox or listbox of sort)
emptytext.Items.Clear()
For Each s In sites
emptytext.Items.Add(s)
Next
You commented that you have two dropdowns, and you want the values in the comma-separated list to be the selected items in the dropdowns. So you will need to go through the items and dropdowns in turn and set the selecteditem of each.
I made a Windows Forms project form looking like this:
(the controls all have their default names).
Then I used the following code:
Public Class Form1
Dim ddls() As ComboBox
Private Sub SetUpDropdowns()
Dim values() As String = "RO,RW".Split(","c)
ddls = {ComboBox1, ComboBox2}
For Each ddl In ddls
ddl.Items.AddRange(values)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim settings = TextBox1.Text.Split(","c)
If settings.Count > ddls.Count Then
MsgBox(String.Format("Too many setting values entered, maximum is {0}.", ddls.Count))
Exit Sub
End If
For i = 0 To settings.Count - 1
ddls(i).SelectedItem = settings(i)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetUpDropdowns()
End Sub
End Class
[Edited: use .AddRange in the setup instead of .Add in a loop.]
I entered RO,RW in the TextBox and clicked Button1 to get this:

Multiple ascx (user controls) that use the same server side code

I currently have multiple user controls in a default page. Each one has a different grid that is going to become a dashboard. However to cut down on repeating code, some of the controls use the same datasource. Is there anyway to have them all share the same datasource. Or if necessary all use the same OnSelecting statement that I could define elsewhere?
Such as one of the LinqDataSources selects all the users in a group, this displays them all in a gridview. However a detailed gridview below that in a separate ascx control uses the same data, but displays it in another way, grouping differently etc. However they both (if placed on one page) use the same datasource. If there anyway for them to use the same selecting Sub somehow? Maybe part of a class somewhere else in the code?
Ok after further research into alternate ways I've decided to place my select on the default page:
Public Function returnGroup() As List(Of Group)
'Use standard LINQ to SQL function to get list of what is needed
End Function
This has been used in combination with the Structure:
Structure Group
Public Property Title As String : Public Property GroupID As Integer
Public Property GroupCode As String
End Structure
So when combined it creates:
Public Function returnGroup() As List(Of Group)
'Use standard LINQ to SQL function to get list of what is needed
Return LINQToSQL_Select.Select(Function(chX) New Group With {.Title = chX.Title} '... etc
End Function
This is then called in the ascx page:
Protected Sub Page_Load(ByVAl sender as object, ByVal e as eventargs) Handles Me.Load
Dim cDef as New _Default
gvGroup.DataSource = cDef.returnGroup() : gvGroup.DataBind()
End Sub

populate multiple dropdownlists with same data

I need to populate 12 asp:dropdownlist 's with the same 0-9 options. (ASP.NET VB)
Now I could just populate them manually, but I wandered if there is an easier method... Can i populate an Array and use that to populate them all?
I thought I could write a small function that you pass the DDL's name into, but how then could I use that function input string as the dropdownlist name?
I know this is simple stuff but its something Ive not needed to do before, and cant see a simple method to do it.
Try this
Protected Sub Page_Load(ByVal sender as Object, ByVal e As EventArgs)
If (Not Page.IsPostBack) Then
BindDropdown(list1)
BindDropdown(list2)
BindDropdown(list3)
End If
End Sub
Private Sub BindDropdown(ByVal list As DrodownList)
Dim items As String() = {"0","1","2","3","4","5","6","7","8","9" }
list.DataSource = items
list.DataBind()
End Sub
Add this to cache and make a common helper function to bind the dropdown

Resources