First character in upper case in SQL

Hi,
Below are the logic for Set first character in Upper case and rest of all set in lower case in MS sql code Like

input = ‘sql data’
output = ‘Sql Data’

Below are the code for that.

declare @inputString varchar(20)
set @inputString = 'AMIT PATEL'

declare @position int

set @position = 1
SET @inputString = Upper(substring(@inputString,1,1))+ LOWER(substring(@inputString,2,len(@inputString)-1))
WHILE @position < DATALENGTH(@inputString)
Begin
set @position = charindex(' ',@inputString,@position+1)
if @position = 0
goto done
SET @inputString = REPLACE(@inputString,substring(@inputString,@position,2),Upper(substring(@inputString,@position,2)))
End
DONE:
PRINT @inputString

About these ads

Auto fill text box in WPF

Below code is helpful to WPF developer to create auto fill text box. Like we are want to create Client list in drop-down but that list is very large so we can provide auto fill text box where user enter any character and we can show relevant list in below of the text box.

I have created one user control in WCP so any can use with set appropriate properties.

Below are the UI for this controls.

Now below the xmal for this controls…

<UserControl x:Class=”MyProject.AutoFillTaxBox”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
Height=”80″ Width=”233″>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”84*” />
<ColumnDefinition Width=”4*” />
<ColumnDefinition Width=”145*” />
</Grid.ColumnDefinitions>
<TextBox x:Name=”txtSearchtext” Height=”25″ VerticalAlignment=”Top” Grid.ColumnSpan=”3″ KeyDown=”txtSearchtext_KeyDown” PreviewKeyDown=”txtSearchtext_PreviewKeyDown” />
<ListBox x:Name=”SuggetionList” Margin=”0,25,0,9″ Visibility=”Collapsed” SelectionChanged=”SuggetionList_SelectionChanged” ScrollViewer.CanContentScroll=”True” Grid.ColumnSpan=”3″ />
</Grid>
</UserControl>

Also below are the code for this..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyProject
{
/// <summary>
/// Interaction logic for AutoFillTaxBox.xaml
/// </summary>
public partial class AutoFillTaxBox : UserControl
{
public delegate void SetIDs(int ID);
public SetIDs functionIDSet;
public AutoFillTaxBox()
{
InitializeComponent();
txtSearchtext.TextChanged += new TextChangedEventHandler(txtSearchtext_TextChanged);
}
private List<SuggestedList> _DataSource = null;
private string _SelectedText;
private int _SelectedID;
public List<SuggestedList> DataSource
{
set
{
_DataSource = value;
}
get
{
return _DataSource;
}
}
public string SelectedText
{
set
{
txtSearchtext.Text = value;
}
get
{
return txtSearchtext.Text.Trim();
}
}
public int SelectedID
{
set
{
_SelectedID = value;
functionIDSet.Invoke(_SelectedID);
}
get
{
return _SelectedID;
}
}
private void txtSearchtext_TextChanged(object sender, TextChangedEventArgs e)
{
List<SuggestedList> oAutoFillList = new List<SuggestedList>();
oAutoFillList.Clear();
foreach (SuggestedList item in _DataSource)
{
if(!string.IsNullOrEmpty(txtSearchtext.Text))
{
if (item.Text.ToUpper().StartsWith(txtSearchtext.Text.ToUpper()))
{
oAutoFillList.Add(item);
}
if (item.Text.ToUpper() == txtSearchtext.Text.ToUpper())
{
_SelectedID = item.ID;
}
}
}
if (oAutoFillList.Count > 0)
{
SuggetionList.ItemsSource = oAutoFillList;
SuggetionList.DisplayMemberPath = "Text";
SuggetionList.Visibility = Visibility.Visible;
}
else
{
SuggetionList.Visibility = Visibility.Collapsed;
SuggetionList.ItemsSource = null;
}
}
private void SuggetionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SuggetionList.ItemsSource != null)
{
SuggetionList.Visibility = Visibility.Collapsed;
// Remove event handler because that shoudl not be fire up to execution
txtSearchtext.TextChanged -= new TextChangedEventHandler(txtSearchtext_TextChanged);
if (SuggetionList.SelectedIndex != -1)
{
txtSearchtext.Text = ((SuggestedList)(SuggetionList.SelectedItem)).Text;
this.SelectedID = ((SuggestedList)(SuggetionList.SelectedItem)).ID;
}
/// Add event handler after change text value.
txtSearchtext.TextChanged += new TextChangedEventHandler(txtSearchtext_TextChanged);
}
}
private void txtSearchtext_KeyDown(object sender, KeyEventArgs e)
{
if (SuggetionList.ItemsSource != null && e.SystemKey == Key.LeftAlt)
{
SuggetionList.Focus();
}
}
private void txtSearchtext_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (SuggetionList.ItemsSource != null && e.SystemKey == Key.LeftAlt)
{
SuggetionList.Focus();
}
}
}
public class SuggestedList
{
public SuggestedList(int iID, string sText)
{
this.ID = iID;
this.Text = sText;
}
public int ID { set; get; }
public string Text { set; get; }
public override string ToString()
{
return this.Text;
}
}
}

