-->

Monday, April 27, 2015

Creating a Database in SQL Mgt Studio 2012

Creating a Database is to occupy some space in physical drive for the records saving through the application. These saved records can easily be listed or modified as per user’s requirement. To perform operations like add/edit/delete on these records a person used to create some application having capability of doing so. In earlier article, we have installed SQL Server Management Studio 2012 following some easy steps. This time we will establish a connection with server and create a database EmpDb having only a single table Employee. Start Management studio through all programs or whatever shortcut you have on your system and follow these steps.
  1. First screen will prompt you to establish a connection with server requiring some values as      SQL Server Management Studio
  2.        
  3.  below: Just write (LocalDb)\v11.0 because i have only local db installed in my system, you can change as per your server installation.
  4. On the left side there are some options "Database, Security, Replication, Management" are pre-added. Right click on Database and select New Database, a window will show as below prompting database name:                                                                                                                
    New Database in SQl server management studio
  5. Enter database name "EmpDb" and click on Ok button, it will create a new database with none of tables, views. Now look out the server connection there is a new entry of your database as shown:                                                                                                                                              
    Enter database name "EmpDb
  6. Right click on tables and select New Table, it will show you editor having three columns name, data-types and nullable. Write some columns with their data-types, don’t forgot to make Id a primary key and Identity set to true. Identity property will auto-increment this columns according to data entry. Save this table and enter name “Employee”. You can see the columns and their data-types in the below image:                                                                                          
    New Table in sql server
Your database have easily been created with a single table and some columns. In the next article we create an edmx file for this database to be used for MVC application and will perform operation on this database.

Saturday, April 25, 2015

Nested Gridview example in asp.net c#

Before we talk about nested gridview asp.net c#. First we talk about gridview control. It is a data source control through which we can bind data in the form of table. In this article, we will take two gridview asp.net controls and we will arrange both control in this way, please see the mentioned picture.

Lets to start how to design it

  1. First to add asp.net gridview in the source page
  2. Add a <Columns> tag inside it.
  3. Add single column inside it using TemplateField with HeaderText property, if you want to add more than one column then add equal number of TemplateField inside it.
  4. Show the data in the browser with the help of ItemTemplate, so add it inside Template Field.
  5. Now, the article's main point is here, Add another gridview asp.net controls inside the ItemTemplate, Now your code look like:
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
     <Columns>
    <asp:TemplateField HeaderText="Subject Marks">
     <ItemTemplate>
     <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
     </asp:GridView>
     </ItemTemplate>
    </asp:TemplateField>
     </Columns>
     </asp:GridView>
  6. Similarly again, repeat the step 2 to 4 for second nested gridview asp.net control. Now, add three columns by using Template Field.
  7. Bind all columns inside the ItemTemplate field of second gridview control, Please see the full code which is mentioned below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Subject Marks">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Hindi">
<ItemTemplate>
<asp:Label ID="hindilabel" runat="server" Text='<%# Eval("hindi") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Maths">
<ItemTemplate>

<asp:Label ID="mathslabel" runat="server" Text='<%# Eval("maths") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="English">
<ItemTemplate>

<asp:Label ID="englishlabel" runat="server" Text='<%# Eval("English") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Friday, April 24, 2015

Display filter ads on the webpage in ASP.NET

If we want to display ads on the webpage then we use Adrotator control in ASP.NET. Without filter ads adrotator control display all ads, which is exist in the xml file. But all ads are not good for website owner because only a content related ad earn more money compare to all ads. So lastly developers decide that they have to design user choice ad system. The KeywordFilter property is created by visual studio developers for ads filtration. Through this property you can display only those ads, which is related to content.

How to use KeywordFilter Property

Advertisements.xml file
<ad>

<Keyword> name of the keyword </Keyword>

<ad>
Adrotator control property
KeywordFilter = name of the keyword which is mentioned in Advertisements file
For more clearance please watch this video:

Thursday, April 23, 2015

Installing SQL Server Mgt Studio 2012

Installing SQL Server is as simple as installing a software following a list of steps. SQL Server 2012 was released in April 2012 and because of its list of features and easiness to use made favorite among professionals and beginners. We will learn about to creating database in SQL Server Management studio after installing it in the system.
Microsoft SQL Server is a powerful and free data management tool, designed for easy development and utilities including:
  • LocalDB (MSI installer) LocalDB is a lightweight version of Express that has all its programmability features. It can be bundled with Application and Database Development tools like Visual Studio and or embedded with an application that needs local databases.
  • Express (Containing only the database engine) Use this if you need to accept remote connections or administer remotely.
  • Express with Tools (with LocalDB) Choose either LocalDB or Express depending on your needs above.
  • SQL Server Management Studio Express (Tools only) This does not contain the database, but only the tools to manage SQL Server instances, including LocalDB, SQL Express, SQL Azure, etc. Use this if you already have the database and only need the management tools.
  • Express with Advanced Services (contains the database engine, Express Tools, Reporting Services, and Full Text Search) This is a larger download than "with Tools" as it also includes both Full Text Search and Reporting Services.
