Category Archives: Uncategorized

Nilesh Gule’s Technical Blog: Entity Framework : Part 6 – Perform CRUD operation…

Nilesh Gule’s Technical Blog: Entity Framework : Part 6 – Perform CRUD operation…: “Background This is the sixth part of the Entity Framework 4 learning series . Today I am going to demonstrate how to perform create new e…”

Data Filtering to DetailsView Via GridView

Introduction

 

We going to need a data table in order to work with this example. in here I’m using StudentResult as my data Table. you can download example files using the  Above link ..

 

Download

 

1

 

Step :1

 

First of all Fire-up Visual Studio and create a new Website select the Language as Visual C#. then hit ok button.. then add a data base to your project. and insert any data you want to display. in my case I’m using SqlExpress Database to store all my data. because its easy to use any I can interact with my database easily via visual studio.

 

 

Then add a GridView and a DetailsView  to the designing area. you will see something like this..

 

2

 

I have a Grid View at the top

and a Details view at the bottom

 

 

 

 

 

 

 

 

 

 

 

 

now we need to bind data in to GridView. to do that you can use smart tag as shown below..

 

3 

 

then configure your data grid using it or you can drag a SqlDatasource  and configure it and Bind it to GridView.  by selecting your data source..

 

4

 

Now elect New Data source and select database from pop-up window

 

 Selecting data Source

 

in next window hit New connection Button  and Select your Sql Server Or Sql Data Base file depend on your Requirement.. in this scenario I’m going to use a  SqlExpress database File(Database.mdf) that contain all my data.

 

So in Add New Connection Window As Data Source I’m Going to Select  Microsoft SQL Server Database File (SqlClient)  and then locate my database. note if you create your database via Visual Studio you must navigate to project’s App_Data folder and select your database. after that hit Test connection button and you must get a message saying Connection Successful ..

 

then you will notice that your database connection string has been created .

and your Connection String should be look like this

 

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True

 

6

 

then hit Next button and IDE will ask you to save then ConnectionString.

this string will be stored in your web.config file so that later whenever you want you can retrieve it in your program as a String.

 

Then in very next window Select the table you want to  extract data and then  Select the columns you want to display. in this scenario I’m using all the columns as I need to display detailed information about the Student. you can define custom SQL Query or Procedure according to your need

 

7

 

 Ok now all set. if you want to test your Query you can hit next button and test it. or else you can hit finish button. now you will see that

 

image

 

Now You will notice that Your GridView Has been Updated. in smart drop down Select Enable Selection.

 

and then Select edit columns.

 

8

 

you will notice that I have removed most of the Fields. I only keep SID Field and Name Field. and there is a especial Field called Select.  the Select field is the link that we are going to use to Filter Data. if you cant see Select field in your Project. you must Click the Smart Tag in GridView and then Select Enable Selection.

 

now your GridView Should be look like this.

9

 

We have 2 fields SID and Name, And a Special Field Called Select.

 

now we are done with the GridView. the next step is to configure the DetailsView.

 

simply click the smart tag that appear on the top right corner of the DetailsView and select sqlDatasource1 as data source. you will notice that for both DetailsView and GridView is Using the same data source.

 

image

 

Up to this point we have Configured DetailsView and GridView with SqlDatasource1

now we are going to enable DetailView’s  selected page index via GridView’s selected index. that means when User Click on GridView’s item DetailsView will Show all the Relevant information about the Selected Field.

to enable that simply double click on Select field on GridView or Go to the GridView’s Property –>then In Event Category Select  SelectedIndexChanged Property Double click it and Enter Following Code segment

 

Code Snippet
  1. protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         DetailsView1.PageIndex = GridView1.SelectedIndex;
  4.     }

 

 

Now you are all Set and Good to Go. just hit the Debug button and you will see browser get start and your page lodes with something like this.

 

10

 

Whenever you click the Select Field in GridView, Information in Details view Changed.

 

if you want to filter data from DetailsView you use this code Segment

Code Snippet
  1. String myStr = DetailsView1.Rows[1].Cells[1].Text;

 

Same as to Filter Data From GridView You can Use this Code Segment

Code Snippet
  1. String myStr = GridView1.Rows[1].Cells[1].Text;

 

note that   in  GridView1.Rows[1].Cells[1]  Rows and Cells Starting From 0 .

 

 

Simple Introduction to web services in ASP.net C#


1.       What is a web service:
A web service allows a web site(web application) to expose programmatic functionality via the Internet. Web services can accept messages and optionally return replies to those messages.


in our case what we are doing is we create a web service and put it in a local web-server like IIS. Then we create a website that can request something from that web service.lets simply read time or some kind of string from web server.