So here you can use public property to create this control as generic and use it….
  • DataSource
  • Delegate to handle get any of the custom event on selection change.
  • selected ID
  • set Selected text in case if edit..
Let me know for more details on this control.

PrintDialog in PDFSharp Control C# code

For implementing a print functionality if we want to user to choise to set their own setting while pringing so bettor option is that provide print dialig box rather then provide fix settings.

Dialog box is shows as below.

Print Dialogbox

If we are using PDFsharp control then we have to capture settings from this dialog and then need to pass it to PDFSharp controls and then execture pdfsharp document’s print command.

Below are the code for that..

PrinterSettings settings = new PrinterSettings();

PrintDialog oDiaLog = new PrintDialog();

 

 

if (oDiaLog.ShowDialog() == DialogResult.OK) 

 

{  

 

PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(oDiaLog.PrinterSettings);

this.PdfDocument.Print(pdfPrintSettings); // Here PDFDocument is document of PDF sharp objects.

 

}

 

PringDialog allows user to set their own printer settings and based on that set request to selected printers.

Singleton pattern used in window application



Below are the code for how to use singleton patterns.

This structure will ensure that classes have only instance is presence globally for an application. This structure is mainly used in remoting application were multiple client is connected and all are accessing same instance so if changes done by any of the client then all client can get updated object every time as well server also updated with updated.

i.e. In server we have to show connected total client details then we have to initiate only one object and after then data would be added in the same object.

This also one common use in window application where if one form open and use try to open another form then use that instance of that form rather than open a new instance.

Below are the codes for that approach.

 

public partial class ShowReports : Form

    {

 

private int InvoiceID;

        public ShowReports()

        {

            InitializeComponent();

        }

        private static ShowReports instance;

 

        public static ShowReports Instance

        {

            get

            {

                if (instance == null)

                {

                    instance = new ShowReports();

                }

                return instance;

            }

        }

        public ShowReports(int intInvoiceID)

        {

            InitializeComponent();

            InvoiceID = intInvoiceID;

        }

        public void ShowReportsInit(int intInvoiceID)

        {

            InvoiceID = intInvoiceID;

            this.Refresh();

        }

}

Here instance is static member of this class that is preserve value of open instance,

 

 

Execute this class.

ShowReports oReports = ShowReports.Instance;

oReports.Show();

 

Here Instance is static member so directly can access that without create object and inside that property implementation code if we have object exist then use that same one else create new instance.

 

This approach ensures that only one instance is created and only when the instance is needed

 

Model View Presenter Architecture

Purpose:

As UI-creation technologies such as ASP.NET and Windows® Forms become more and more powerful, it’s common practice to let the UI layer do more than it should. Without a clear separation of responsibilities, the UI layer can often become a catch-all for logic that really belongs in other layers of the application. One design pattern, the Model View Presenter (MVP) pattern, is especially well suited to solving this problem

