Tuesday, December 23, 2014

WPF - Set Startup Window Based on Some Condition

Here I will like to explain you that how to set startup form in WPF window application based on non-conditional and conditional.

choose startup window without any condition

look into App.xaml
Change default xaml startuoUri with your file : StartupUri="MainWindow.xaml" 

<Application x:Class="YourProject.App"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Startup="MainWindow.xaml">

choose startup window with some condition

look into App.xaml
remove StartupUri="MainWindow.xaml" 
add Startup="Application_Startup" new event Handler

<Application x:Class="YourProject.App"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Startup="Application_Startup">

From code behind App.xaml.cs create Application_Startup event as follow:
private void Application_Startup(object sender, StartupEventArgs e)
    {
        //add some startup logic
        var identity = AuthService.CheckLogin();
        if (identity == null)
        {
            //if identity is null then show login window
            //LoginWindow is your win form(Xaml File)
            LoginWindow login = new LoginWindow();
            login.Show();
        }
        else
        {
            //if identity is not null then show main window
            MainWindow mainView = new MainWindow();
            mainView.Show();
        }
    }


Sunday, December 14, 2014

Delete Multiple Rows in Gridview using Checkbox in Asp.net

Here I will explain how to delete multiple rows in gridview using checkbox in asp.net in c# with confirmation message box.

Before use this example first design one table UserData in your database as shown below.

Column Name
Data Type
Allow Nulls
UserID
Int(IDENTITY=TRUE)
No
UserName
varchar(100)
Yes
FirstName
varchar(100)
Yes
LastName
varchar(100)
Yes

Once above table created in database then enter some data to test application after that write the following code in your aspx page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>delete Multiple Rows from gridview in asp.net using checkbox</title>
<script type="text/javascript">
function Confirmation() {
var result = confirm('Are you sure you want to delete selected User(s)?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserData" DataKeyNames="UserID" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserID" DataField="UserID" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<asp:Button ID="btnDelete" Text="Delete Records" runat="server" Font-Bold="true" OnClientClick="javascript:return Confirmation();" onclick="btnDelete_Click" />
</div>
</form>
</body>
</html>

Now goto code behind file(press F7 key) and write the following code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DeleteRows : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserData();
}
}
protected void BindUserData()
{
DataTable dt = new DataTable();

SqlConnection con = new SqlConnection("Data Source=vibhavdb;Integrated Security=true;Initial Catalog=SampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UserData", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
gvUserData.DataSource = dt;
gvUserData.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in gvUserData.Rows)
{
//Finiding checkbox control in gridview for particular row
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkSelect");
//Condition to check checkbox selected or not
if (chkdelete.Checked)
{
//Getting UserId of particular row using datakey value
int usrid = Convert.ToInt32(gvUserData.DataKeys[gvrow.RowIndex].Value);

SqlConnection con = new SqlConnection("Data Source=vibhavdb;Integrated Security=true;Initial Catalog=SampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("delete from UserData where UserID=" + usrid, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
BindUserData();
}
}