Monday, 16 January 2012

ASP.Net C# Passing values from a grid view


Passing values from a grid view (ASP.net C#)


When working in asp.net, I usually use gridViews to show and manipulate data. Well, it’s a good clean and easy way to get ad manipulate data from a db.
But suppose you select a value from a gridView and when you select it say… some sort of a key, it should pass into another page.
In this tutorial we will look at creating a simple data base table, creating a connection, getting data to a grid view and passing it to another page :D
Believe it or not, it’s not as hard as it sounds.

Well, first things first

Create a data base using ms sql server management studio express (wow that’s a long name …)
Go to the MSSMSE, log in to it and by right clicking the Databases folder, create a new database called
myTesting

now you will be able to see the newly created database. 
Select it and then click on the new query so that you can write sql for the database :D

Then you can write the following queries,

/*this is how we comment in sql*/

create table sampleTable(
id  int primary key identity (0, 1),
name varchar(100),
age int,
country varchar(100)
)

the create table  statement will create a table with the name sampleTable with attributes id, name,
age and country. In the table you can see data types as int and varchar.
id is set as the primary key
identity is a special feature in mssql server (as I remember, some of the other databases doesn't support that). It is defined as identity (SEED, INCREMENT). SEED means the starting value and INCREMENT means the value that it is incremented from. With this, we can make the identity auto increment.



run this query and see if the table is working properly, you will see an empty table
select * from sampleTable

Now insert some values in to the table....
note that I'm not inserting any values in to the id column because it will auto increment.

insert into sampleTable(name, age, country) values ('sampleName0', 13, 'sampleCountry0')
insert into sampleTable(name, age, country) values ('sampleName1', 16, 'sampleCountry1')
insert into sampleTable(name, age, country) values ('sampleName2', 17, 'sampleCountry2')
insert into sampleTable(name, age, country) values ('sampleName3', 17, 'sampleCountry3')
insert into sampleTable(name, age, country) values ('sampleName4', 18, 'sampleCountry4')
insert into sampleTable(name, age, country) values ('sampleName5', 19, 'sampleCountry5')
insert into sampleTable(name, age, country) values ('sampleName6', 13, 'sampleCountry6')


Run this query and you will see all the data in the table
select * from sampleTable

Run this and you will see only the name, age and country columns from sampleTable
select name, age, country from sampleTable

Run this and you will see all rows from the table where the age is greater than 17
select * from sampleTable
where age > 17



( if you don’t know how to run a query, all you have to do is highlight the relevant part of the query, and press execute… for example, if you want to run a select query , highlight select * from sampleTable and the press execute)

Now that creation of tables is complete, let us go to visual studio.

Start a new web site in C#, because I am bored, I keeping the name as the default one "WebSite1"

Now make a new page in the web site,
right click on the root that is in shown in the solution explorer, 

select Add New Item 

select Web Form (make sure you select visual c#, in Installed Templates)
Now change the name to TestingForm.aspx
click Add 


Now you will be able to see the page in the solution explorer.


ok, so lets do some cool stuff.

Now add a  gridView controller to the to the page.
(well you can do it in design or source view)

Now it should look like this in the design view.


Now, click on that > like thing in the data gridView and in the choose data source drop select <new data source >

select SQL data base
make the datasoruce name as appropriate.





select new connection 


select Microsoft SQL Server and press continue 





Now in the new window, for the server name TYPE, localhost\SQLEXPRESS ( server and instance name, change as appropriate ).
after entering the server name is entered, select the database. now press Test Connection

If the connection is successful, press OK



now you will be able to see the data connection press NEXT






Keep notice the data connection name and press Next





Leave them as they are and press next







press test query and you will see all the data that was inserted in to the db, press finish




Now run the application by right clicking and selecting view in browser ...







notice all the data is visible in the grid view






now go back to the TestingForm and in the gridView go to edit columns







add a HyperLinkField









Give it a name






Now, lets create a new web form called TestingForm2 , we will pass the selected gridView value from the TestingForm to this TestingForm2.





You can see the new page in the solution explorar








Now add a new Label to the newly created TestingForm2










Now lets get back to the TestingForm.aspx and edit that gridView, go inside it and select the link field and add the new TestingForm2.aspx  the NavigateURL










Select the TestingForm2.aspx so it is added as the navigation url







See  .... you can see the TestingForm2.aspx in the NavigateURL, press OK






Now, you can see the links in blue color that means, they are active so they can navigate to another page.








Right click the TestingForm and select view in browser and check out the page ...











Press the Go link in TestingForm and you will notice that you have navigated to TestingForm2









Now in the TestingForm2 you can only see the label, no values passed yet








Now go back to the TestinForm.aspx and edit the gridView as the following.
We edited the NavigateURL before,
now
set DataNavigateUrlFields to "id"
and set DataNavigateUrlFormatString to TestingForm2.aspx?id={0}
that is
<place to navigate>?<passingvalue>={0}










Now go to the code behind of the TestingForm2








not set the Label1's text to the values that were passed to the query string from the other page.
id is the identifying value we set ( you should see the code view of the gridView to get a better understanding of this).







Now run the page again
and press the Go link
















you can see the value passed to the other page





WOW that was long ... if this works, help ur self to a nice cup of tea :D :D

Hope this helps,
Cheers



3 comments: