Tuesday, 30 December 2014

dATA

bll


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using dal;

namespace bll
{
  public  class BLmanager
    {
      DalMAnager Ob = new DalMAnager();
      public Master Load(Master Obj,string Mode)
      {
          return Ob.lOAD(Obj, Mode);

      }
      public void sAVE(Master Obj, string Mode)
      {
          Ob.SAVE(Obj, Mode);

      }
    }
}
-------------------

DAL


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using model;
namespace dal
{
    public class DalMAnager
    {

        string con = ConfigurationManager.ConnectionStrings["ConStng"].ConnectionString;


        public void SAVE(Master Objcts, string Mode)
        {
            SqlConnection cong = new SqlConnection(con);
            Master OBJ = new Master();
            TableMngr obct = new TableMngr();
            try
            {
                cong.Open();
                foreach (TableMngr ob in Objcts.TBL)
                {
                    obct.Address = ob.Address;
                    obct.PlceData = new PlaceManager()
                    {
                        PlaceName = ob.PlceData.PlaceName
                    };
                    obct.Phone = ob.Phone;
                }


                SqlCommand cmd = new SqlCommand("Sp_Load", cong);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Mode", Mode);
                cmd.Parameters.AddWithValue("@Pid", null);
                cmd.Parameters.AddWithValue("@Address", obct.Address);
                cmd.Parameters.AddWithValue("@PlaceName", obct.PlceData.PlaceName);
                cmd.Parameters.AddWithValue("@Phone", obct.Phone);
                cmd.ExecuteNonQuery();
            }

            catch (Exception e)
            {
            }
            finally
            {
                cong.Close();
                cong.Dispose();
            }

        }



        public Master lOAD(Master Objcts, string Mode)
        {
            SqlConnection cong = new SqlConnection(con);
            Master OBJ = new Master();
            OBJ.TBL = new TableMngrColl();
            OBJ.PLCOLL = new PlaceMngrColl();
            try
            {
                cong.Open();
                SqlCommand cmd = new SqlCommand("Sp_Load", cong);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Mode", Mode);
                cmd.Parameters.AddWithValue("@Pid", Objcts.PlId);
                cmd.Parameters.AddWithValue("@Address", null);
                cmd.Parameters.AddWithValue("@PlaceName", null);
                cmd.Parameters.AddWithValue("@Phone", null);
                DataSet ds = new DataSet();
                SqlDataAdapter ad = new SqlDataAdapter();
                ad.SelectCommand = cmd;
                ad.Fill(ds);
                DataTable PlDt = new DataTable();
                DataTable Dtils = ds.Tables[0];
                if (Mode == "LO")
                {
                    PlDt = ds.Tables[1];

                    if (PlDt.Rows.Count > 0)
                    {
                        foreach (DataRow Object in PlDt.Rows)
                        {
                            OBJ.PLCOLL.Add(new PlaceManager()
                            {
                                PlceId = Convert.ToString(Object["pID"]),
                                PlaceName = Convert.ToString(Object["pNAME"]),
                            });
                        }
                    }
                }
                else if (Mode == "LG")
                {

                    if (Dtils.Rows.Count > 0)
                    {
                        foreach (DataRow Object in Dtils.Rows)
                        {
                            OBJ.TBL.Add(new TableMngr()
                            {
                                Address = Convert.ToString(Object["address"]),
                                Phone = Convert.ToString(Object["phone"]),
                                PlceData = new PlaceManager()
                                {
                                    PlaceName = Convert.ToString(Object["pID"])
                                }
                            });
                        }
                    }

                }

            }
            catch (Exception e)
            {
            }
            finally
            {
                cong.Close();
                cong.Dispose();
            }
            return OBJ;
        }
    }
}