1.       Getting familiar with web service..
Creating the web service…
simply go to new project –> web –> Asp.net web service application and give the project name as you want.. in my case im using myWebservice as my project name..
1.       In Solution Explorer you will see something like this..

Note that we don’t have something like ASPX..

Thing we have here is asmx that is the extension of service file..
that means Service1.asmx is the name of our service and all the  service based coding   will be written in Service1.asmx.cs
later you will we able to able to create data-driven web-services,  complex requests ,, almost anything that web server support ..but for now we will be going to create a web-service that respond to time request
1.  Now Without doing anything Just run the project..
 then you will get something like this

 

That means in Service1  have a method called HelloWorld(). Which we can access via our program (website) this is a simple method generated by visual studio. you don’t need this method in order to develop your service. if you don’t need to simply you can delete it. but for now im going to keep it.
Note that http://tempuri.org/ is the place we have place our web service.. 
. now if you click the HellowWorld link you will see a thing like this..
 by clicking you can test this method. it will show you what kind a respond you will receive from web-service. in IE you may get a XML respond. if you use Firefox you will show the filtered message.
note that web servers send reply by using XML in order to enable communication between different type of clients. because  ASP , JSP, java ,C# or any language based applications may be requesting resources from web-service. so in order to serve to different type of clients web services use XML based responds.
after you click the invoke button you will see something like this
 2nd one is the url of the service’s reply which we gets after clicking the invoke button.
Code behind about the things we discussed so far..

doble click and open Service1. then you will see the code behind of service1.asmx


1.       to define where the webservice is located on we use this code segment
 [WebService(Namespace = http://tempuri.org/”)]
We write our methods inside public class Service1: System.Web.Services.WebServices class
Which I have marked in Blue Color
        [WebMethod]
        public string HelloWorld()
        {  return “HellowWorld”;
        }

              
       Notice if you want to add a web method you must define your method   after [WebMethod] tag.. else it will not available to client-side ..
Simply write a method after HelloWorld() bt don’t add the tag [WebMethod] before writing it.. then run your program.. check u can see it after method hellow world? No you cant because server think it should only available to service1 not to the clients..
Then add the tag [WebMethod] above your method and run your project again..
Then you will see your method is redy to be available  clients to acsess..
Ok lets get Rollin with that part I will show you how I did it..

look at pictures its very easy to understand..

 

 
HelloWorld()  is .net generated simple method that returns string “HellowWorld”.. 
 
 
 
 
 
 
 
 
 
 
 
and TellMeServerTime( ) is the method that i created. it will respond to the request with a string type  that sends the server’s time..

ok now run your project and then you will see something like this.. if you dont get the newly created method called  TellMeServerTime() rebuild your project and run again..you must get something like this..
now click on TellMeServerTime  then browser will let you to test your method as follows

if you invoke the method several times you will notice the out put is changing each time.. 

1.      Its time to roll with a real world application
First we need to add a web form to our project that is the place where we going to call our web service output
            simply go to add –> new item –> select Web Form and give a name
In my case its MyWebForm.aspx .. 
Then right click on MyWebForm.aspx and select “Set as Start Page”
Now if you run the project you will see nothing bt a empty web page.. Because MyWebForm.aspx is empty
1.        Adding Web Service’s reference to Project…..
 we need to add service1’s reference to our web project in order to use the functionality of the web-service. to do the.. all you need to do is just right click on the project in solution explorer and click add service references.  
then follow the instructions as below picture explains..

Now you must have something like this .. that means we have added service reference to our project..
so that we can create instances/ objects from Service1 and use it…

note that there is a another way you can add this reference.
you can do this by adding web reference instead of adding service reference. both way you will be able to call the function we implemented in Service1.asmx

 
1.      Requesting  server time from  web server via our web form.
To do that im going to need a label and a button..
(Label1 and Button1 in this scenario..)
when you click the button the label will show the time from server..  to do this double click the button and add the following code inside Button1 click event
      Service1 myTimeService = new Service1();
      Label1.Text= myTimeService.TellMeServerTime();
it should look like this..
 

 now u have set everything.. all you need to do is hit the run button..
if you have done everything correctly you may get a out put like this,..

note that each time you click on the button webform is requesting the time from server..
when server respond to that method .net will rip the XML output and get the exact value you need..
simply you don’t need to worry about XML out put you are getting from the web service. Visual Studio will handle the XML part for your convenience

 ok now i think you must have a simple but overall idea about how web services can be applied to our projects.

 Here is another great example for data driven web service application
http://www.developerfusion.com/article/3114/building-xml-web-services-using-c-and-aspnet/2/