Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2008, 10:18 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default Put them in a procedure...

Put the code into a procedure then call that procedure and pass the values to it. that way you can call it and have the procedure do the work..

Code:
Private Sub SaveData(ByVal AccessNumber As String, ByVal AssignedTo As String)
           Dim sqlCon As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("LocalSQLServer").ConnectionString)
           Dim sqlCmd As New System.Data.SqlClient.SqlCommand("update_assignreceivers")

       Try
            sqlCon.Open()
            sqlCmd.CommandType = CommandType.StoredProcedure
            sqlCmd.Parameters.AddWithValue("@AccessNumber", AccessNumber)
            sqlCmd.Parameters.AddWithValue("@AssignedTo", AssignedTo)
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery

       Finally
            sqlCmd.Dispose()
            sqlCmd = Nothing
            sqlCon.Close()
            sqlCon.Dispose()
            sqlCon = Nothing
       End Try
End Sub
Now you can just call this procedure and pass the values for each time you need it.

SaveData(RegularReceiver01.Text, DropDownList1.SelectedValue)
SaveData(RegulareReceiver02.Text, DropDownList1.SelectedValue)
__________________
Jason Hall

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
taboadar (December 17th, 2008)
 
Old December 17th, 2008, 10:21 AM
Authorized User
 
Join Date: Dec 2008
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default Calling a Stored Procedure more than once

Ok, I tryed this and works fine. However is crazy to do this for 70 times:

Dim sqlCon AsNew System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("LocalSqlServer").ConnectionString)
Dim sqlCmd AsNew System.Data.SqlClient.SqlCommand("update_assignreceivers")
Try
sqlCon.Open()
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue(
"@AccessNumber", RegularReceiver01.Text)
sqlCmd.Parameters.AddWithValue(
"@AssignedTo", DropDownList1.SelectedValue)

sqlCmd.Connection = sqlCon
sqlCmd.ExecuteNonQuery()

Catch ex As Exception
Response.Write(ex.Message)

Finally
sqlCmd.Dispose()
sqlCmd =
Nothing
sqlCon.Close()
sqlCon.Dispose()
sqlCon =
Nothing
EndTry

Dim sqlCon1 AsNew System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("LocalSqlServer").ConnectionString)
Dim sqlCmd1 AsNew System.Data.SqlClient.SqlCommand("update_assignreceivers")
Try
sqlCon1.Open()
sqlCmd1.CommandType = CommandType.StoredProcedure
sqlCmd1.Parameters.AddWithValue(
"@AccessNumber", RegularReceiver02.Text)
sqlCmd1.Parameters.AddWithValue(
"@AssignedTo", DropDownList1.SelectedValue)

sqlCmd1.Connection = sqlCon1
sqlCmd1.ExecuteNonQuery()

Catch ex As Exception
Response.Write(ex.Message)

Finally
sqlCmd1.Dispose()
sqlCmd1 =
Nothing
sqlCon1.Close()
sqlCon1.Dispose()
sqlCon1 =
Nothing
EndTry...
.....................
..............

Dim sqlCon70 AsNew System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("LocalSqlServer").ConnectionString)
Dim sqlCmd70 AsNew System.Data.SqlClient.SqlCommand("update_assignreceivers")

Try
sqlCon70.Open()
sqlCmd70.CommandType = CommandType.StoredProcedure
sqlCmd70.Parameters.AddWithValue(
"@AccessNumber", RegularReceiver02.Text)
sqlCmd70.Parameters.AddWithValue(
"@AssignedTo", DropDownList1.SelectedValue)

sqlCmd70.Connection = sqlCon70
sqlCmd70.ExecuteNonQuery()

Catch ex As Exception
Response.Write(ex.Message)

Finally
sqlCmd70.Dispose()
sqlCmd70 =
Nothing
sqlCon70.Close()
sqlCon70.Dispose()
sqlCon70 =
Nothing
EndTry

I know I need a FOR loop, How I will do it?


 
Old December 17th, 2008, 10:58 AM
Authorized User
 
Join Date: Dec 2008
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by alliancejhall View Post
Put the code into a procedure then call that procedure and pass the values to it. that way you can call it and have the procedure do the work..

Code:
Private Sub SaveData(ByVal AccessNumber As String, ByVal AssignedTo As String)
           Dim sqlCon As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("LocalSQLServer").ConnectionString)
           Dim sqlCmd As New System.Data.SqlClient.SqlCommand("update_assignreceivers")
 
       Try
            sqlCon.Open()
            sqlCmd.CommandType = CommandType.StoredProcedure
            sqlCmd.Parameters.AddWithValue("@AccessNumber", AccessNumber)
            sqlCmd.Parameters.AddWithValue("@AssignedTo", AssignedTo)
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery
 
       Finally
            sqlCmd.Dispose()
            sqlCmd = Nothing
            sqlCon.Close()
            sqlCon.Dispose()
            sqlCon = Nothing
       End Try
End Sub
Now you can just call this procedure and pass the values for each time you need it.

SaveData(RegularReceiver01.Text, DropDownList1.SelectedValue)
SaveData(RegulareReceiver02.Text, DropDownList1.SelectedValue)

So I will have something Like that:

scriptlanguage="VB"runat="server">

