Pages

Showing posts with label asp.net. Show all posts
Showing posts with label asp.net. Show all posts

Thursday 10 December 2015

SQL VIEW

View
A VIEW in SQL in completely based on the concept of Virtual Table which comes as a result of some SQL query. It behaves like a real table and you can perform all SQL statements and function on a VIEW.


Tech-spoon.com - SQL-VIEW


How to create a VIEW:

Syntax:

CREATE VIEW [dbo].[View_Name] as
select A.Name, A.Email, A.Phone
from tbl_name as A
where A.status = 1

Example:

For example let me create a table as tblEmployee
CREATE TABLE [dbo].[tblEmployee]
(
id int identity(1,1),
Emp_Code varchar(20) primary key,
Emp_Name varchar(500),
Emp_Email nvarchar(500),
Emp_Salary decimal,
Emp_Dept int,
Emp_Status int
)

Now I am going to create another table with the name tblDepartment

CREATE TABLE [dbo].[tblDepartment]
(
id int identity(1,1),
Dept_Id int primary key,
Dept_Name varchar(500),
Dept_Status int
)

Now based on these two tables I want to get details of those employees who are currently working for the organization i.e. has Emp_Status as 1, for this I am going to create a VIEW named as View_Emp_Active.

CREATE VIEW [dbo].[View_Emp_Active] as
select A.Emp_Code, A.Emp_Name, A.Emp_Email, A.Emp_Salary, B.Dept_Name
from tblEmployee A inner join tblDepartment B
on A.Emp_Dept = B.Dept_Id
where a.Emp_Status = 1

So, whenever you need to get the list of active employees just call this View rather than writing the full code of the select statement from two tables using join statement.

Some Important things you should know about the SQL VIEW:
1.    A view can only be updated i.e. insert, delete, update statement can only applied on the View if it is formed from s single table, otherwise you cannot perform these actions on a View.
2.    You cannot have a view having more than 1000+ columns.

For more information about website development please visit http://www.jalwaltechnologies.com.

Friday 30 October 2015

ASP.NET Dropdown for Months (Filled)

               <asp:DropDownList ID="ddlmonth" runat="server" CssClass="form-control">
                                    <asp:ListItem Text="January" Value="1"></asp:ListItem>
                                    <asp:ListItem Text="February" Value="2"></asp:ListItem>
                                    <asp:ListItem Text="March" Value="3"></asp:ListItem>
                                    <asp:ListItem Text="April" Value="4"></asp:ListItem>
                                    <asp:ListItem Text="May" Value="5"></asp:ListItem>
                                    <asp:ListItem Text="June" Value="6"></asp:ListItem>
                                    <asp:ListItem Text="July" Value="7"></asp:ListItem>
                                    <asp:ListItem Text="August" Value="8"></asp:ListItem>
                                    <asp:ListItem Text="September" Value="9"></asp:ListItem>
                                    <asp:ListItem Text="October" Value="10"></asp:ListItem>
                                    <asp:ListItem Text="November" Value="11"></asp:ListItem>
                                    <asp:ListItem Text="December" Value="12"></asp:ListItem>
               </asp:DropDownList>

Wednesday 22 July 2015

How to upload file in ASP.NET

Introduction :
This code will help you understand how to use file upload control in asp.net and the code to upload the file on specific folder path.

Code:

       string iim = DateTime.Now.ToString("ddMMyyyyHHmmss"); //prefis of file name to upload
                if (FileUpload01.HasFile)
                {
                    string f1 = Path.GetFileName(FileUpload01.PostedFile.FileName);
                    string[] xx = f1.Split('.');  // to get posted file extension
                    string a = iim + "_" + xx[1].ToString();
                    FileUpload01.SaveAs(Server.MapPath("~/FolderPath/") + a);
                }

Thursday 9 July 2015

List Box Control

Introduction :
This code will show how to use ListBox to Select/Filter items :
Design Code :
<div style="width: 100%; float: left;">
            <div style="width: 25%; float: left; margin-top: 10px;">
                <asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="98%" SelectionMode="Multiple">
                    <asp:ListItem>A</asp:ListItem>
                    <asp:ListItem>B</asp:ListItem>
                    <asp:ListItem>C</asp:ListItem>
                    <asp:ListItem>D</asp:ListItem>
                    <asp:ListItem>E</asp:ListItem>
                    <asp:ListItem>F</asp:ListItem>
                </asp:ListBox>
            </div>
            <div style="width: 8%; float: left; margin: 10px;">
                <asp:Button ID="btn01" runat="server" OnClick="btn01_Click" Text=">" Style="margin: 3px 14px;" />
                <asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Text=">>" Style="margin: 3px 14px;" />
                <asp:Button ID="Button3" runat="server" OnClick="Button2_Click" Text="<" Style="margin: 3px 14px;" />
                <asp:Button ID="Button4" runat="server" OnClick="Button3_Click" Text="<<" Style="margin: 3px 14px;" />
            </div>

            <div style="width: 25%; float: left; margin-top: 10px;">
                <asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="98%" SelectionMode="Multiple"></asp:ListBox>
            </div>
            <asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
        </div>


