Sql Server: The backup set holds a backup of a database other than the existing (Restore failed)
Posted by Ruslan Sudentas in SQL on March 3, 2010
There are few common tasks in SQL server that should work out of the box, but give errors that confuse less experiences developers. Like when you try to restore a backup from another server, you get:
Restore failed for Server ‘.\SQLEXPRESS’. (Microsoft.SqlServer.SmoExtended)
System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing ‘SomeDb’ database. (Microsoft.SqlServer.Smo)
The solution could not be simpler: you should use WITH REPLACE option of RESTORE command, or in SQL Server Management studio select “Options” page and check “Overwrite the existing database (WITH REPLACE)”
SQL Server 2008 ldf file size
Posted by Ruslan Sudentas in SQL on March 1, 2010
SQL Server is smart in selecting great default options for a new database. If you can afford to lose the changes between backups to keep maintenance simple (and in most of non-critical databases it is a reasonable compromise) and you backup your database regularly using a tool like SQLBackupAndFTP, then the “Simple” recovery model (selected by default) is appropriate for you. However if you ever had massive data operations, you may notice that the size of your transaction log (LDF) file is huge. The reason for it is that SQL server does not automatically shrinks the size of transaction log.
To keep log file under control, it may be tempting to enable Auto Shrink option. That would be a mistake – it is an evil option that should always be off. Shrink is a heavy procedure that should only be used rarely.
Instead just run this command to decrease the size of the data and log files to leave 10 percent free space (read more):
DBCC SHRINKDATABASE (YourDatabaseName, 10)
For more precise operation use DBCC SHRINKFILE or you can just do it in Management Studio
It is not over yet! Most often shrink will increase fragmentation of your indexes. To defragment indexes, you’ll need to use ALTER DATABASE – see this link
Creating a Self-Signed SSL Certificate without a mess of makecert.exe (using SSL Diagnostics Tool)
Posted by Ruslan Sudentas in Random Thoughts on December 13, 2008
Introduction:
So you have a server and you need to implement SSL to allow secure (https) communication. What choices do you have? You can buy a certificate from certification authority or you can issue a Self-Signed certificate to yourself. The difference is that your browser “knows” it can trust the certificates from the authorities (it has it installed). But when the browser encounters the https connection with a server with the self-signed certificate, the user is presented with a message like this:
Thus, self-signed certificates are OK for test and development web sites, but generally not OK for public websites.
This article will show you the simplest ways to create a Self-Signed SSL Certificate.
Here are your options. (Or just go to the Best Solution )
Solution 1 (quite long, but recommended by Microsoft)
Setting Up SSL Using IIS and Certificate Server
MS recommends that you get the certificate from the certificate server. This means that you have to have an access to Window 2000 or Windows 2003 server with “Certification Services” installed. You use IIS MMC to generate request to this server. Then using browser you submit this request to the server. Then, when somebody at that server approves the request, you will get back a certificate.
See details at http://support.microsoft.com/default.aspx?kbid=299525
Solution 2 (fast, but sometimes could be tricky)
Creating Self-Signed SSL Certificates using makecert.exe
It is a quite simple solution. The only problem is that sometimes it just doesn’t work, and it’s hard to determine what is wrong. The makecert.exe comes with VS.NET. It you don’t have .NET Framework 1.1 installed, the makecert might be outdated. You can download a newer version from http://download.microsoft.com/download/platformsdk/Update/5.131.3617.0/NT45XP/EN-US/makecert.exe
Just replace yourservername with the computer name of your PC and run:
makecert -r -pe -n “CN=yourservername” -b 01/01/2000 -e 01/01/2050 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localMachine -sky exchange -sp “Microsoft RSA SChannel Cryptographic Provider” -sy 12
Then go to the IIS “Web Site Properties”, “Directory Security”, “Server Certificate…”, “Assign an existing certificate” and select the new certificate from the list.
It works? Fine! No? Go to the Best Solution
Solution 3 (OK for not-technical users)
Download a test certificate from certification authorities
The certificate companies like VeriSign and Thawte issue test certificates, but they expire after 90 days or so, and the process of getting it could be quite tedious.
Solution 4 (The Best and Recommended)
Create a Self-Signed Certificate using SSL Diagnostics Tool
Avoid all this pain with a nice tool from Microsoft: SSL Diagnostics . Download setup.exe (2112 KB) from here: http://www.microsoft.com/downloads/details.aspx?familyid=CABEA1D0-5A10-41BC-83D4-06C814265282&displaylang=en
Install it and run. In the main window of SSL Diagnostics, right-click the Web site level (shown by [W3SVC/<site number>]), and then click Create New Certificate.
That is it. You are done. Don’t forget to explore other capabilities of this nice tool.
Writing to Event Log in ASP.NET application
Posted by Ruslan Sudentas in Random Thoughts on December 13, 2008
Writing to Event Log is not simple, but very simple:
// Create an EventLog instance and assign its source.
EventLog
myLog = new EventLog();
myLog.Source
= "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing
to event log.");
EventLog is in System.Diagnostics namespase.
Before using the source of the EventLog, you have to create
it. But if you use the code like this:
//Create Event Log if It Doesn’t Exist
if (! EventLog.SourceExists("MySource"))
EventLog.CreateEventSource("MySource",
"MySource");
you will get the error:
System.Security.SecurityException: Requested registry access
is not allowed.
Instead, go to registry editor (regedit), locate key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
and create a new key with the source name
Hierarchical Data Binding in ASP.NET
Posted by Ruslan Sudentas in Random Thoughts on December 13, 2008
This will show a sample of hierarchical data bound to
hierarchical List Controls (repeaters)
This is just a transcript of the great article by Fritz Onion
from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspn-hierdatabinding.asp
Get the data
This is obvious
//Create DataSet and Bind it to the
repeater
private
void Page_Load(object sender, EventArgs e)
{
string strConn
=
“server=.;trusted_connection=yes;database=northwind”;
string strSql
= “SELECT CustomerID, CompanyName FROM ” +
” Customers;
“ +
“SELECT OrderID, CustomerID,
“ +
“ EmployeeID FROM
Orders;” +
“SELECT OrderID,
Products.ProductID,” +
“ProductName, Products.UnitPrice
FROM” +
” [Order Details], Products WHERE
“ +
” [Order Details].ProductID =
“ +
“Products.ProductID”;
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
da.TableMappings.Add(“Customers1″,
“Orders”);
da.TableMappings.Add(“Customers2″,
“OrderDetails”);
//Fill DataSet
_ds = new DataSet();
da.Fill(_ds, “Customers”);
//Add Relations
_ds.Relations.Add(“Customer_Order”,
_ds.Tables["Customers"].Columns["CustomerID"],
_ds.Tables["Orders"].Columns["CustomerID"]);
_ds.Relations[0].Nested = true;
_ds.Relations.Add(“Order_OrderDetail”,
_ds.Tables["Orders"].Columns["OrderID"],
_ds.Tables["OrderDetails"].Columns["OrderID"]);
_ds.Relations[1].Nested = true;
//Bind
_customerRepeater.DataSource =
_ds.Tables["Customers"];
_customerRepeater.DataBind();
}
Very useful function
///<summary>
/// returns a DataView of ChildRows
///</summary>
///<param name=”dataItem”>parent row</param>
///<param name=”relation”>the name of the
relation</param>
///<returns>DataView</returns>
protected DataView GetChildRelation(object dataItem,
string relation)
{
DataRowView drv = dataItem as DataRowView;
if (drv != null)
return drv.CreateChildView(relation);
else
return
null;
}
HTML view
Three hierarchical repeaters:
<asp:RepeaterRunat=”server”ID=”_customerRepeater”
EnableViewState=”false”>
<ItemTemplate>
Customer:
<%#
DataBinder.Eval(Container.DataItem, “CustomerID”) %>
<%#
DataBinder.Eval(Container.DataItem,”CompanyName”) %>
<br/>
<asp:Repeaterrunat=”server”EnableViewState=”false”
DataSource=
‘<%# GetChildRelation(Container.DataItem,
“Customer_Order”)%>‘
ID=”Repeater1″>
<itemTemplate>
Orderid:<b>
<%#DataBinder.Eval(Container.DataItem,
“OrderID”)%>
</b><br/>
<asp:Repeaterrunat=”server”EnableViewState=”false”
DataSource=
‘<%#
GetChildRelation(Container.DataItem,
“Order_OrderDetail”)%>‘
ID=”Repeater2″>
<itemTemplate>
<b><%# DataBinder.Eval(Container.DataItem,
“ProductName”) %></b>
$<%#
DataBinder.Eval(Container.DataItem,
”UnitPrice”) %>
<br/>
</itemTemplate>
</asp:Repeater>
</itemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Small development Q&A
Posted by Ruslan Sudentas in .NET on December 13, 2007
Q:how to zip in .NET
A:find library ICSharpCode
Q:System.Net.HttpWebRequest vs WebClient
Q:How to create self-signed ssl certificate?
A:use makecert.exe (part of .NET) Ex:makecert.exe -a SHA1 -ss my -sr LocalMachine -n "CN=ComputerName" -b 01/01/2000 -e 01/01/2050 -eku 1.3.6.1.5.5.7.3.1 -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
Then install it in IIS
Q:How to implement ssl switching (b/w http & https) in asp.net app?
A:See "Switching Between HTTP and HTTPS Automatically" by "Matt Sollars"
Q:How to create multiple Web Servers in WinXP?
A:cscript c:\Inetpub\Adminscripts\adsutil.vbs COPY W3SVC/1 W3SVC/2
http://www.bobshowto.com/iis_servers.htm
Q: how to copy user’s profile?
A: http://support.microsoft.com/default.aspx?scid=kb;EN-US;811151
Q: SELECT permission denied on object ‘ASPStateTempApplications’, database ‘tempdb’, owner ‘dbo’.
A: http://support.microsoft.com/default.aspx?scid=kb;en-us;810474
http://idunno.org/dotNet/sessionState.aspx
Q: How to debug Client-Side in .NET
A: -In IE Tools\Adv Options clear "Disable script debugging…"
-In VS.NET Tools\Debug Processes attach to the IExplore.exe
-In VS.NET set a breakpoint in the Client Side code
-In IE cause the script to run
Q: When connecting another computer to cable modem, it can’t connect to Internet saying "Unable to contact DHCP host"
A:Reboot the cable modem
Q:aspx files don’t run, but open "File download" dialog.
A:C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\aspnet_regiis.exe -i
Q: When trying to run asp.net, the server gives: "Server Application Unavailable" error
A:http://support.microsoft.com/default.aspx?scid=kb;en-us;Q315158
Q: Disable Windows Explorer search assitant
A: In HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState add String Value "Use Search Asst" with value "no"
Q: How to output an xls file
A: Response.ContentType = "application/vnd.ms-excel" OR "application/excel" and maybe Page.EnableViewState = "false"