Here are the steps to installing SQL Server Management studio 2012:
  1. Download the executable file from the Microsoft official site. Double click on the .exe file and "SQL Server installation center" window will open.                                                    
    Installing SQL Server Mgt Studio 2012
  2. Select first option "New SQL server stand-alone installation" and it will check all the support rules with a progress bar.                                                                                                  
    New SQL server stand-alone installation
  3. After completing rule check it will opt for license terms that must accept by you for installation. User may also select the option to send feature data to Microsoft or just ignore this checkbox.                                                                                                                         
    User may also select the option to send feature data to Microsoft
  4. It will then install setup files used for preparing the installation into your system showing progress to user. On this step if your system is connected to internet connection then any update available will also be installed with this installation.             
     install setup files used for preparing the installation
  5. Next step will opt for feature selection to be installed as shown in the image. Some features must be installed and those features are only readable as LocalDB and Connectivity tools                                                                                                                              
     only readable as LocalDB and Connectivity tools
  6. User can help Microsoft to SQL server features and services with selecting a checkbox of Error reporting. It is optional.                                                                                                    
     Microsoft to SQL server features
  7. Important step in which the installation process continues with copying all the required files on the system. This step takes more time because of copying required files.                                                                                                                                                       
    Important step in which the installation process
  8. The last step of installation to confirm about installation have successfully completed or having some errors. Successfully completed window will show with list of all the features installed with this installation.                                                                                        
    installation to confirm about installation have successfully completed
Completing these steps you can easily access SQL Server management studio to create database. We will create database with some tables in next article.

Nested gridview asp.net c# with expand and collapse example

In previous example we have already learn about nested gridview asp.net . Now, today we will learn nested gridview asp.net  with expand and collapse feature. Parent Gridview bind with country database table and child Gridview bind with State database table when we select any country. Child Gridview appeared according to country_id column like. Lets start to design parent GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">

<Columns>

<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:expandcollapse('<%# Eval("country_id") %>');">
<img src="plus.gif" border="0" id='img<%#Eval("country_Id") %>' />

</ItemTemplate>

</asp:TemplateField>
<asp:BoundField DataField="country_Id" HeaderText="Country Id" />
<asp:BoundField DataField="country_name" HeaderText="Country Name" />

</Columns>

</asp:GridView>
Here,
  1. AutoGenerateColumns="false" means, automatically generated columns will not display in the Gridview.
  2. OnRowDataBound="GridView1_RowDataBound" means, raise event after bind the parent gridview for next child gridview.
  3. Using template field we create a column in the gridview.
  4. ItemTemplate used for displaying data in the browser.
  5. Both anchor and image tag bind with the country_id using Eval method. Also call a javaScript method when we click on image. Script method define later in the post.
  6. Bind two column with table , now the design window look like
Nested gridview asp.net c# with expand and collapse example

Now, add nested Gridview  using Template field. Similarly again create columns for state table but remember that state table get from country_id so also take country_id inside the template field, see the code
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id='<%# Eval("Country_Id") %>' style="display:none" />

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="state_Id" HeaderText="State Id" />
<asp:BoundField DataField="state_name" HeaderText="State Name" />

</Columns>

</asp:GridView>

</td>

</tr>

</ItemTemplate>

</asp:TemplateField>
Here,
  1. Bind division with the country_Id and set display style as none so its not visible.
  2. GridView with two data Field that are state_Id and state_name.
Now the whole source code is :
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">

<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:expandcollapse('<%# Eval("country_id") %>');">
<img src="plus.gif" border="0" id='img<%#Eval("country_Id") %>' />


</ItemTemplate>


</asp:TemplateField>
<asp:BoundField DataField="country_Id" HeaderText="Country Id" />
<asp:BoundField DataField="country_name" HeaderText="Country Name" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id='<%# Eval("Country_Id") %>' style="display:none" />

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="state_Id" HeaderText="State Id" />
<asp:BoundField DataField="state_name" HeaderText="State Name" />

</Columns>

</asp:GridView>

</td>


</tr>


</ItemTemplate>


</asp:TemplateField>

</Columns>


</asp:GridView>
</div>
</form>
Now, come to the javaScript function which is calling from anchor tag in first TemplateField. Now, copy this code and paste into your <Script> block.
<script>
function expandcollapse(name)
{
var div = document.getElementById(name);
var img = document.getElementById('img' + name);
if(div.style.display=='none')
{
div.style.display = "inline";
img.src = "minus.gif";
}
else
{
div.style.display = "none";
img.src = "plus.gif";
}}

