Find your device model and serial number using command prompt

How to find your computer/ laptop model

Open the command prompt and type the following command

 wmic bios get serialnumber

if you want to get more details use following command.

 wmic csproduct get name, identifyingnumber

to add vendor name to above result enter this command.

 wmic csproduct get name,vendor,identifyingNumber

utf-8 Optimized Encryption and Decryption in .Net

we know that in our applications, both web and desktop we need to use encrypting and decrypting functionalities. but problem is how strong your encryption is. and how you are going to manage url  encode and  url decode against encrypted content.  Continue reading

Toggel visibility of Div tag in easy way

usually most developers use J Query/CSS to toggle the visibility of webpage elements. for example a DIV tag, however even it is not visible in a webpage, if you examine the source code you will be able to see the div tag in source code. because it is already being rendered by server. however in we can use following code to do the same trick.

Page.FindControl("YourDivId").Visible = false;

Advantage of using this way is,

  • Easy to use.
  • Server do not render the div tag. completely invisible, even to java script web crawlers
  • Most of all its pure .Net. no need to write too many lines of code. simple yet powerful

FTP File Upload in C#

this is a simple FTP file upload example

you can set your proxy credentials using request.Proxy;

private static void UploadFile(string dir, Uri target, string fileName, string username, string password, string finilizingDir, string startupPath, string logFileDirectoryName)
                    // Get the object used to communicate with the server.
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
                    request.Proxy = null;
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    // logon.
                    request.Credentials = new NetworkCredential(username, password);

                    // Copy the contents of the file to the request stream.
                    StreamReader sourceStream = new StreamReader(dir + "\\" + fileName);
                    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                    request.ContentLength = fileContents.Length;

                    Stream requestStream = request.GetRequestStream();

                    requestStream.Write(fileContents, 0, fileContents.Length);

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    if (response.StatusCode == FtpStatusCode.ClosingData)
                       Console.WriteLine(" --> Status Code is :" + response.StatusCode);

                     Console.WriteLine(" --> Upload File Complete With Status Description :" + response.StatusDescription);

                catch (Exception ex)
                     Console.WriteLine("*** Error Occurred while uploading file :" + fileName + " System Says :" + ex.Message + " **********END OF ERROR**********");

Dynamically Build Entity Framework Connection At Runtime

sometimes you may need to build your entity framework based connection dynamically during the run time instead of using app.config file.

note that you need to add following using statements

using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

 internal static EntityConnection GetConnection()
            // Specify the provider name, server and database.
            string providerName = "System.Data.SqlClient";
            string serverName = GetServerName() ;
            string databaseName = GetDatabaseName();

            // Initialize the connection string builder for the
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
            new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.UserID = userID;
            sqlBuilder.Password = password;

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
            new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/MetaModel.csdl|

            EntityConnection conn = new EntityConnection(entityBuilder.ToString());

            return conn;


Usage Of this Method

       using (var contxt = new YourDatabaseEntities(GetConnection()))
         // your code go here.       

Happy Coding.. !!

How to recover Suspected Database

MSSQL server database can be suspected due to following reasons.

  • The database is corrupted
  • There is no enough storage in your system in order Sql Server to fix your database.
  • Database files are being locked by other programs or operating system
  • unexpected shutdown of sql server or system

Here is a simple way to fix your database. however you must be able to log in to Sql Server Management studio first.

Replace Your_Suspected_Database with your actual database name first. then run this query afainst master database.
its recommended to run one line at a time. in checkdb steps you may get error results as well. how ever you can continue to next step.

EXEC sp_resetstatus [Your_Suspected_Database];
DBCC checkdb([Your_Suspected_Database]);
DBCC CheckDB ([Your_Suspected_Database], REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE [Your_Suspected_Database] SET MULTI_USER;