Random Notes and simple code on Developing Web Sites (Applications) in ASP.net using C# #19

Random Notes and simple code on Developing Web Sites (Applications) in ASP.net using C#

  • The information below primarily apply to the web page model of web-development in C#
  • _PageStart.cshtml runs at each page start/open
  • _AppStart.cshtml runs at the beginning of the application
  • Syntax to use the Layout file: @{Layout = “/Shared/Layout.cshtml”;}
  • if you use this – @{Layout = /Shared/Layout.cshtml”;} in the _PageStart.cshtml file, you will not need to use it at the top of each file
  • You can use Microsoft Webmatrix, Visual Web Developer, or Visual Studio for web development
  • Webmatrix, includes IIS Express, and also a database workspace where you can create and use compact SQL Server Databases
  • DateTime.Now : will display current date and time
  • You can connect to a database using var db = Database.Open(“db_name”);
  • db = Database.Open(“db_name”); will look for the database in the same workspace; if not found will use the connection parameter as defined in the web.config file to find and connect to database
  • Executing a query: var queryString = “SELECT * FROM Product ORDER BY Name”; db.Query(queryString)
  • Accessing each row from a rowset of data as returned from database:
                   foreach(var entry in db.Query((queryString))){                @entry.Id, @entry.Name            }               
  • if (IsPost) : will check if a form is submitted by POST or not
  • username = Request.Form[“username”]; Will retrieve username from the Request object [submitted form with control named username]
  • A basic login authentication code can be as follows
               var username = "";      var password = "";      var ErrorMessage = "";              // If this is a POST request, validate and process data     if (IsPost){                        username = Request.Form["username"];            password = Request.Form["password"];                        if (username.IsEmpty() || password.IsEmpty()){              ErrorMessage = "You must specify a username and password.";         }else{              // Login, Navigate back to the homepage and exit                if (WebSecurity.Login(username, password, false)){                  Response.Redirect("~/");                }               else{                   ErrorMessage = "Login failed";              }           }       }                                       @if (ErrorMessage!="") {            @ErrorMessage, Please correct the errors and try again.     }   
  • Execute an Insert Statement: db.Execute(“INSERT INTO UserProfile (Email) VALUES (@0)”, email);
  • WebSecurity object can be used for the security of your web-site (application) such as user authentication, block a page until a user is authenticated
  • Block user if he is not logged in [can be added at the top of the page to be blocked]
               @{              if (!WebSecurity.IsAuthenticated) Response.Redirect("~/Login"); //redirect to login page            }       
  • WebSecurity.InitializeDatabaseConnection(“db_name”, “profile_of_the_users_table”, “UserId”, “Email”, true);
  • WebSecurity.CreateAccount(email, password, false);

From: http://sitestree.com/?p=5298
Categories:19
Tags:
Post Data:2012-01-30 09:33:18

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Automating Digital Delivery with Paypal Payment Processing System #19

In the past, I implemented an automatic notification and digital product delivery system with Paypal Payment Processing System. The concept is, when a person buys products from your web-sites, he gets his products automatically as email attachments after you have confirmed the transaction. The most important part is – collecting payment data and buyer information from Paypal to ensure that the transaction is legitimate and the payment really went through. You can use either Paypal IPN or Paypal PDT to collect these data and email products on successful verification. Either will/should work. For me, PDT worked alright. Check the following web-pages/documents. Also, from PDT section, check the sample examples. For me the sample example with ASP/VBSCript worked alright.

In the past, I have worked with MiraServ, and Moneris payment processing systems. You can find short-notes and video tutorials in this web-site on them. Just search through our web-sites.

A relevant code in ASP is provided below. The code collects the payer’s information just after someone has paid. The code also checks, if the payment was successful, if so prints some details about the transaction and the payer

The flow of actions (in terms of code and operations)

  • Create a paypal button, provide return url for successful payment and canceled payment.
  • In your successful payment page, write down the following code
  • Your paypal account needs to be configured properly to make the following code work – consult the documents as listed above
  • You need to use the authentication code from your paypal account to/in the following code
  • The follwing code section
    authToken = "ytrtyrtr45654hgfhgfjhfsfdsfdsfds;ljlk" 'auth code for your paypal account txToken = Request.Querystring("tx") 'as sent from paypal query = "cmd=_notify-synch&tx=" & txToken & "&at=" & authToken set objectHttp = Server.CreateObject("Microsoft.XMLHTTP")   objectHttp.open "POST", "http://www.paypal.com/cgi-bin/webscr", false objectHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded" objectHttp.Send query

    submits to the paypal to grab transaction details. (remember, after payment the control is required to come to this page, then based on authentication code and tx-id, the code grabs detail transaction information)

< %      Dim query   Dim objectHttp  Dim strQuerystring  Dim i, result   Dim firstName, lastName, itemName, curGross, mcCurrency, email  Dim authToken, txToken  Dim strParts, intParts, aParts  Dim strResults, strKey, strValue        authToken = "ytrtyrtr45654hgfhgfjhfsfdsfdsfds;ljlk" 'auth code for your paypal account   txToken = Request.Querystring("tx") 'as sent from paypal     query = "cmd=_notify-synch&tx=" & txToken & "&at=" & authToken  set objectHttp = Server.CreateObject("Microsoft.XMLHTTP") objectHttp.open "POST", "http://www.paypal.com/cgi-bin/webscr", false    objectHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded" objectHttp.Send query   strQuerystring = objectHttp.responseText    If Mid(strQuerystring,1,7) = "SUCCESS" Then       strQuerystring = Mid(strQuerystring,9)      strParts = Split(strQuerystring, vbLf)      intParts = UBound(strParts) - 1     ReDim strResults(intParts, 1)       For i = 0 To intParts           aParts = Split(strParts(i), "=")          strKey = aParts(0)          strValue = aParts(1)            strResults(i, 0) = strKey           strResults(i, 1) = strValue         Select Case strKey              Case "first_name"                 firstName = strValue                Case "last_name"                  lastName = strValue             Case "item_name"                  itemName = strValue             Case "mc_gross"                   curGross = strValue             Case "mc_currency"                    mcCurrency = strValue               Case "payer_email"                    email = strValue                    email = Replace(email, "%40", "@")          End Select      Next        email = Replace(email, "%40", "@")      Response.Write("")        'send email to customer     Dim myObject        Set myObject = Server.CreateObject("CDO.Message")       'set the To and From        myObject.To = email     myObject.From = "webmaster@justetc.net"     myObject.Bcc = "sayed@justetc.net"              myObject.Subject = "Please download your product"       myObject.TextBody = "Dear Customer, Thank you for purchasing from us. Download from the link belownn"       myObject.AddAttachment "http://www.justetc.net/test.pdf"     myObject.Send()     'Set objects to "nothing" to free up the computer memory        Set myObject = nothing      'send email to the seller       Set myObject = Server.CreateObject("CDO.Message")       'set the To and From properties     myObject.To = "webmaster@justetc.net"       myObject.From = "webmaster@justetc.net"     myObject.Bcc = "sayed@justetc.net"              myObject.Subject = "You got one customer: Product emailed to the customer"      myObject.TextBody = "Customer email address: " & email              myObject.Send()     'Set your objects to "nothing" to free up the computer memory       Set myObject = nothing      Else        'log        Response.Write("ERROR") End If%>

We have received your order") Response.Write("Details
") Response.Write("
  • Name: " & firstName & " " & lastName & "
  • ") Response.Write("
  • Description: " & itemName & "
  • ") Response.Write("
  • Amount: " & mcCurrency & " " & curGross & "
  • ") Response.Write("
  • Email: " & email & "
") Response.Write("
Home Page

From: http://sitestree.com/?p=5281
Categories:19
Tags:
Post Data:2006-10-25 18:41:19

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Code: Email Sending using ASP #19


From: http://sitestree.com/?p=5280
Categories:19
Tags:
Post Data:2012-11-16 11:05:08

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Formatting Web-application output #19

  • You can apply CSS styles to format web-application output
  • If you have the same styles defined in multiple places (for the same element), the styles defined closest to the element will be applied. The order, inline, page, global
  • You can create classes that can be applied to multiple elements
  • Use the CssStyle/CssStyle attribute to apply a style class to a server control
  • use the class attribute to apply a style class to an HTML element
  • Yes, you need to create a style file, and link to it from your web-form
  • You can click the style property of an element or the style property/attribute of the form to bring the Style Wizard to define/modify existing styles
  • You can also use the ‘Add Style Rule’ option in the popup menu on the .css files to define new styles. Also, use the ‘Synchronize Document Elements’ from the popup menu if required

From: http://sitestree.com/?p=5234
Categories:19
Tags:
Post Data:2009-07-01 02:45:13

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Providing Help in an ASP.Net Web-application #19

  • Use the Tooltip attribute of the ASP.Net controls to display help in Tooltip text
  • You can create HTML pages to provide/show help. Just as a simple web-site
  • You can use Web Forms to display help information
  • You can display help using HTML Help Viewer
  • You can use the HTML Help Workshop as comes with the Visual Studio to create Help projects similar to the generic windows help. Use the workshop and create a help project. Compile the help project into one .chm file. Use the ShowHelp method of the Window object to open this file (as a help file). Such help files usually have three tabs such as Contents, Index, and Search.

From: http://sitestree.com/?p=5233
Categories:19
Tags:
Post Data:2010-06-14 05:19:10

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

.Net: Globalizing Web Applications #19

Three ways of globalizing web-applications

  • Create one for each culture/language – usually the web-interfaces. Redirect to a web-site built based on the current culture.
  • Write one web-application but detect current culture, then change the user interface (texts, date time formats) to represent the current culture – dynamically
  • Satellite Based: Use resource files to keep culture specific messages. One resource file for one culture. Resource files will be in the format: key = value/message. Use the keys in the user interfaces. The keys will be replaced with the messages/values as stored in the resource files – based on current culture.
  • Best Approach: Satellite Based
  • Current culture: as set in the browsers/operating systems – or give culture/language change option on a button click
  • How to detect current culture: Use CultureInfo, Calendar, and comparison classes
  • Setting culture in the web.config file: Use the globalization element of the web.config file. You can set properties for each culture
  • Satellite Based
    • set the id and runat attributes for all the user-interface elements that require translation
    • create a fallback resource file to keep default strings (key=value) (displays by default or no culture is set or culture is not recognized)
    • create resource file for each culture that you want to support
    • Use the ResourceManager class to load the resource files
    • write code to detect the user’s current culture.
    • Write code to load strings from the resource files and display them
    • Example:
        using System.Globalization  using System.Threading  using System.Resourcesprotected ResourceManager gStrings = new ResourceManager( "JustEtcWebAppsCS.strings", typeof(Satellite).Assembly);......head1.InnerHtml = gStrings.GetString("satellite.head1");

From: http://sitestree.com/?p=5232
Categories:19
Tags:
Post Data:2010-11-12 15:45:20

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

JQuery & ASP.Net #19

Don’t panic. It’s not rocket science. Watch the following videos. Then read a book on ASP.Net & JQuery (May be from Apress), and then consult a good reference book lightly. Keep the book with you always. You will be there soon.

From: http://sitestree.com/?p=5199
Categories:19
Tags:
Post Data:2012-04-30 11:23:06

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

nHibernate & ADO.net entity Framework #19

From: http://sitestree.com/?p=5104
Categories:19
Tags:
Post Data:2007-04-05 04:55:01

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Basic Dot Net Web Development Concepts #19

From: http://sitestree.com/?p=5100
Categories:19
Tags:
Post Data:2006-10-08 13:14:33

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

How to Build a Personal Web-site with ASP.Net (VS 2008, .Net 2.0/3,5) #19

  1. Video Tutorial on this topic
  2. Web-site Code used in this tutorial
  3. Steps
    • Open visual studio 2008 IDE
    • File -> new project -> Visual Basic -> Web -> ASP .Net Web Application
    • Rename default.aspx to template.aspx
    • Insert a table from the tool box
    • Create three rows for the table: top, body, bottom
    • Top Row: banner, slogan
    • Body Row: three cell: left, right, middle: left for left menu, right for right menu/advertising code, middle for contents
    • bottom: privacy, copyright, links to contact us
    • Keep left menu, right menu, top banner, bottom/footer contents in separate files such as leftMenu.aspx, rightMenu.aspx, topBanner.aspx
    • In the template.aspx use Response.WriteFile(“file.aspx”) to link to the left and right menu, the top banner, the bottom/footer. Change file.aspx to the right file name.
    • Create the leftMenu.aspx, rightMenu.aspx, topBanner.aspx, bottom.aspx
    • template.aspx is just the template file. We have two options in the left menu like Home and Companies, that point to index.aspx, and companies.aspx respectively. You have to create those files
    • Now copy the template file, paste it, rename it to index.aspx. index.aspx will be the root file of your web-site. Change the body part of the index.aspx to add contents
    • Copy the template.aspx file, paste it, rename it to companies.aspx. Change the body part of companies.aspx to add contents
    • You can modify leftMenu.aspx to add more options in the left menu. For each option that links to a .aspx file you have to create the .aspx file using the template.aspx [unless already existing]
    • Similarly, you can change your right menu contents by updating rightMenu.aspx
  4. Deployment:
    • Can copy the aspx files in the right place under IIS
    • or let VS 2008 to deploy – right click the project in the solution explorer, click on publish, select the path where you want to publish the website, publish it to a folder where IIS has access
    • You may need to configure IIS a bit, create/identify this application as a .Net application. Otherwise it may not work. Else, you can adjust/edit the files to make them work [remove some lines].
    • Test the application

From: http://sitestree.com/?p=5086
Categories:19
Tags:
Post Data:2010-03-18 10:48:21

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>