|
 |
| Note:
This uses the internal blog search engine. The Google search engine is also available at the top of the page. |
|
|
 |
|
|
|
|
 |
Please review the site disclaimer before downloading or using content found on this site
|
|
|
 |
|
|
|
|
|
 |
As always, I welcome your comments!
|
Author:
|
Steve Gray
|
Created:
|
4/11/2009 8:00 PM
|
|
|
Code samples and tips from the fine folks in the DevShed. As always, we welcome your comments!
|
By Steve Gray on
7/6/2011 1:58 PM
I’ve placed some controls onto a page dynamically, and was having trouble accessing them in the code behind. There were several complications – they were inside a place holder, inside a master page. Not my favorite technique, but business needs dictated it. After a while I stumbled across the code to make it work, and I wanted to record it. HTML: - <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication1.WebForm2" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
- </asp:Content>
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
- <div>
- <asp:PlaceHolder ID="Placeholder1" runat="server"></asp:PlaceHolder><br />
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
- </div>
- </asp:Content>
and the code behind: - Public Class WebForm2
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
- 'add a textbox directly to the Placeholder
- Dim tb As New TextBox
- tb.ID = "txtTemp"
- Me.Placeholder1.Controls.Add(tb)
-
-
- 'add a text box that is nested inside a table
- Dim t As New HtmlTable
- Dim r As New HtmlTableRow
- Dim c As New HtmlTableCell
-
- tb = New TextBox
- tb.ID = "txtTemp2"
- c.Controls.Add(tb)
- r.Controls.Add(c)
- t.Controls.Add(r)
- Placeholder1.Controls.Add(t)
-
-
- End Sub
-
- Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
- 'this works for the first textbox
- Dim tb As New TextBox
- tb = Me.FindControl("ctl00$ContentPlaceHolder1$txtTemp")
-
- 'and this works for the textbox inside the table. Note the ',0'
- Dim tb2 As New TextBox
- tb2 = Me.FindControl("ctl00$ContentPlaceHolder1$txtTemp2", 0)
-
- End Sub
- End Class
|
By Steve Gray on
7/1/2011 11:25 AM
John Adams had written to his wife Abigail during the debate of the Declaration of Independence: "The second day of July, 1776, will be the most memorable epoch in the history of America. I am apt to believe that it will be celebrated by succeeding generations as the great anniversary festival. It ought to be commemorated as the day of deliverance, by solemn acts of devotion to God Almighty. It ought to be solemnized with pomp and parade, with shows, games, sports, guns, bells, bonfires, and illuminations, from one end of this continent to the other, from this time forward forever more"
|
By Steve Gray on
7/1/2011 10:27 AM
Recently I was trying to troubleshoot an MSIEXEC install (you know, where you see MyInstallName.msi in the folder) that wasn’t really giving an error message, and wasn’t logging anything either. I stumbled upon KB314852 that details how to get the MSIEXEC app to use verbose logging. It applies to XP, but worked fine in Server 2008 http://support.microsoft.com/kb/314852 Enable Windows Installer Logging by Adding Registry Entries Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base: 322756 (http://support.microsoft.com/kb/322756/ ) How to back up and restore the registry in Windows Start Registry Editor (Regedt32.exe), and then create the following path and keys in the registry: HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\Installer Reg_SZ: Logging Value: voicewarmup The letters in the value field are the options that are available to use with Windows Installer logging. You can use the options in any order. Each option turns on a specific logging mode. For MSI version 1.1, the function of each option is as follows : v - Verbose output o - Out-of-disk-space messages i - Status messages c - Initial UI parameters e - All error messages w - Non-fatal warnings a - Start up of actions r - Action-specific records m - Out-of-memory or fatal exit information u - User requests p - Terminal properties + - Append to existing file ! - Flush each line to the log * - Wildcard, to log all information except for the v option. To include the v option, specify *v. It is recommended that you use this service only for troubleshooting. Leaving the service turned on creates a new Msi*.log file every time you use the Add/Remove Programs tool in Control Panel. This activity adversely affects system performance and disk space.
|
By Steve Gray on
7/1/2011 7:55 AM
This works in SQL 2008 and doesn’t in SQL 2000, not sure about 2005 We all have to work with ‘black box’ applications sometime – the kind that has a database with tables and stored procedures that you can’t see into but you still have to work with it. I had a case like that today, the parameters that I was sending to an encrypted stored procedure were not matching up. Usually that’s a versioning issue. Here’s a handy piece of code that will show you all the parameters in a stored procedure - select PARAMETER_NAME, PARAMETER_MODE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION, NUMERIC_SCALE
- from INFORMATION_SCHEMA.PARAMETERS
- where SPECIFIC_NAME = 'taUpdateCreateCustomerRcd'
- order by ORDINAL_POSITION
|
By Steve Gray on
6/20/2011 9:04 AM
I’m writing a new web site (which I hope to announce soon) and I need to use a dynamic where clause. The user is going to enter some filter conditions on the web site and the data that they see will be filtered by that criteria. I’m using the RadFilter control by Telerik; it basically gives me the complete where clause. So, I have a something like Select * from myTable Where @WhereClause Of course, that’s not valid SQL. What to do? In the end, I’ll have to use my least favorite solution, dynamic SQL using the EXEC command. I’ll change the statement above into DECLARE @sql varchar(1000) SET @sql = ‘SELECT * FROM myTable WHERE ‘ + @WhereClause exec(@sql) Ug. At anyrate, the best advice that I could find on the subject was here: http://sommarskog.se/dyn-search.html, I didn’t want to lose the reference so I’ll blog it.
|
By Steve Gray on
6/1/2011 11:47 AM
This is a short primer on why indexes are important in SQL Server. I spend most of my day writing code, usually modifying accounting systems. These systems have lots of tables and lots of stored procs and can be kind of daunting. But the people that wrote them optimized them for getting data in, they didn’t spend much time on getting data out… that’s were we come in. I’m going to show you a quick example on the benefits of writing an index on your stored procedure. But don’t overdo it, too many indexes can be just as bad as no indexes… In order to show you the benefits, we’ll hook up SQL Profiler and look at what’s happening behind the scenes as our queries run. We’ll also configure SSMS to show us the Actual Execution Plan by clicking on Query > Show Actual Execution Plan We’ll start with a small query against the customer table: - SELECT CUSTNMBR, CUSTNAME
- FROM RM00101
- ORDER BY CUSTNMBR
This produces (in my sample database) 577 lines, and the execution plan looks like this:  In Profiler, we see this:  Note that the query did 124 reads to retrieve the query. Now, we’ll create an index: - create index custnmbr on rm00101 (custnmbr) include (custname)
Note that the index is on CUSTNMBR, but we’ve included the CUSTNAME field also. We call this a ‘covering’ index. The index has all the data that SQL will need to return the dataset… SQL won’t even have to look at the actual table. This particular table is very wide, it has quite a few fields. Not having to read the actual table is quite a benefit. So, now we run the query again and our execution plan changes to this:  Note that we do an Index Scan instead of a Table Scan. Table Scans should scare you. In most cases they’re very bad. (the exception would be on small tables where it doesn’t matter anyway) To prove our point that we saved some time, we’ll look at Profiler:  Not that our duration is now 4 milliseconds, and we only had 9 reads. It’s easy to see that a reduction in reads from 124 to 9 would have quite an impact on large datasets.
|
By Steve Gray on
5/26/2011 12:44 PM
When you create a web site in ASP.NET the form tag is included for you. This issue is magnified if you use master pages (who doesn’t?). The issue is that the code that you get from PayPal has a <form> tag in it, and you can’t nest form tags. So your code doesn’t post correctly when the user clicks ‘Buy Now’. Here’s a real easy way around that. There are two ways you can do this, as a link from the <HTML> code, or as a Response.Redirect from the code behind. The link will be less work, but also less flexible. From the HTML, code something like this: - <a href="https://www.paypal.com/cgi-bin/webscr
- ?cmd=_xclick&business=YOUR-PAYPAL-EMAIL-HERE
- &item_name=Widget
- &amount=29.00
- &undefined_quantity=1
- ¤cy_code=USD">
- <img src="http://www.paypal.com/en_US/i/btn/x-click-but23.gif"
- border="0" alt="Buy Now Using PayPal" />
- </a>
If you’ll compare the above to the code that you got from PayPal, you’ll see that we converted the <input> tags to be parameters in the query string. From the code behind it looks like this: - Protected Sub btnSubscribeYear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubscribeYear.Click
- Dim dblAmount As Double
- dblAmount = Me.txtAmount.text
-
- https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amount=" & dblAmount & "&hosted_button_id=3IMNOTREAL3E3
- End Sub
Here’s a list of all the arguments (not guaranteed to be complete or correct, you’ll want to check with PayPal for complete information) Argument Description business Email address associated with seller's PayPal account quantity Quantity of items being sold undefined_quantity Allows user to edit quantity item_name Name of item item_number Optional item number amount Price of each item (without currency symbol) undefined_amount Allows user to edit the amount (good for donations) shipping Price of shipping currency_code Code for type of currency (Default appears to be USD) first_name Customer's first name last_name Customer's last name address1 Customer's first address line address2 Customer's second address line city Customer's city state Customer's state zip Customer's zip code email Customer's email address night_phone_a Customers telephone area code night_phone_b Customers telephone prefix night_phone_c Remainder of customer's telephone number
|
By Steve Gray on
5/25/2011 2:26 PM
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding When clicking ‘Design’ in the SQL 2005 SSMS  I’ve Googled this in the past and got nothing, but today I hit on this piece of code: - EXEC SP_CONFIGURE 'remote query timeout', 1800
- reconfigure
- EXEC sp_configure
-
- EXEC SP_CONFIGURE 'show advanced options', 1
- reconfigure
- EXEC sp_configure
-
-
- EXEC SP_CONFIGURE 'remote query timeout', 1800
- reconfigure
- EXEC sp_configure
I’m a little skeptical about what it does or why it works but this error slows me down so much I’m willing to try anything. It seemed to work (after closing and opening SSRS) We’ll see what happens the next time I find this problem. I’ll keep you posted.
|
By Steve Gray on
5/24/2011 1:42 PM
This piece of code will show how to serialize a DataTable to a string, where it can be saved to disk, and then how to read that string back into a DataTable again. Actually, we’ll have to use a DataSet to read the string (in XML format) back in, if you use a DataTable you’ll get: DataTable does not support schema inference from Xml Oddly, the DataSet works just fine There are code comments for each line below, you should not have any trouble with it. - Imports System.IO
- Imports System.Xml
-
- Module Module1
-
- Sub Main()
- 'declare a datatable and populate it with data. Here, we call a
- 'subroutine that loads invoices from a text file.
- Dim oDT As DataTable = getAPInvoices("Data.txt")
-
- 'declare a stringWriter and an XmlTextWriter
- Dim sw As New StringWriter
- Dim tw As New XmlTextWriter(sw)
- 'give the table a name. This name will become the name of each row in the XML file
- oDT.TableName = "invoice"
-
- 'make the XML pretty
- tw.Formatting = Formatting.Indented
-
- 'write to the XmlTextWriter/StringWriter
- oDT.WriteXml(tw)
-
- 'view the results
- MsgBox(sw.ToString)
-
- 'declare a new DataSet. Note that we're not using a DataTable... you'll get
- 'an error if you do:
- 'DataTable does not support schema inference from Xml
- Dim oDSNew As New DataSet
-
- 'declare a StringReader, populate it with our XML String
- Dim sr As New StringReader(sw.ToString)
-
- 'read in the XML into our DataSet
- oDSNew.ReadXml(sr)
-
- 'loop through it to prove it worked.
- For Each oRow As DataRow In oDSNew.Tables(0).Rows
- Console.WriteLine(oRow("vchnumwk"))
- Next
- End Sub
|
By Steve Gray on
5/24/2011 9:27 AM
Here's a primer for the jargon, and an opinion about what's important. This was written in May 2011… so it should be valid for… 2 more weeks <smiles> 1. Operating System: They're all going to be Windows 7, but 64bit is much faster. The 'information highway' inside the PC is twice as wide as 32bit. It's like having a hose twice as big, or a road twice as wide. More stuff can flow. 2. CPU. This stuff confuses me. CPUs play a big part in the price, there are lots of differences. Cheaper PCs have slower CPUs. Pick a price point ($399, 499) and try to get the best for the money. 3. Screen size: Personally, I have 4 monitors. Really. It's easier to work on a bigger screen. Same as above. Pick a price point and get a better one. 4. Memory (ram). Windows 7 requires 2gb, and that should be fine. More memory allows you to have more programs open at once; 4gb would be great. 5 Hard drive space (not the same as RAM). RAM goes away when you turn the computer off (think: husband’s memory), the hard drive is magnetic media like a VCR tape, it never erases (think: wife’s memory). More hard drive space means you can store more stuff on your computer. More programs; these days it means more photos and more music. I have 9000 photos to date (8 gb of storage) , and about 80 CDs (5 gb). So, even a smaller 320 gb drive should do. 6. CD (optical drive). You need this. A DVD is almost always included anyway. (think: watching movies in the airport) 7. Web Cam: I bought my wife’s laptop without one because I didn’t think we’d need one. She’s always taking mine now to Skype with the grandkids. If your kid is going away to college, be sure to get a web cam 8. Last is weight. People don’t usually lug these things around much, unless you’re a travelling salesman. So this might not be so important. But it’s something to think about. Lighter usually means a smaller screen, less battery life, and more expensive. Everything’s a trade off.
|
As always, I welcome your comments! |
|
|
 |
|
|
|
|
|
|