Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 1 December 2015

How to call .cs file from .aspx.cs file in asp.net c#?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click 
imagination hunt to see latest Blogs


Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#

Up till now, I have created a simple website in which I have added a web form file and a class file. But, why did I added these two files in the very beginning of the project. What's the need behind adding these two file's? 

Let's understand this fact. If you quick watch the extension for the class file, its .cs (dot cs). And if you press F7 on the web form you will notice that the extension of web form file is .aspx.cs (dot aspx dot cs). Very much similar to the blog title. .cs and .aspx.cs. Now, as per the title of the blog we have to call the class (.cs) file from the web form code behind (.aspx.cs) file.

Practical Implementation: 

So, let's first create a simple form in which we have a textbox control, a button control and a label control.

Source code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="calling_cs_page_from_aspxcs.aspx.cs" Inherits="calling_cs_page_from_aspxcs" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width:30%;">
                <legend>Application Form</legend>
                <table>
                    <tr>
                        <th colspan="2"><asp:Label ID="lblmsg" runat="server" Text="" Visible="false" ForeColor="Red"></asp:Label></th>
                    </tr>
                    <tr>
                        <th style="width:50%;">
                            Name:
                        </th>
                        <td style="width:50%;">
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
                    </tr>
                </table>
            </fieldset>
        </div>
    </form>
</body>
</html>


Press Ctrl+F5. Our form will look like this



What next, we have to do is? We have to validate the values entered in the textbox.

So, for that we have to write the validation code in our Validate class file that we had created in "App_Code" folder. 

Class code:

using System;        //--------------> Namespace
/// <summary>

/// Summary description for Validation
/// </summary>
public class Validation
{
    public Validation()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    //checkName  function that we have created
    public string checkName(string txtId)
    {
        string msg = string.Empty;
        if (txtId.Trim() == "")  //statement check if textbox is empty
        {
            return msg = "Enter Name";
        }
        else if (txtId.Trim().Length >= 15)    //statement check if textbox contains more than 15 characters
        {
            return msg = "Name length cannot be greater than 15 characters";
        }
        return msg;
    }
}


This will create business logic to our web form.


Related Questions:


Q-1 How many values does a function return?
A) any number of values
B) 2
C) 0
D) 1
Ans. Option (D)
Explanation- A method can return only either single value or no value if no then it’s declared as void method();

Q-2 What is the output for selective code?

public static void Main(string[] args)
 {
     p();
     void p()
     {
         Console.WriteLine("hi");
     }
 }

A) hi infinite times
B) hi
C) None of the mentioned
D) Compile time error
Ans. Option (D)
Explanation: Invalid definition of function p() inside main(). 

Click imagination hunt to see latest Blogs

Keep learning and sharing...
Read More »

How to call .cs file from .aspx.cs file in asp.net c#?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR  +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs


Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#

This question is very general. And is faced by almost everyone including me. So, I have tried something which will be very helpful to everyone and beginners.

To understand this let's create a new website.



Select Asp.net Empty website. After writing the file name click OK button.



Right Click on the website title in the Solution Explorer -> Add -> Choose Web Form (or Add New Item) -> Write the appropriate web form name and click OK button.







Similarly, to add a class file. Right click on project title -> Add new item.



Choose Class file. Write the file name and click the Add button.



An alert box will appear, that will ask that all the class file will be added in 'App_Code' folder. Click 'Yes' button. It is not necessary; you can add your own folder. But, it's good to keep all your class file in 'App_Code' as it will keep your files well organized and easily managed.



You can see that only your class file is added in the 'App_Code' folder.




Related Questions:

Q-1 Why class files need to add in 'App_Code' folder?
Ans. Files need to be add in 'App_Code' folder because:
A) It not only store class file, but it can also store various other the files, such as typed data set, text files, and reports. And it will automatically compile at run-time.
B) Files can be easily accessible through the project.
C) One of the important features of the App_Code folder is that only one .dll is created for the complete folder, irrespective of how many files it contains when we publish our project.

Q-2 Can we access a class file that is declared in the 'App_Code' within our entire application?
Ans. Yes. Files declared can be easily accessible through the project.

Click imagination hunt to see latest Blogs

Keep learning and sharing...
Read More »

Sunday, 29 November 2015

How to create flat files from SQL Server table

Flat Files in SQL Server

What are flat files and what did they give?
Flat files are data files that give the table data. (Only Table Data Not Schema).

How to make flat files in SQL Server?
Follow these simple steps:

1. Right click the database for which you want to create the flat file. Select Tasks -> Export Data...



2. SQL Server Import and Export Wizard window appear. Click on next button.



3. A window appears in which you have to choose the Data Source and its respective information. 
  • By default- the Data Source is selected as SQL Server Native Client 10.0 or anything. Do not change.
  • Select the server name from the drop-down list or you can write the server name by yourself if the respective server name is not there.
  • Now, select one of the Authentication modes - Windows Authentication or SQL Server Authentication.
  • Re-check the database name for which you want to create the flat file. And click next Button.



4. A window appears in which you have to choose the Destination and its respective information.
  •          Select the Destination to Flat File Destination.



Click imagination hunt to read latest blogs.

5. Click the browse button. Here, you have to write a file name (For example- I write my file name as ‘a’ and its extension is .txt (Text File)). Click Open Button.



6. Click Next Button.



7. A Window appears in which you have to select on "Copy data from one or more tables or views". Now, click Next button.



8. Select the table for which you want to create the flat file. And proceed by clicking the next button.



9. Check the checkbox with run immediately option and click next Button.



10. Click Finish.



Click imagination hunt to read latest blogs.

11. File Execution successful. Your file is created successfully.



12. Open the destination folder in which you have created the file.



13. Here is your flat file. Ready to use.




For any query, comment us below.



Related Questions:


Q-1 What are the features of flat files?
Ans. Flat files come with various features:
A) Easy to understand
B) Easy to set up
C) Much lighter in file size.
D) The column data are separated by a comma.

Q-2 Why do we need flat files?
Ans. Flat files are very light as it is created in a text file. And thus, useful and helpful in data storage and data transfer.

Click imagination hunt to read latest blogs.


Keep learning and sharing...
Read More »

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...