Showing posts with label sub. Show all posts
Showing posts with label sub. Show all posts

Thursday, March 29, 2012

Pass Parameter As DataGridItemEventArgs

A Form has a DataGrid & a Button. The DataGrid's ItemDataBound event
calls a sub named 'BindData'. This sub first finds a Label which exists
in the ItemTemplate of the TemplateColumn of the DataGrid & does some
work with the Label.
Sub BindData(obj As Object, ea As DataGridItemEventArgs)
Dim lbl As Label
If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
lbl = ea.Item.FindControl("lblAcre")
...
...
End If
End Sub
The Button has the Click event which invokes a sub named 'SubmitPage'.
Sub SubmitPage(obj As Object, ea As EventArgs)
...
End Sub
Now I did like the 'SubmitPage' sub to invoke the 'BindData' sub. How
do I accomplish this? In other words, what parameters do I pass from
the 'SubmitPage' sub to the 'BindData' sub which the latter expects?You can't get DataGridItemEventArgs in SubmitPage (it is event argument data
type specific to a event, so it's not even wise to use as input to a
method), but you can loop through the DataGrid manually.
Sub SubmitPage(obj As Object, ea As EventArgs)
For each dgitem As DataGridItem in DataGrid1.Items
Dim lbl As Label=Nothing
If (dgitem.ItemType = ListItemType.Item Or dgitem.ItemType =
ListItemType.AlternatingItem) Then
lbl = dgitem.FindControl("lblAcre")
'Do something with the Label
End If
Next
End Sub
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<rn5a@.rediffmail.com> wrote in message
news:1165068166.263744.157190@.l12g2000cwl.googlegroups.com...
>A Form has a DataGrid & a Button. The DataGrid's ItemDataBound event
> calls a sub named 'BindData'. This sub first finds a Label which exists
> in the ItemTemplate of the TemplateColumn of the DataGrid & does some
> work with the Label.
> Sub BindData(obj As Object, ea As DataGridItemEventArgs)
> Dim lbl As Label
> If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
> ListItemType.AlternatingItem) Then
> lbl = ea.Item.FindControl("lblAcre")
> ....
> ....
> End If
> End Sub
> The Button has the Click event which invokes a sub named 'SubmitPage'.
> Sub SubmitPage(obj As Object, ea As EventArgs)
> ....
> End Sub
> Now I did like the 'SubmitPage' sub to invoke the 'BindData' sub. How
> do I accomplish this? In other words, what parameters do I pass from
> the 'SubmitPage' sub to the 'BindData' sub which the latter expects?
>
Thanks, Teemu...that's exactly what I was looking out for
Teemu Keiski wrote:
> You can't get DataGridItemEventArgs in SubmitPage (it is event argument da
ta
> type specific to a event, so it's not even wise to use as input to a
> method), but you can loop through the DataGrid manually.
> Sub SubmitPage(obj As Object, ea As EventArgs)
>
> For each dgitem As DataGridItem in DataGrid1.Items
> Dim lbl As Label=Nothing
> If (dgitem.ItemType = ListItemType.Item Or dgitem.ItemType =
> ListItemType.AlternatingItem) Then
> lbl = dgitem.FindControl("lblAcre")
> 'Do something with the Label
> End If
> Next
> End Sub
>
> --
> Teemu Keiski
> ASP.NET MVP, AspInsider
> Finland, EU
> http://blogs.aspadvice.com/joteke
> <rn5a@.rediffmail.com> wrote in message
> news:1165068166.263744.157190@.l12g2000cwl.googlegroups.com...

Pass Parameter As DataGridItemEventArgs

A Form has a DataGrid & a Button. The DataGrid's ItemDataBound event
calls a sub named 'BindData'. This sub first finds a Label which exists
in the ItemTemplate of the TemplateColumn of the DataGrid & does some
work with the Label.

Sub BindData(obj As Object, ea As DataGridItemEventArgs)
Dim lbl As Label

If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
lbl = ea.Item.FindControl("lblAcre")
....
....
End If
End Sub