Sub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click

SaveData(RegularReceiver01.Text,DropDownList1.SelectedValue)
SaveData(RegularReceiver02.Text,DropDownList1.SelectedValue)
SaveData(RegularReceiver03.Text,DropDownList1.SelectedValue)
SaveData(RegularReceiver04.Text,DropDownList1.SelectedValue)
...........
SaveData(RegularReceiver70.Text,DropDownList1.SelectedValue)

End Sub

Private Sub SaveData(ByVal AccessNumber As String, ByVal AssignedTo As String)
Dim sqlCon As New System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("LocalSQLServer").Connect ionString)
Dim sqlCmd As New System.Data.SqlClient.SqlCommand("update_assignrec eivers")

Try
sqlCon.Open()
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@AccessNumber", AccessNumber)
sqlCmd.Parameters.AddWithValue("@AssignedTo", AssignedTo)
sqlCmd.Connection = sqlCon
sqlCmd.ExecuteNonQuery

Finally
sqlCmd.Dispose()
sqlCmd = Nothing
sqlCon.Close()
sqlCon.Dispose()
sqlCon = Nothing
End Try
End Sub
 
Old December 17th, 2008, 11:08 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default Even easier...

Would be to do a For Next loop....

Something like....

Code:
For i as integer = 1 to 70
  If i < 10 Then
     If Not String.IsNullOrEmpty(CType(Page.FindControl("Receiver0" & i.ToString), TextBox).Text) Then
         SaveData(CType(Page.FindControl("Receiver0" & i.ToString), TextBox).Text, DropDownList1.SelectedValue)
     End If
  Else
     If Not String.IsNullOrEmpty(CType(Page.FindControl("Receiver" & i.ToString), TextBox).Text) Then
        SaveData(CType(Page.FindControl("Receiver" & i.ToString), TextBox).Text, DropDownList1.SelectedValue)
     End If
  End if
Next
This will loop through the page and find each of the 70 textboxes then run the procedure if the text value is not blank.
__________________
Jason Hall

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
taboadar (December 17th, 2008)
 
Old December 17th, 2008, 11:09 AM
Authorized User
 
Join Date: Dec 2008
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by alliancejhall View Post
Would be to do a For Next loop....

Something like....

Code:
For i as integer = 1 to 70
  If i < 10 Then
     If Not String.IsNullOrEmpty(CType(Page.FindControl("Receiver0" & i.ToString), TextBox).Text) Then
         SaveData(CType(Page.FindControl("Receiver0" & i.ToString), TextBox).Text, DropDownList1.SelectedValue)
     End If
  Else
     If Not String.IsNullOrEmpty(CType(Page.FindControl("Receiver" & i.ToString), TextBox).Text) Then
        SaveData(CType(Page.FindControl("Receiver" & i.ToString), TextBox).Text, DropDownList1.SelectedValue)
     End If
  End if
Next
This will loop through the page and find each of the 70 textboxes then run the procedure if the text value is not blank.
THANK YOU WORKS FINE.!!!!!!!
 
Old December 17th, 2008, 11:10 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default That was quick!

Glad to hear it's working....
__________________
Jason Hall

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
taboadar (December 17th, 2008)
 
Old December 17th, 2008, 12:08 PM
Authorized User
 
Join Date: Dec 2008
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by alliancejhall View Post
Glad to hear it's working....
Thank you, now my all 70 AccessNumbers are updating the DB. Now I have to print a report or receipt of those access numbers was given that assingneddate and that assignTo Person. How I retrieve what I just updated to be output and ready to be print?

Roberto
 
Old December 17th, 2008, 01:06 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default There are a few ways...

You could query the database and retrieve all those newly added rows based on the DropDownList1.SelectedValue and then Write it to the page or as you are saving the data you can store the value into a collection or array list then loop through that list and write the value to the page.

I think the Arraylist might be a little quicker and easier but that's up to you...

If you do want to query the db and get the exact values, because with the array list you'll have to add the date manually to the list since you don't add it in the sub procedure you add it in the stored procedure it won't show the exact time that was put in to the db, you'll have to use an Identifier and i'm assuming the drop down will be that identifier. again i think the arraylist is the best way if you don't care about the time and just the date part.

let us know which way you want to go.
__________________
Jason Hall

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
taboadar (December 17th, 2008)
 
Old December 17th, 2008, 03:59 PM
Authorized User
 
Join Date: Dec 2008
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

You know what I did, since in my page everytime, when I click the save button the information stays there, it does not clear it, I added a java script print page button and also I added a time and date label. So when I press the save button everything stays there plus time and date and then I press the print button. Good or bad, but I works!!

Thank You





Similar Threads
Thread Thread Starter Forum Replies Last Post
calling stored procedure jomet JSP Basics 0 November 23rd, 2007 08:06 AM
Calling on a database within a stored procedure Hadware SQL Language 1 January 8th, 2007 05:11 PM
Calling Stored Procedure Using vc++ senthil_mano Visual C++ 0 August 30th, 2006 12:20 AM
Calling Stored Procedure with parameters zarina_24 Classic ASP Professional 4 March 2nd, 2006 11:57 AM
Calling an Oracle Stored Procedure booksnore2 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 2 October 1st, 2004 09:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.