I was trying out to find a way to do this the other day and got it to work after a few tweeks. I have SQL 2005 installed and wanted to save the Uploaded Company Logos into the Database. First make sure that the table has a column of "image" type. the Upload control id is "flUploadImage". I will limit the code to that specific Image Upload, while removing the rest of the table fields/properties.
if (flUploadImage.HasFile && flUploadImage.PostedFile.ContentLength > 0) { byte[] img = null; img = new byte[flUploadImage.PostedFile.ContentLength]; flUploadImage.PostedFile.InputStream.Read(img, 0, img.Length); System.Data.Linq.Binary postedimage = new System.Data.Linq.Binary(img); sponsorship.Logo = postedimage; }
Fetching the Record:
To Display the Image, I had a seperate page built for rendering the image. The Image was queried on the basis of the querystring parameter that was passed on the page.
So my Image control named "imgLogo" was assigned this way
The ToArray() method was a pretty simple way to convert the System.Linq.Binary to an Array of bytes, which initially had made me little crazy figuring out how to do that, until I found out this Method. The image appeared without a doubt! Perfect!
Update : 03/01/2011
If you would like to post a default image if no image is present, here's how you go about it.
FileStream fileStream = new FileStream(Server.MapPath("~/images/noimage.png"), FileMode.Open, FileAccess.Read); byte[] img = new byte[fileStream.Length]; fileStream.Read(img, 0, (int)fileStream.Length); fileStream.Close(); System.Data.Linq.Binary postedimage = new System.Data.Linq.Binary(img); sponsorship.Logo = postedimage;
One of the small issues of the Cassini server thats built in with Visual Studio IDE is that when you run it, it also has the Folder name of your application/project in the url. While this is not a major issue, it surely does some damage to the relative urls of images and/or other files which are relative. This would mean changing the relative paths for the development and production copies. I have done that in the past and know it is annoying if one forgets to change them, even though they can be stored in Application web.config file
One of the fixes that I found out was to configure the site using the external tools section of the IDE:
Clicking on the external tools takes you to the following window:
And thats what it is.
Follow this to have it done.
Click on "Add" to add a new Item to the project. Title : This could be your project name Command : This is the path for the Cassini Web server. Locate its exe and provide over here. This is what I have
Arguments : Here you would assign a port number and your application folder as your arguments. The port number can be any number. The Application folder is where your project resides
/port:8085 /path:"C:\Users\Username\Documents\Visual Studio 2008\WebSites\SD30"
Once you are done. Click on Ok.
Now click on "Tools" from the IDE and you will see your Application Name in the Menu. Click on your Application Name to instantiate the Cassini Web Server for your Application. You will see the Web server activated in the task bar. Click on it to open your default page.
Today I wanted to create a dynamic google sitemap file for my site. So I started reading the documentation for creation of a sitemap. Google has a very good documented format, making all the points clear. you can vew it here. The Google Webmaster tools are also one of the powerful features where you could view the site stats, and thats where you need to submit your google sitemap.
The sitemap is placed in the root directory which contains all the site urls for google to index pages. You can have a maximum of 50,000 urls per Sitemap. For additional urls, another sitemap file can be created and then linked with each other using SiteMap indexing file. Google does great at explaining all this stuff, so make sure you go through all the stuff in the link that I have put up here above to get the best of both worlds.
Generating the dynamic SiteMap file:
Generation is just like any other xml file, except that you have to adhere to the standards mentioned in the sitemap protocol.
Here's the code. You have to modify the query to suit your needs.
public partial class NewsSiteMap : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DateTime now = DateTime.Now; DataClassesDataContext db = new DataClassesDataContext();
var latestNewsArticles = (from articles in db.article_to_magazine_mappings where articles.magazine_id == 1 && articles.article.article_type_id != 20 select new { ID = articles.article.id, Title = articles.article.web_title, TitleLink = articles.TitleLink, Author = articles.AuthorName, TitleKeywords = articles.article.meta_tags, Date = articles.publish_date, LegacyID = articles.article.legacy_id }) .Take(49999);
Response.Clear();
Response.ContentType = "text/xml";
using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8)) { writer.WriteStartDocument();
Today I had an a very minor issue, but it was worth writing here. Firefox had this nasty delay of triggering from the mouseover even to mouseout event on my menu which had a javascript to be executed in order to change the text inside a span tag. Having done this tons of times before, I cannot recall this issue. However, this might be due to the fact that the anchor tag encompassing the span tag of the menu had a background image applied and this might be the cause of conflict or delay on the triggering of the mouseout event of the span tag. This issue was not displayed in IE, Opera or Safari, though.
I tried using innerHTML to change the text, but of no avail. After a couple of quick searches, I found out using the
obj.firstChild.data
element, where obj is your span tag. It worked perfect miraculously! This brings to the conclusion that the old school DOM model is still worthwhile!
Here's a quite and easy way to find a url and surround it with anchor tags, and also putting a target='_blank' so that you could open up the link in a new tab. Very useful and powerful feature of Regex.
string yourtext = "Heres the link to the site: http://www.yoursite.com/content.html"
Google Searches by Relevance by Default. One of the things that was on my TODO list was to have the articles search sorted by date, irrespective of the article relevance. So I had a radiobutton list which lets the users to choose if they want the results by relevance, or by date. There were not really lot of information about it on google blogs/forums, and nobody was sure whats the correct way to do it. After some clicks and head bashings, I found out the closest way to get to the results to sort by date is to have its own Annotation file and context file. So I logged into my google account and went to the custom search section, where I had customized my google site search. On the advanced Tab, I downloaded the Annotation file and the context file.
The Idea is that, when the user opts to sort by date, call these customized xml files to show results sorted out by date.
In the context file you will find the following tag
This references the annotations file that you downloaded from the "Advanced Console"
There wont be any change in the annotations file, except that a copy will reside on the server. Make sure that when you change the site restrictions (Adding/Removing folders/files for site search), compare your downloaded version with the one on the Custom search.
Based upon the Radio Button selection, just select the annotation file:
if (rbSearchby.SelectedValue == "r") //Search by Relevance Response.Redirect(GetRoot() + "content/googlesearch.aspx?cx=01054907335329%3Abolrua_bzhw&cof=FORID%3A11&ie=UTF-8&q=" + HttpUtility.UrlEncode(SearchBox_TextBox.Text) + "&sa=Search"); else //Search by Date Response.Redirect(GetRoot() + "content/googlesearch.aspx?cref=http%3A%2F%2Fwww.yourwebsite.com%2Fgooglespecc.xml&cof=FORID%3A11&ie=UTF-8&q=" + HttpUtility.UrlEncode(SearchBox_TextBox.Text) + "&sa=Search");
I had been having problems with regards to adding or updating rows from the management studio with my version of SQL Server on the dev server, but it worked perfect on the live server. I use to write add and update queries to update my records, but this is a hassle if I would like to update large number of rows. So I wanted a quick solution to this. On doing some R & D, I found out that the "no count" feature which is available in the "SQL Server" properties (obtained by right clicking on the Registered Server and clicking on properties).
You can find the "no count" property in the "Connections" tab under "Default connection options". Just uncheking the "no count" feature and refreshing your server would make it work. Voila !!!
This, being my first post, I want to make a powerful debut. Though the content is not produced by me (Ya its the microsoft guys), I hope it will be useful for using them. Shower your power !!!
What’s New in PowerCommands 1.1
Enable/Disable PowerCommands in Options dialog
This feature allows you to select which commands to enable in the Visual Studio IDE.Point to the Tools menu, then click Options.Expand the PowerCommands options, then click Commands.Check the commands you would like to enable.
Format document on save / Remove and Sort Usings on save
The Format document on save option formats the tabs, spaces, and so on of the document being saved.It is equivalent to pointing to the Edit menu, clicking Advanced, and then clicking Format Document.
The Remove and sort usings option removes unused using statements and sorts the remaining using statements in the document being saved.
Note:The Remove and sort usings option is only available for C# documents.
Note:Format document on save and Remove and sort usings both are initially defaulted OFF.
To select either option,point to the Tools menu, then click Options.Expand the PowerCommands options, and then click General.
Note:If you click Save, the option you select applies to the selected document.If you click Save All, the option applies to all open documents.
Clear All Panes
This command clears all output panes. It can be executed from the button on the toolbar of the Output window.
Copy Path
This command copies the full path of the currently selected item to the clipboard. It can be executed by right-clicking one of these nodes in the Solution Explorer:
·The solution node.
·A project node.
·Any project item node.
·Any folder.
Email CodeSnippet
To email the lines of text you select in the code editor, right-click anywhere in the editor and then click Email CodeSnippet.
Note: If no lines are selected, the entire document will be emailed.
Insert Guid Attribute
This command adds a Guid attribute to a selected class.From the code editor, right-click anywhere within the class definition, then click Insert Guid Attribute.
Note:If the class already has a Guid attribute, the Insert Guid Attribute command will not be visible.
Show All Files
This command shows the hidden files in all projects displayed in the Solution Explorer when the solution node is selected.It enhances the Show All Files button, which normally shows only the hidden files in the selected projectnode.
Undo Close
This command reopens a closed document , returning the cursor to its last position.To reopen the most recently closed document, point to the Edit menu, then click Undo Close.Alternately, you can use the Ctrl+Shift+Z shortcut.
To reopen any other recently closed document, point to the View menu, click Other Windows, and then click Undo Close Window.The Undo Close window appears, typically next to the Output window.
Double-click any document in the list to reopen it.
What’s New in PowerCommands 1.0
Collapse Projects
This command collapses a hierarchy in the solution explorer starting from the root selected node. It can be executed from three different places: solution, solution folders and project nodes respectively.
Copy Class
This command copies a selected class entire content to the clipboard. It can be executed from a single project item or a project item with dependent sub items.
Paste Class
This command pastes a class entire content from the clipboard. It can be executed from a project or folder node.
Copy References
This command copies a reference or set of references to the clipboard. It can be executed from the references node, a single reference node or set of reference nodes.
Paste References
This command pastes a reference or set of references from the clipboard. It can be executed from different places depending on the type of project. For CSharp projects it can be executed from the references node. For Visual Basic and Website projects it can be executed from the project node.
Copy As Project Reference
This command copies a project as a project reference to the clipboard. It can be executed from a project node.
Edit Project File
This command opens the MSBuild project file for a selected project inside Visual Studio. It can be executed from a project node.
Open Containing Folder
This command opens a Windows Explorer window pointing to the physical path of a selected item. It can be executed from a project item node
Open Command Prompt
This command opens a Visual Studio command prompt pointing to the physical path of a selected item. It can be executed from four different places: solution, project, folder and project item nodes respectively.
Unload Projects
This command unloads all projects in a solution. It can be executed from the solution node.
Reload Projects
This command reloads all unloaded projects in a solution. It can be executed from the solution node.
Remove and Sort Usings
This command removes and sort using statements for all classes given a project. It can be executed from a solution node or a single project node.
Note: The Remove and Sort Usings feature is only available for C# projects since the C# editor implements this feature as a command in the C# editor (which this command calls for each .cs file in the project).
Extract Constant
This command creates a constant definition statement for a selected text. It can be executed from the code window over a selected text.
After specifying the constant visibility and identifier (Figure 24) the following constant declaration is created:
Clear Recent File List
This command clears the Visual Studio recent file list.
Clear Recent Project List
This command clears the Visual Studio recent project list.
Transform Templates
This command executes the associated custom tool with text templates items. It can be executed from a DSL project node or a folder node.
Close All
This command closes all documents. It can be executed from a document tab.
One thing that everyone must have experienced is to remove the stored user credentials on a Windows PC, when you start to think it can become a security vulnerability. I did little googling on this, after failing to find out where those passwords were stored. I wanted to remove my remote server's stored credentials. The search result showed a cute windows command to be entered to get there. Cute! I love windows command shortcuts!
Here's what it is:
Type the following command in the Run window (Windows + R):
rundll32.exe keymgr.dll, KRShowKeyMgr
This will take you to the Password management store.
Thats it! You can do whatever you want from here. Needs no further explaination
This article of mine explains how to sign a code without the need to buy the digital signature for Microsoft Trusted Certificate Authorities. Please note that this would be helpful for test purposes only since it requires you to install the certificate in all client browser using the application. Eventually, a certificate from Trusted Root CA like Verisign, Thawte. Etc has to be bought for insuring that your CAB/ActiveX component is available for use to all without security issues.It even makes code signing easier, which needs only signcode.exe utility along with the spc file and private key provided by the CA.
For the list for Microsoft trusted CA’s , click the link below:
The article uses Ascertia as the CA to obtain the free digital certificate.
Steps for Signing Code
Some of the utilities provided in the process of signing the code are part of the Microsoft SDK. So we would be having them if we have any version of .NET installed on our machine. This works like a charm if the steps are followed correctly. We just need to execute all the utilities one by one.
Following is the Procedure for code signing:
1. Downloading the certificate from Ascertia website
This requires the user to register for an account and provide some information so as to embed it into the certificate.
Please note that the "Name" that you enter will be the name seen on the certificate. When the information is provided, a prompt will be asked to add the certificate into the browser's certificate root store. Add the certificate in the "Trusted Root Certification Authorities" store.
2. Generation of Software Publishing Certificate (spc)
Run certmgr.exe. This
would display the certificate installed in the previous step along with other certificates. Select the option to export without the private key and in DER encoded binary. This should produce a certificate file with .cer extension. Put it in a folder and at a path, which is not cumbersome. E.g. c:\<foldername>. Put all the utility files in this folder too.
Now convert the certificate file to spc file - Software Publishing Certificate with the following utility
cert2spc <insert cer file path> <insert new spc file path>
This is the file, which is provided by the Verisign along with a private key. We will generate a private key later in this stage.3.
3. Generation of Personal Information Exchange file (pfx)
Run certmgr.exe again. Select the same certificate, and export again.
This time export the private key. Make sure "Include all certificates in the certification if possible" is checked and "Delete the private key if the export is successful" is unchecked.
This will export a pfx file. (Make sure to remember the password you set.)
We can verify the signature by opening the properties of the signed file, and clicking the Digital Signatures tab. If there is no Digital Signatures tab, then there is something wrong that we have done in our procedure.
If everything goes right, then we have our code digitally signed by Ascertia CA and we can go and use our activeX to install in our IE 6 and IE 7 browsers.
It really proved helpful to me to test my ActiveX Component for IE 7 Browser. Hope it proves useful to you guys too.