The Button has the Click event which invokes a sub named 'SubmitPage'.

Sub SubmitPage(obj As Object, ea As EventArgs)
....
End Sub

Now I did like the 'SubmitPage' sub to invoke the 'BindData' sub. How
do I accomplish this? In other words, what parameters do I pass from
the 'SubmitPage' sub to the 'BindData' sub which the latter expects?You can't get DataGridItemEventArgs in SubmitPage (it is event argument data
type specific to a event, so it's not even wise to use as input to a
method), but you can loop through the DataGrid manually.

Sub SubmitPage(obj As Object, ea As EventArgs)

For each dgitem As DataGridItem in DataGrid1.Items

Dim lbl As Label=Nothing

If (dgitem.ItemType = ListItemType.Item Or dgitem.ItemType =
ListItemType.AlternatingItem) Then
lbl = dgitem.FindControl("lblAcre")
'Do something with the Label
End If

Next

End Sub

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<rn5a@.rediffmail.comwrote in message
news:1165068166.263744.157190@.l12g2000cwl.googlegr oups.com...

Quote:

Originally Posted by

>A Form has a DataGrid & a Button. The DataGrid's ItemDataBound event
calls a sub named 'BindData'. This sub first finds a Label which exists
in the ItemTemplate of the TemplateColumn of the DataGrid & does some
work with the Label.
>
Sub BindData(obj As Object, ea As DataGridItemEventArgs)
Dim lbl As Label
>
If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
lbl = ea.Item.FindControl("lblAcre")
....
....
End If
End Sub
>
The Button has the Click event which invokes a sub named 'SubmitPage'.
>
Sub SubmitPage(obj As Object, ea As EventArgs)
....
End Sub
>
Now I did like the 'SubmitPage' sub to invoke the 'BindData' sub. How
do I accomplish this? In other words, what parameters do I pass from
the 'SubmitPage' sub to the 'BindData' sub which the latter expects?
>


Thanks, Teemu...that's exactly what I was looking out for

Teemu Keiski wrote:

Quote:

Originally Posted by

You can't get DataGridItemEventArgs in SubmitPage (it is event argument data
type specific to a event, so it's not even wise to use as input to a
method), but you can loop through the DataGrid manually.
>
Sub SubmitPage(obj As Object, ea As EventArgs)
>
>
For each dgitem As DataGridItem in DataGrid1.Items
>
Dim lbl As Label=Nothing
>
If (dgitem.ItemType = ListItemType.Item Or dgitem.ItemType =
ListItemType.AlternatingItem) Then
lbl = dgitem.FindControl("lblAcre")
'Do something with the Label
End If
>
Next
>
End Sub
>
>
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
>
<rn5a@.rediffmail.comwrote in message
news:1165068166.263744.157190@.l12g2000cwl.googlegr oups.com...

Quote:

Originally Posted by

A Form has a DataGrid & a Button. The DataGrid's ItemDataBound event
calls a sub named 'BindData'. This sub first finds a Label which exists
in the ItemTemplate of the TemplateColumn of the DataGrid & does some
work with the Label.

Sub BindData(obj As Object, ea As DataGridItemEventArgs)
Dim lbl As Label

If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
lbl = ea.Item.FindControl("lblAcre")
....
....
End If
End Sub

The Button has the Click event which invokes a sub named 'SubmitPage'.

Sub SubmitPage(obj As Object, ea As EventArgs)
....
End Sub

Now I did like the 'SubmitPage' sub to invoke the 'BindData' sub. How
do I accomplish this? In other words, what parameters do I pass from
the 'SubmitPage' sub to the 'BindData' sub which the latter expects?

Monday, March 26, 2012

Pass the values to the sub..

I am not sure how to pass the value to use another sub..

i want to pass these two value to another sub(passvalue ())..

Sub page_load(source as object, E as eventArgs)
strvalue1="value1"
strvalue2="value2"

passvalue()

end sub

sub passvalue()