Benefits of MVP:

  • In this Pattern application make independent from its platform so when ever requirement comes to move to window to web or any other version its    only  need to change on code behind page for view Implementation.
  • So application stays loose couples from code behind page with business layer or any other next latter.
  • MVP is purely loose couples from any plate form so that it is very use to prepare automated unit test cases.
  • Keeps code more maintainable throughout the lifetime of the project, especially during the maintenance phase.

Overall application architecture:

MVP Class Diagram:

1.       Model: Model is UI interface for how and what are the data need to displayed.

2.       View : Show the representation of model, that can be build in presenter

3.       Presenter : Build all data for model and pass all data to model in form of view by communicating with services class( or business logic layer)

Below is the MVP patterns example with service class

1.       User Interface Layer

In this section contains actual UI, In .net terms its aspx or ascx pages.

2.       Presentation Layer( MVP :- Code behind + view (interfaces) + Presenter)

In this layer we have designed as MVP patters so its divided in below three parts.

a.       View – It is interfaces. This interface contains signature of all properties and events that needs to implement in code behind page that will be use in presenter class.

I.e.

Public Interface Icustomer

ReadOnly Property CustomerName()

WriteOnly Property DisplayMessage()

End Interface

b.      Code Behind – In this class we need to implement View.(No need to specify any logic). We are just implement properties which is defined in view.

#Region "Member"

Dim Presnter As CustomerPresenter

#End Region

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Presnter = New CustomerPresenter(Me)

Presnter.LoadData()

End Sub

ReadOnly Property CustomerName() Implements ICustomer.CustomerName

Get

Return textCustomerName.Text

End Get

End Property

WriteOnly Property DisplayMessage() Implements ICustomer.DisplayMessage

Set(ByVal value)

Response.Write(value)

End Set

End Property

c.       Presenter – In this class we are using all those members which we have specified in view and implemented in code behind. Presenter class is responsible for execute methods of business layer.

Public Class CustomerPresenter

Private iView As ICustomer

Private SessionProvider As ISessionProvider

Public Sub New(ByVal view As ICustomer)

Me.iView = view

End Sub

‘Public Sub New(ByVal view As ICustomer, ByVal SessionProvider As ISessionProvider)

‘ Me.iView = view

‘ Me.SessionProvider = SessionProvider

‘End Sub

Public Sub LoadData()

Me.iView.DisplayMessage = Me.iView.CustomerName

End Sub

End Class

Summary:

ü  MVP is some how lengthy process to implement and understanding on initial phase, but later on that will be very useful  to application maintainability on application maintenance phase, increase code reusability because all code is separate from UI.

ü  So all projects MVP is not a best approaches but yes on some large scale project and we need to think on project maintainability, reusability and also required automated test cases methods that case this is best approaches for this scenario.


Check Logged in user in every page

Hi

This helps in every asp.net web application we have put some mechanisum in every page so that user can not access any of the page without loged in

If user trying to it by writing it in query string then it will redirect to login page.

So u can use below code with minumun line of codes.

Create one BasePage class, with below code and that will need to inhered by all pages.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AmitCode
{
public class BasePage : System.Web.UI.Page
{

protected override void OnLoad(EventArgs e)
{
if (Session["UserID"] != null && Convert.ToInt32(Session["UserID"]) > 0)
{
base.OnLoad(e);
}
else
{
Response.Redirect(@"~/Login.aspx");
}

// do stuff after Load-event is raised
}

}
}

and that class need to inherited by each pages. hence if user try to access any pages without login then session is get null and system redirect that user to login page.
Below are the code for inheritance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;;

namespace AmitCode
{
public partial class CandidateManage : BasePage
{

}
}

Here I am using session to check loged in that we can check by form authentication also.

Thanks,
Amit Patel

Follow

Get every new post delivered to your Inbox.

Join 239 other followers