-----------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using model;
using bll;
public partial class _Default : System.Web.UI.Page
{
    string Mode = string.Empty;
    BLmanager ob = new BLmanager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadPlace();
        }
    }
    public void LoadPlace()
    {

        Master obj = new model.Master();
        obj.PlId = DropDownList1.SelectedValue;
        if (obj.PlId == "" || obj.PlId == null)
        {
            Mode = "LO";
            obj = ob.Load(obj, Mode);
            DropDownList1.DataSource = obj.PLCOLL;
            DropDownList1.DataTextField = "PlaceName";
            DropDownList1.DataValueField = "PlceId";
            DropDownList1.DataBind();
        }
        else
        {
            Mode = "LG";
            obj = ob.Load(obj, Mode);
            GridView1.DataSource = obj.TBL;
            GridView1.DataBind();
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadPlace();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadPlace();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        LoadPlace();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtAdd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAdd");
        TextBox txtPhone = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPhone");
        Master obj = new model.Master();
        obj.TBL = new TableMngrColl();
        TableMngr Tm = new TableMngr();
        Tm.Address = txtAdd.Text;
        Tm.Phone = txtPhone.Text;
        Tm.PlceData = new PlaceManager()
        {
            PlaceName = txtName.Text
        };

        obj.TBL.Add(Tm);
        Mode = "UP";
       ob.sAVE(obj, Mode);
    }
}