xxxxxxxxx
end sub


Sub page_load(source as object, E as eventArgs)
strvalue1="value1"
strvalue2="value2"

passvalue(strvalue1, strvalue2)

end sub

sub passvalue(byval arg1 as string, byval arg2 as string, etc...)
xxxxxxxxx
end sub

Pass value to function

Hello,
I have this function:
Sub Add_Menu_Item(ByVal Id As String, ByVal Text() As String)
And I am calling it this way:
Add_Menu_Item("home", {"contact","contacts"})
I get this error:
Compiler Error Message: BC30201: Expression expected.
Why?
Thanks,
Miguel
You have to use it like this:
Add_Menu_Item("home", new string() {"contact","contacts"})

Saturday, March 24, 2012

Pass variable between sub routines

For example, I have the following code:

<%@dotnet.itags.org. Page Language="VB" Debug="True" %>
<script language="vb" runat="server">
Dim var As String

Sub button1_click(sender As object, e As EventArgs)
var = "one"
Response.Write(var)
End Sub

Sub button2_click(sender As object, e As EventArgs)
Response.Write(var)
End Sub
</script>
<html>
<head>
<title>Untitled Document</title>
</head
<body>
<form runat="server">
<asp:Button ID="button1" OnClick="button1_click" Text="one" runat="server"></asp:Button>
<asp:Button ID="button2" OnClick="button2_click" Text="two" runat="server"></asp:Button>
</form>
</body>
</html
The problem is when I click button 2 it response.writes nothing. It should spit out "one". Is there anyway I can share variables between multiple subs?When you click button2, the page is reloaded and "var" is redecleared and the button1 value is gone. You may need use ViewState or Session State to keep the value when page reload or redirect to other page.

Pass variable between sub routines

Hi,

I'm trying to write some code that will create an order number by searching through an order_detail table for the highest numbered order number. Once found, I would like to pass this number to the FillOrder sub routine, which inserts it into the order_detail table. It looks like the findMax routine works, since the response.write statement a the end of it prints out the correct number. However, the response.write statements in the fillorder routine do not display, nor does the table get populated. I think the first problem is that I may not be passing the "highest" variable correctly.

I know the code is probably hack, but I would appreciate help/advice.

Thanks in advance.

Tom


Public Sub findMax()
Dim highest As Int32
Dim strMax As String
Const strMaxConn As String = "data source=ENVISAGENT1;initial catalog=tsproductsSQL3;integrated security=SSPI;persist security info=False;workstation id=ENVISAGENT1;packet size=4096"
Dim objMaxConn As New SqlConnection(strMaxConn)
Dim dsOrderHead As DataSet
Dim daOrderHead As SqlDataAdapter
dsOrderHead = New DataSet()

strMax = "SELECT MAX(order_num) as highest FROM order_header"
Dim CmdMax As New SqlCommand(strMax, objMaxConn)

If numOrders = 0 Then
highest = numOrders + 1
End If

If numOrders > 0 Then
objMaxConn.Open() 'open connection

'run SQL command
CmdMax.ExecuteNonQuery()

objMaxConn.Close() 'close connection
End If
Response.Write(highest)
FillOrder(highest)
End Sub

'populate order_detail table
Public Sub FillOrder(ByVal ordNum As Int32)
'findMax()

Const strFillOrdConn As String = "data source=ENVISAGENT1;initial catalog=tsproductsSQL3;integrated security=SSPI;persist security info=False;workstation id=ENVISAGENT1;packet size=4096"
Dim objFillOrdConn As New SqlConnection(strFillOrdConn)
Dim daFillOrd As SqlDataAdapter
Dim strFillOrdSQL As String

Dim detNum As Int32 = 0
Response.Write("DetailNum " & detNum)
Response.Write("PRoduct " & grdCart.Items(1).Cells(1).Text)

strFillOrdSQL = "INSERT order_num,detail_num,product_num,quantity,line_item_price INTO order_detail values (ordNum,detNum,pnum,quan,price)"