</script>
Here,
  1. <Script> tag, which is include in <head> section
  2. expandcollapse is the function name in which we have a parameter named "name".
  3. parameter take country_id as a value
  4. we have two variable first is used for division which is hide in the second template field and second variable is used for image which is changed according to expand and collapse.
  5. display inline means state table open in the parent table boundary.
Now, come to the code file. The following code demonstrates how to bind  the GridView control with the DataSet also demonstrate how to wok with RowDataBound event.
public partial class Default21 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
bindgridview();

}

}

private void bindgridview()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["btti"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("select * from [country]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["btti"].ToString();
con.Open();
GridView gv = (GridView)e.Row.FindControl("GridView2");
int countryid = Convert.ToInt32(e.Row.Cells[1].Text);
SqlCommand cmd = new SqlCommand("select * from [state] where country_id="+countryid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gv.DataSource = ds;
gv.DataBind();

}
}
}

Here,
  1. First to bind the gridview with DataSet.
  2. In the RowDataBound event, first to check the selected row is DataRow.
  3. Now, find the nested gridview in the block and bind it with data source.

Wednesday, April 22, 2015

Listing with Insert and Update MVC Part 1

Listing of records is binding the model with custom list of objects and show on the view page. For this process we will create custom class that will be used to create objects list and then will bind that list with model.
We have learnt how to use default process and create listing with CRUD actions. In this article we will create with our own code blocks. Create a class CategoryD having all the fields saved in the database table Category as:
public class CategoryD
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
}
Create a controller CategoriesController with default Index action. This action is empty and write following code fragment in this action:
public ActionResult Index()
{
List<CategoryD> categories = new List<CategoryD>();
using (EmpDbEntities db = new EmpDbEntities())
{
categories = db.Categories.Select(a => new CategoryD
{
Id = a.CategoryId,
Title = a.Name,
Description = a.Description,
IsActive = a.IsActive
}).ToList();
}
return View(categories);
}
Create view for this action and paste below code in this view page:
@model CodeWallMVCApplication.ViewModels.CategoryVM
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
int i = 1;
}
<h2>Categories List</h2>
<input type="button" value="Create New" id="btnNew" />
<table>
<thead>
<tr>
<td><b>#</b></td>
<td><b>Title</b></td>
<td><b>Description</b></td>
<td><b>Active</b></td>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@i</td>
<td>@item.Title</td>
<td>@item.Description</td>
<td>@(item.IsActive ? "Active" : "D")</td>
</tr>
i = i + 1;
}
</tbody>
</table>
Run this project and go to this controller’s action, it will show all the records from categories table. This output is as same as the output in earlier article where the view has been created using default MVC feature.
Listing with Insert and Update MVC Part 1

This is only listing part, we have to insert and update through this same page.  To perform this, just change the category class a little bit and add one more class. We will insert and update category entity in our next article.

Monday, April 20, 2015

Working with Database First: edmx in MVC

Database First approach creates all the model classes, properties and its context from the selecting database and then let the user access those classes for performing CRUD actions. User first have to create database and then follow this article to create an edmx file.
An edmx file is an XML file defines a model used to perform CRUD operations on database. This file can also be used with Entity Framework and contains some information to be used by EF designer. User can change this file to change EF designer and sometimes have to change in edmx file manually.
In previous article we have created a database EmpDb with single table, now we will create an edmx file for this database. By using the same MVC project (used in earlier articles) follow below steps:


  1. Right click on Models folder and Add New Item and then select ADO.NET Entity Data Model, name it whatever you want and click Add.
    Add New Item
  2. The next window is for choosing model contents whether from an existing database or an empty model. Because we have our existing database so select Generate from database and click on next.
    Generate from database

  3. You need to connect with your data connection, click on new connection and select Microsoft SQL Server from data source and then continue. After clicking on next button, it will ask for connection properties.
    new connection and select Microsoft SQL Server

  4. Enter server name or it may be selected by-default if you have any. It will load all the databases by clicking on drop-down for enter database name, select your database and click on OK. You can even test this connection whether it have problem or succeed.
Enter server name or it may be selected
  1. Now the third steps will open again with filled information like whole connection string and name in web.config to be saved. This entities name is changeable, leave it as is and next.
    name in web.config

  2. Next window will ask for entity framework version your project is using. It will detect all you entity framework versions and checked according to compatibility. Look out below image, it will disable V-6 and auto select V-5, so just next this step.
     entity framework version

  3. Choose database objects like tables, stored procedures etc. to be include in edmx file and click on next.
    Choose database objects like tables, stored procedures

  4. Your edmx file have been created as shown below.
 edmx file output

This will have all the tables in classes forms to be easily access by programmer and a constructor with the name of entities saved in web.config file. Now we can easily create our controllers and views as per the tables, we will continue in next article.
© Copyright 2013 Computer Programming | All Right Reserved