-------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace model
{
   public class Master
    {
       public PlaceMngrColl PLCOLL { get; set; }
       public TableMngrColl TBL { get; set; }
       public string PlId { get; set; }
    } 
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace model
{
   public class PlaceManager
    {

        public string PlceId { get; set; }
        public string PlaceName { get; set; } 


    }
    public class PlaceMngrColl:System.Collections.ObjectModel.ObservableCollection<PlaceManager>
    {
    }
  
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace model
{
   public class TableMngr
    {

        public PlaceManager PlceData { get; set; }

        public string Address { get; set; }

        public string Phone { get; set; }

    }
    public class TableMngrColl : System.Collections.ObjectModel.ObservableCollection<TableMngr>
    {
    }
}

USE [fthrsoft]
GO
/****** Object:  StoredProcedure [dbo].[Sp_Load]    Script Date: 01/01/2005 00:22:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Sp_Load]

@Mode varchar(5),
@Pid int=null,
@Address varchar(50)=null,
@PlaceName varchar(50)=null,
@Phone varchar(50)=null
AS
BEGIN
   
    SET NOCOUNT ON;

IF(@Pid is null)
SELECT     PL.pID, PL.pNAME, DT.phone, DT.address
FROM         dbo.Tbl_Place AS PL INNER JOIN
dbo.[tbl-Details] AS DT ON PL.pID = DT.Pid
else
SELECT     PL.pID, PL.pNAME, DT.phone, DT.address
FROM         dbo.Tbl_Place AS PL INNER JOIN
dbo.[tbl-Details] AS DT ON PL.pID = DT.Pid
where PL.pID=@Pid

IF(@Mode='LO')
SELECT     pID, pNAME
FROM         dbo.Tbl_Place AS PL
END

IF @Mode='UP'
begin

update A set phone=@Phone,address=@Address
from [tbl-Details] as A
inner join Tbl_Place as B ON A.pID = B.Pid
WHERE A.pID=@PlaceName
end

Friday, 11 April 2014

ASP Wizard

Email Registration Application

Let us create a small web application which will allow is to take input from the user using the wizard control.
1. Open a new web site project as shown below.
1s.jpg
2. Add a wizard control to the project from the toolbox and name it as EmailRegWizard. By default you will notice there are 2 steps in the wizard.
2s.jpg
3.Increase the size of the wizard control as shown below. We will now add 3 more steps to the wizard control. To add steps to the wizard control let us select the wizard control, right click and select Add\Remove Wizard Steps.
3s.jpg
4. WizardStep Collection Editor will be launched. Click on Add and select WizardStep. (If you click on add directly a wizard will be added).Add 2 more steps to the wizard control as shown below. Totally you should have 5 steps in the wizard as shown below.
4s.jpg 5s.jpg
5.Let us now rename each step of the wizard control depending on the activity that we are going to perform in that step.
In the first step we will take the basic user information,the second step wll address details for email, third step will address the details for forgot password, in the fourth step we will ask users about their interests and 5th is the final step in which the user will have to accept the various terms and conditions. Now let us rename each of these steps as follows (as shown below):
Step 1 -- User Info
Step 2 -- Email Details
Step 3 -- Forgot Password
Step 4 -- Interests
Step 5 -- Terms&Conditions
6s.jpg
6. For the first step let us set the StepType to "Start" and for the last step let us set the step type to "Finish". After setting the step type click on "OK".
7.JPG
8.JPG
7. now let us add some controls for each step. For the first step we will add the basic user details like firstname, lastname. Gender, State, PinCode ,Alternate Email Address. Please refer to the below image and code.
9s.jpg
<asp:WizardStep runat="server" StepType="Start" Title="User Info">
<table cellpadding=0 cellspacing=0 width="100%">
 <tr>
     <td>
       First Name: 
     </td>
     <td>
       <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
     </td>
     <td>
       Last Name: 
     </td>
     <td>
        <asp:TextBox ID="LastName" runat="server"></asp:TextBox>
     </td>                            
 </tr>
 <tr>
     <td>
       Gender: 
     </td>
     <td>
       <asp:DropDownList ID="Gender" runat="server">
         <asp:ListItem>Male</asp:ListItem>
         <asp:ListItem>Female</asp:ListItem>
       </asp:DropDownList>
     </td>
 </tr>                        
 <tr>
     <td>
       State:
     </td>
     <td>
      <asp:DropDownList ID="State" runat="server">
        <asp:ListItem>Andaman and Nicobar Islands</asp:ListItem>
        <asp:ListItem>Andhra Pradesh</asp:ListItem>
        <asp:ListItem>Arunachal Pradesh</asp:ListItem>
        <asp:ListItem>Assam</asp:ListItem>
        <asp:ListItem>Bihar</asp:ListItem>
        <asp:ListItem>Chandigarh</asp:ListItem>
        <asp:ListItem>Chattisgarh</asp:ListItem>
        <asp:ListItem>Dadra and Nagar Haveli</asp:ListItem>
        <asp:ListItem>Daman and Diu</asp:ListItem>
        <asp:ListItem>Delhi</asp:ListItem>
        <asp:ListItem>Goa</asp:ListItem>
        <asp:ListItem>Gujarat</asp:ListItem>
        <asp:ListItem>Haryana</asp:ListItem>
        <asp:ListItem>Himachal Pradesh</asp:ListItem>
        <asp:ListItem>Jammu and Kashmir</asp:ListItem>
        <asp:ListItem>Jharkhand</asp:ListItem>
        <asp:ListItem>Karnataka</asp:ListItem>
        <asp:ListItem>Kerala</asp:ListItem>
        <asp:ListItem>Lakshadweep</asp:ListItem>
        <asp:ListItem>Madhya Pradesh</asp:ListItem>
        <asp:ListItem>Maharashtra</asp:ListItem>
        <asp:ListItem>Manipur</asp:ListItem>
        <asp:ListItem>Meghalaya</asp:ListItem>
        <asp:ListItem>Mizoram</asp:ListItem>
        <asp:ListItem>Nagaland</asp:ListItem>
        <asp:ListItem>Orissa</asp:ListItem>
        <asp:ListItem>Pondicherry</asp:ListItem>
        <asp:ListItem>Punjab</asp:ListItem>
        <asp:ListItem>Rajasthan</asp:ListItem>
        <asp:ListItem>Sikkim</asp:ListItem>
        <asp:ListItem>Tamil Nadu</asp:ListItem>
        <asp:ListItem>Tripura</asp:ListItem>
        <asp:ListItem>Uttarakhand</asp:ListItem>
        <asp:ListItem>Uttaranchal</asp:ListItem>
        <asp:ListItem>Uttar Pradesh</asp:ListItem>
        <asp:ListItem>West Bengal</asp:ListItem>
      </asp:DropDownList>                                
    </td>
 </tr>
 <tr>
     <td>
       Pin Code: 
     </td>
     <td>
       <asp:TextBox ID="PinCode" runat="server"></asp:TextBox>
     </td>
 </tr>
  <tr>
     <td>
        Alternate Email: 
     </td>
     <td>
       <asp:TextBox ID="AltEmail" runat="server"></asp:TextBox>
     </td>
 </tr>
</table>  
</asp:WizardStep>
After adding the user info we will take the email details like Email Address, Password, and Confirm Password.
<asp:WizardStep runat="server" Title="Email Details">
<table>
 <tr>
     <td>
       Login Name: 
     </td>
     <td>
        <asp:TextBox ID="LoginName" runat="server"></asp:TextBox>
     </td>                        
 </tr>
 <tr>
     <td>
         Password: 
     </td>
     <td>
         <asp:TextBox ID="Password" runat="server" TextMode="Password">
</asp:TextBox>
     </td>                      
 </tr>
 <tr>
     <td>
        Confirm Password: 
     </td>
     <td>
        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password">
        </asp:TextBox> 
     </td>
 </tr>
</table> 
</asp:WizardStep>
10s.jpg
Similarly we will add controls for the last three steps.
Step 3
<asp:WizardStep runat="server" Title="Forgot Password">
<table cellpadding=0 cellspacing=0 width="100%">
 <tr>
     <td>
       Hint Question: 
     </td>
     <td>
       <asp:DropDownList ID="HintQuestion" runat="server">
        <asp:ListItem>What is the name of your first school?</asp:ListItem>
        <asp:ListItem>What is your favourite pass-time?</asp:ListItem>
        <asp:ListItem>What is your mother's maiden name?</asp:ListItem>
        <asp:ListItem>What is your favourite food?</asp:ListItem>
        <asp:ListItem>What is your exact time of birth?</asp:ListItem>
       </asp:DropDownList>
     </td>
 </tr>
 <tr>
     <td>
      Hint Answer: 
     </td>
     <td>
       <asp:TextBox ID="HintAnswer" runat="server" TextMode="Password"></asp:TextBox>
     </td>
 </tr>        
</table>
</asp:WizardStep>
11s.jpg
Step 4
<asp:WizardStep runat="server" Title="Interests">
    <table cellpadding=0 cellspacing=0 width="100%">
 <tr>
     <td>
  <asp:CheckBox ID="News" runat="server" Text="News"/>
     </td>
     <td>
  <asp:CheckBox ID="HnF" runat="server" Text="Home &amp; Family"/>
     </td>
     <td>
  <asp:CheckBox ID="HnN" runat="server" Text="Health &amp; Nutrition"/>
     </td>
     <td>
  <asp:CheckBox ID="Auto" runat="server" Text="Automobiles"/>
     </td>                            
 </tr>
 <tr>
     <td>
  <asp:CheckBox ID="Education" runat="server" Text="Education"/>
     </td>
     <td>
  <asp:CheckBox ID="Loand" runat="server" Text="Loans"/>
     </td>
     <td>
  <asp:CheckBox ID="Travel" runat="server" Text="Travel"/>
     </td>
     <td>
  <asp:CheckBox ID="Computers" runat="server" Text="Computers"/>
     </td>                            
 </tr>                        
 <tr>
     <td>
  <asp:CheckBox ID="Shopping" runat="server" Text="Shopping"/>
     </td>
     <td>
  <asp:CheckBox ID="Insurance" runat="server" Text="Insurance"/>
     </td>
     <td>
  <asp:CheckBox ID="Beauty" runat="server" Text="Beauty "/>
     </td>
     <td>
  <asp:CheckBox ID="Fashion" runat="server" Text="Fashion"/>
     </td>                            
 </tr>
 <tr>
     <td>
  <asp:CheckBox ID="Sports" runat="server" Text="Sports "/>
     </td>
     <td>
  <asp:CheckBox ID="OnlineGames" runat="server" Text="Online Gaming"/>
     </td>
     <td>
  <asp:CheckBox ID="SpecialOffers" runat="server" Text="Special Offers"/>
     </td>
     <td>
  <asp:CheckBox ID="Family" runat="server" Text="Family"/>
     </td>                            
 </tr>
    </table>
</asp:WizardStep>
Step 5
<asp:WizardStep runat="server" StepType="Finish" Title="Terms&amp;Conditions">
    <table>
 <tr>
     <td style="height: 294px">
  <asp:TextBox ID="TermsAndConditions" runat="server" TextMode="MultiLine" 
  Height="323px" Width="337px" Text="Terms &amp; Conditions" Font-Bold="True" 
  Font-Names="Impact" Font-Size="XX-Large"></asp:TextBox> 
     </td>
 </tr>                
    </table>
</asp:WizardStep>
13s.jpg
After creating the wizard steps, let us now load the page and execute all the steps of the wizard. You should be able to use the wizard control without any code. Now let us add a few validators for FirstName and Gender on the first step and for login name on the second step.
<asp:RequiredFieldValidator ID="FirstNameVal" runat="server" ErrorMessage="!" 
ControlToValidate="FirstName"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="GenderVal" runat="server" ErrorMessage="!" 

ControlToValidate="Gender"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="LoginVal" runat="server" ErrorMessage="!" 

ControlToValidate="LoginName"></asp:RequiredFieldValidator>
I have added validators for the first and second step only you can add them for all the steps. On execution you will notice that if the first name is not keyed in and the Gender is not selected on clicking the next button the wizard does not move forward. The same behaviour is repeated if we click on "Next" without entering the LoginName. But the validators don't fire if we click the "Previous" Button.
Similarly we can fill all the details of email registration. After filling in all the details and clicking on the "Finish" button in the last step. We must display a success message after saving the data successfully. Let us assume that we have saved out data in the DB and the registration was successful. Hence clicking on "Finish" we will redirect the user to the "Message.aspx". Wizard control has a lot of events like:
  • ActiveStepChanged
  • CancelButtonClick
  • FinishButtonClick
  • NextButtonClick
  • PreviousButtonClick
  • SideBarButtonClick
Let us trap the FinishButtonClick event and redirect to Message.aspx. Let us add an another page called Message.aspx to the project. The startup page of the project should be Default.aspx. In the Message.aspx we will add the below code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Message:  System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextMessage.Text = Convert.ToString(this.Request.QueryString["FirstName"]) 
+ " has been successfully registered." ;
    }
}

In the Default.aspx we will add the below code for the FinishButtonClick event

    protected void EmailRegWizard_FinishButtonClick(
        object sender, WizardNavigationEventArgs e)
    {
        Response.Redirect("Message.aspx?FirstName=" + ((TextBox) 

EmailRegWizard.WizardSteps[0].FindControl("FirstName")).Text);   
    }
Let us execute on running the application. You should be redirected to Message.aspx on clicking the Finish Button.




Thursday, 10 April 2014

Connection String in Asp.Net

In WEB CONFIG....


SYNTAX

<add name="ConnectionStringName"  connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>

  

 EXAMPLE :

<connectionStrings>
<add name="Con" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

We can use this connection string in our project.....


1.First Method

SqlConnection con = new SqlConnection("data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" );
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();

 

 

2.2nd method

 

 

 
<connectionStrings>
    <add name="ConnectionStringName" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=DatabaseName;User ID=username;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
 
 
 
Add a Class File
 
 
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
 
public class WFDFeedsNews
{
    SqlConnection con=new SqlConnection();
    SqlCommand cmd=new SqlCommand();
 
    public createConnection()
    {
 
            con = new SqlConnection(ConfigurationManager.ConnectionStrings                                                             ["connectionStringName"].ConnectionString);
}
 
 public insertData(int id,string name)
    {
 
            con.Open();
 
            try
            {
 
                cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "INSERT INTO (userID, HeadLine)
                       VALUES (@id,@headline)";
 
                cmd.Parameters.AddWithValue("@id",id);
                cmd.Parameters.AddWithValue("@headline",name);
 
                int result=con.executeNonQuery();
 
            }
            catch (Exception ex)
            {
 
            }
            finally
            {
                con.Close();
            }
 
        }
 
    }
 
}
 
Pass data from aspx page to this code...