Dim CmdFillOrd As New SqlCommand(strFillOrdSQL, objFillOrdConn)

objFillOrdConn.Open()

' Iterate through all rows within shopping cart list
Dim i As Integer

For i = 0 To grdCart.Items.Count - 1
'for each item in cart,
'insert current order #, detail#,product, quantity & price
'into order_detail

detNum = detNum + 1

Dim pnum As Int32 = Convert.ToInt32(grdCart.Items(i).Cells(1).Text)
Dim price As Decimal = Convert.ToDecimal(grdCart.Items(i).Cells(3).Text)
Dim quan As Int32 = Convert.ToInt32(grdCart.Items(i).Cells(4).Text)

CmdFillOrd.ExecuteNonQuery()

Next

objFillOrdConn.Close()

Hi Tom,
There is no embeded variables in ADO.NET. You have to create command with parameters and use them. Alter your code this way (check types of parameters):

' ...
strFillOrdSQL = "INSERT order_num,detail_num,product_num,quantity,line_item_price INTO order_detail values (@.ordNum,@.detNum,@.pnum,@.quan,@.price)"

Dim CmdFillOrd As New SqlCommand(strFillOrdSQL, objFillOrdConn)
CmdFillOrd.Parameters.Add(New SqlParameter("@.ordNum", SqlDbType.Int))
CmdFillOrd.Parameters.Add(New SqlParameter("@.detNum", SqlDbType.Int))
CmdFillOrd.Parameters.Add(New SqlParameter("@.pnum", SqlDbType.Int))
CmdFillOrd.Parameters.Add(New SqlParameter("@.quan", SqlDbType.Int))
CmdFillOrd.Parameters.Add(New SqlParameter("@.price", SqlDbType.Decimal))

CmdFillOrd.Parameters[0].Value = ordNum
objFillOrdConn.Open()

' Iterate through all rows within shopping cart list
Dim i As Integer

For i = 0 To grdCart.Items.Count - 1
'for each item in cart,
'insert current order #, detail#,product, quantity & price
'into order_detail

detNum = detNum + 1
CmdFillOrd.Parameters[1].Value = detNum

Dim pnum As Int32 = Convert.ToInt32(grdCart.Items(i).Cells(1).Text)
Dim price As Decimal = Convert.ToDecimal(grdCart.Items(i).Cells(3).Text)
Dim quan As Int32 = Convert.ToInt32(grdCart.Items(i).Cells(4).Text)

CmdFillOrd.Parameters[2].Value = pnum
CmdFillOrd.Parameters[3].Value = quan
CmdFillOrd.Parameters[4].Value = price

CmdFillOrd.ExecuteNonQuery()
Next


Thank-you. I'll give it a try.
Ok I finally got around to trying the code - it looks like it may work, but again, nothing happens after the findMax sub routine listed above. It is as if it isn't even invoking. Also, I took the response.write("Highest = " & highest) statement out, and it still displays the text when the button is clicked. Why does this persist even though the statement is out? Does it also have something to do with the FillOrder sub routing apparantly doing nothing?

Thanks

Tom
There are only two possible explanations for persisting results of response.write:
1. Page is cached and not executed at all
2. You are running wrong version of page
How would I determine which one? I've tried deleting cache files to no avail.
Try to change something visual on page (add some text for example) and see if you receive changes when page is served.
I did all of that.

If you look at the connection strings in the code above, are they creating a cache that persists?

I created new pages and copied the code over to them. I changed the connection strings to "server=localhost;database=tsproductssql3;uid=sa;pwd=sa" and ran it. I got rid of the persistent response.write, and was able to display all of the others. However, I tried again, and got a database (primary key) violation which I have to sort out to see if the problems persist. I'll re-post then.

Thanks for your replies.

Tom
OK, cleared up the persist problem.

If you (or someone) could tell me if the original connection string (above) that I had would have created this cache & persist problem, that would be great. I'm still getting a lot of roadrash fumbling my way through my first ASP.NET project.

Thanks

Tom