Code Behind : 

ArrayList arraylist1 = new ArrayList();
    ArrayList arraylist2 = new ArrayList();
    protected void btn01_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (ListBox1.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    if (!arraylist1.Contains(ListBox1.Items[i]))
                    {
                        arraylist1.Add(ListBox1.Items[i]);
                    }
                }
            }
            for (int i = 0; i < arraylist1.Count; i++)
            {
                if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
                {
                    ListBox2.Items.Add(((ListItem)arraylist1[i]));
                }
                ListBox1.Items.Remove(((ListItem)arraylist1[i]));
            }
            ListBox2.SelectedIndex = -1;
        }
        else
        {
            lbltxt.Visible = true;
            lbltxt.Text = "Please select atleast one in Listbox1 to move";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        while (ListBox1.Items.Count != 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (ListBox2.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                if (ListBox2.Items[i].Selected)
                {
                    if (!arraylist2.Contains(ListBox2.Items[i]))
                    {
                        arraylist2.Add(ListBox2.Items[i]);
                    }
                }
            }
            for (int i = 0; i < arraylist2.Count; i++)
            {
                if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
                {
                    ListBox1.Items.Add(((ListItem)arraylist2[i]));
                }
                ListBox2.Items.Remove(((ListItem)arraylist2[i]));
            }
            ListBox1.SelectedIndex = -1;
        }
        else
        {
            lbltxt.Visible = true;
            lbltxt.Text = "Please select atleast one in Listbox2 to move";
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        while (ListBox2.Items.Count != 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
        }

    }

Saturday 20 June 2015

Code to set default startup page in web.config

Introduction:

When someone opens your website by entering your domain name in the web browser then by default it will show the startup page. If you haven't setup an startup page than it will look for Default.aspx or index.html page in the source files. You can setup your startup page in web.config file.

Code:

  <system.webServer>
    <directoryBrowse enabled="false"/>
    <defaultDocument>
      <files>
        <clear/>
        <add value="Default.aspx"/>
      </files>
    </defaultDocument>
  </system.webServer>

URL mapping in web.config

Introduction :
URL mapping is very useful tool to make your URL look good and meaningful to site user. It will overcome the querystring issue. The user sometimes got confused with the parameters passed in the url in the form of querystring.

Code :
For url mapping you have to pace this code in the web.config file in <system.web></system.web> tag

    <urlMappings enabled="true">
      <add url="~/ComputerCourse" mappedUrl="~/courses.aspx?cid=2"/>
      <add url="~/ComputerHardware" mappedUrl="~/courses.aspx?cid=3"/>
      <add url="~/MobileRepairing" mappedUrl="~/courses.aspx?cid=4"/>
      <add url="~/CuttingTailoring" mappedUrl="~/courses.aspx?cid=5"/>
      <add url="~/Beautician" mappedUrl="~/courses.aspx?cid=6"/>
    </urlMappings>


Thursday 18 June 2015

Code to read content of Word/.doc file

Introduction: In this article I will explain how to read the content of a .doc file and show this in a Rich Textbox. And further we will filter the Email IDs form the content of the file.

NameSpace :
First of all you need to add a reference for ‘Microsoft.Office.Interop.Word’
And the use this namespace on your page.

using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
    
Code :
Here is the complete code:
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op1 = new OpenFileDialog();
            op1.Multiselect = false;
            op1.ShowDialog();
            int count = 0;
            string[] FName;
            foreach (string s in op1.FileNames)
            {
                FName = s.Split('\\');
                //createting the object of application class 
                Microsoft.Office.Interop.Word.Application Objword = new Microsoft.Office.Interop.Word.Application();

                //creating the object of document class 
                Document objdoc = new Document();

                //get the uploaded file full path 
                dynamic FilePath = s;

                //pass the optional (missing) parameter to API 
                dynamic NA = System.Type.Missing;

                //open Word file document  
                objdoc = Objword.Documents.Open
                              (ref FilePath, ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA

                               );


                //creating the object of string builder class 
                StringBuilder sb = new StringBuilder();

                for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
                {
                    string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();

                    if (Filedata != string.Empty)
                    {
                        //Append word files data to stringbuilder 
                        sb.AppendLine(Filedata);
                    }

                }

                //closing document object  
                ((_Document)objdoc).Close();

                //Quit application object to end process 
                ((_Application)Objword).Quit();

                //assign stringbuilder object to show text in textbox 
                richTextBox1.Text = Convert.ToString(sb);
            }


            // Code to Find Email Ids from the file content.
            MatchCollection mc;
            mc = Regex.Matches(richTextBox1.Text, "([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})");
            string res = (mc.Count - 1).ToString();
            for (int i = 0; i < mc.Count; i++)
            {
                MessageBox.Show (mc[i].Value);
            }
        }