Sunday, 8 September 2013

Simplest method to check if the string entered in a TextBox in C# application contain only numbers(0-9) i.e. the string is numeric

I bet you, you won't find this simplest method to check if TextBox contains only numbers anywhere else on the whole net.

Suppose the name of our TextBox is 'textBox1'.

int numeric=0;
foreach (char ch in textBox1.Text)     //loop through each character of the string entered in textBox1
{
     if (ch >= '0' && ch <= '9')     //check if character falls between 0-9
          {
                numeric++;                 //increment on finding each numeric character
          }
}
if(numeric==textBox1.Text.Length)     //true if all the characters of string are numeric i.e. the textBox1 contains only numbers
{
     //Implement your code here
}
           
        //You could also use the same logic to check if certain number of characters are numeric in string i.e. if(numeric==str.Length)

You can also make a class library and save this code in it and use it wherever needed.

public class CheckTextBox
    {
        public static bool AllNumbers(string str) //Pass thextBox1.Text as parameter
        {
            int numeric = 0;
            foreach (char ch in str)
            {
                if (ch >= '0' && ch <= '9')
                {
                    numeric++;
                }
            }
            if (numeric == str.Length)
            {
                return true;
            }
            else
            {
                return false;  //Use the returned bool type values to check if all characters are numeric. If true is returned, all characters are numeric. If false is returned, all values are not numeric.
            }
        }
    }

Friday, 6 September 2013

How to Configure a Database Connection String in an Application you created in C# to make it run on other PC/other SQL Server


When you create an application, you usually embed the connection string in your application by doing something like this:










You have created your application and connected it to Database in your PC. Now, you want to run this application at some other PC having other SQL Server. So, you copy all the executable files from your PC and save them on other PC and run the application. You can find all these files in your bin folder:

The first file of type 'Application' is the main application executable file. Now when you will run this application on the other PC where you saved these files it will give you an error like 'An Unhandled Exception exception has occured. A network-related or instance-specific error occured while establishing a connection to SQL Server." This is because the Connection String you entered in your application was the Connection String of your SQL Server on your PC which may not be the same on other PC. So what You must do now?
The best way is to keep the Connection String in a Configurable file so that it can be configured anytime.

Steps to save Connection String in a Configurable file: Look at the pictures below:


If you have more than one project in same solution then add the Configuration File in each project separately.
Here I have two projects(ACW and Employee) in the same Solution(ACW). I will show you how to store Connection String in Configuration File in one project. You can perform the same steps to store Connection String in other projects and use it.



Click Add


Write the following code in above file:



Save it

You will see that App.config file is added to your project. Now use the following code wherever you have to use the Connection String in the code:

Now you don't have to worry about changing the Connection String on deploying your application on other PC/SQL Server. Just use the Configuration File and change the connection string there and you need not worry about changing at every place you used it. Look at the pictures below on how to do it:

You will find the CONFIG file in your bin folder





Save it.

END


Hope this post helped you.
Don't forget to leave your comments. Thank you.. :)

Error on adding class or other items to a C# program in Visual Studio 10. Error: "Access to path is denied"

So you are getting an error on adding a class or interface or any other item to a Console Windows Application/Project. An error like "Access to path is denied":















Make sure the following two things and the error will go away:


  • Your 'C:\Users\Admin\AppData\Local\Temp' folder is not hidden. If hidden, then unhide it and all its subfolders and files.
  • Your Visual Studio Folder i.e. where Visual Studio is saved in you computer must not be hidden especially the VS\Common7\IDE folder. You generally find this folder in 'C:\Program Files\VS10\Common7\IDE'. Unhide the IDE folder and all its subfolders and files.

Thursday, 5 September 2013

How to access a SQL Server database from other computer connected to the same Workgroup (Local Network/Home Network)

So you are getting an error on adding a class or interface or any other item to a Console Windows Application/Project. An error like "Access to path is denied":















Make sure the following two things and the error will go away:


  • Your 'C:\Users\Admin\AppData\Local\Temp' folder is not hidden. If hidden, then unhide it and all its subfolders and files.
  • Your Visual Studio Folder i.e. where Visual Studio is saved in you computer must not be hidden especially the VS\Common7\IDE folder. You generally find this folder in 'C:\Program Files\VS10\Common7\IDE'. Unhide the IDE folder and all its subfolders and files.

Sunday, 1 September 2013

How to copy database from one SQL server to other (The simplest way)

The simplest method to copy database from one SQL Server to other:

Example: Copy a database named 'ACW' to a SQL Server on ABC-PC from a SQL Server on XYZ-PC.

Open the SQL Server folder(wherever it is saved) on XYZ-PC and go to:
"Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA"
Here you will find all the databases.

XYZ-PC







Copy the file having 'database' and 'database_log' name. In our case they are 'ACW' and 'ACW_log'.





Now copy and save these files onto some external device which you could attach to other PC and get these files onto that PC.

Now the next step is to create a database in the Server where you are going to implement/paste the copied database. Give this database the same name of the copied database. In our example: Create a database named 'ACW' in the SQL server on ABC-PC.



ABC-PC


































Now open the SQL Server folder(wherever it is saved) on ABC-PC and go to:
"Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA"
Here you will find all the databases.




 Now paste the database files(ACW and ACW_log) here that we copied from XYZ-PC. On notifying 'The files with the same name already exists. Do you want to replace it?' accept yes and replace the files. Now open the SQL server on ABC-PC and you will find the ACW database there with all the data.





END

Comments are highly appreciated.