March 2006 - Posts

Manually configure GridView to display and delete row

I will like to share a tip here on what I have discovered today. Things are getting real fun :)

When you drag a GridView from your toolbox to your designer and you plan not to rely on the Smart Link (is the arrow called Smart Link?) to select the data source, below are the steps you can consider.

Okay, I assume when you select your data source (including selecting your sql statement) you are allow to set enable delete and insert feature to your grid view. And that is not what you want.

Steps by Steps:

1. Create a stored procedure to return 3 columns.

SELECT NameID, Name, Description

FROM Table1

2. Right now, you have to add 3 databounds controls in your gridview. Use the smart link to add columns or edit columns.

*You don't have to add a datasource first before adding these columns

a) NameID BoundField

b) Name BoundField

c) Description BoundField

3. Now you have decided to have a delete button. You can do that as well in the edit columns or add columns.

d) Delete ButtonField

4. But again, you feel that you don't want to show NameID column because it is complicated to the users.

Try not to ignore step 5 and 6.

5. I think you need to remain the NameID column. All you can do is set that column visible = false in the Edit Columns. It won't display.

6. This is a very crucial step. If you don't have this you will have an error during runtime (compilation is okay).

Click on the GrdiView's properties and set DataKeyNames = NameID

7. Time to delete some records in gridview during runtime. You have to add an RowDeleting event.

protected void gvNames_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

int rowIndex = e.RowIndex;

// The DataNameKey is crucial here. If not you will receive an error pointing to e.RowIndex

int nameID = (int)gvIndividualSearch.DataKeys[e.RowIndex].Value;

// Your business process here

}

8. Run your web application. I believe it will work on your side too :)

Cheers.

Posted by chuawenching with no comments

String.Format - mm and MM are not the same

Don't make the same foolish mistakes I have done for formatting date based on day/month/year. By default, it is based on month/day/year.

Do not confuse between mm and MM.

If not mistaken,

mm = minutes

MM = months

Sample Code:

String.Format("{0:dd/MM/yyyy}", calendarControl);

Cheers.

Posted by chuawenching with no comments

ASP.NET 2.0 - Custom Paging for GridView VS GridView Built-In Paging? Which one?

Wonder which one is faster. In the past, [correct me if I am wrong] developers chose to use custom paging instead of relying on datagrid paging.

Extra Information: You can use IDENTITY to do this custom paging.

How about now? I had seen articles people still recommeding custom paging using ROW_Function() which was part of SQL Server 2005 instead of relying on GridView paging.

Extra Information: You can do this:

SELECT ROW_Function() OVER (ORDER BY ProductID) AS Row, SomeProduct

FROM Product

WHERE ProductID = '1'

But most articles demonstrated on how to use GridView Paging. Maybe due to the simplicity of having a GridView pointing to a datasource and enabling sorting + paging without writing codes.

I can't visualize myself except I have multi millions of records in my database to test :P

Posted by chuawenching with 3 comment(s)

ASP.NET 2.0 Profile - Must have FULL type property in web.config

Just want to share something. Noobie tips.

This is the right way of having a profile property. Must have the full type :)

<add name="someobject" type="System.Data.DataTable" />

Do not do this as below

<add name="someobject" type="DataTable" />

You will get a runtime error when you try to read from this property. I guess maybe the System.Data namespace is not generated automatically like

using System.Data;

....

upon this Profile property. Correct me if I am wrong. Just my another noobie assumption.

Cheers.

Posted by chuawenching with no comments

ASP.NET 2.0 Multiview Control makes VS Designer harder

Frustrated :( I like VS 2005, but I really don't like ASP.NET 2.0 Multiview Control. It was suppose to save my day with tab pages on web applications. But I had a few problems and it was slowing down my development work.

Before I share with the problems I have. This was what I had done.

I had a mutltview, 3 views and 1 menu controls. I wanted to achieve something like window forms tab pages. It could be done. I had also written a bunch of CSS and applied these styles to my multiview.

Currently, I am using P4 1.9 GHz (not the fastest in the market) and 1GB of SD RAM (think so).

Ok I am done. Back to my topic, you will face a lot of issues in VS 2005 designer when you want to have mutliple asp.net 2.0 standard controls on top of your multiview control.

The simplest example, "most of the time" I was not able to change my controls ID like label, textbox or calendar. I had to change inside my html code. You can also see the multiview control refreshes a lot of times in VS 2005 designer. Sometimes I tried to change properties for button 1, but it ended up applied in button 2 or 3. Weird huh?

Wonder other people faces this problem too? Or there is something wrong with my code or machine. [Shy :( ]

Posted by chuawenching with no comments

"I am a bad developer" - Frustrated with DataTable, use DataSet to get values for each row

I couldn't believe it. I tried my very best to avoid DataSet in ASP.NET 2.0 but I just couldn't. I will store my values within a DataTable, but later I need to add that DataTable into my DataSet. Not only that, it took me so long to solve this problem. Lucky my colleagues hinted me. Quite embarassing. Plus I never think out of the box. (Hint: my gf can solve a problem in 2 minutes, and I take ages)

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = HttpContext.Current.User.Identity.Name;

DataTable dt = new DataTable();

// Don't forget to name your DataTable :) It is crucial. You will get some errors stating  that

// DataTable name not set.

dt.TableName = "Test";

dt.Columns.Add("Company / Organisation", typeof(string));

dt.Columns.Add("Position Held", typeof(string));

dt.Columns.Add("Representative", typeof(string));

dt.Columns.Add("Date of Appointment", typeof(string));

// 3 rows of dummy values

dt.Rows.Add((new object[] {

"1", "2", "3", "4"}

)

);

dt.Rows.Add((new object[] {

"5", "6", "7", "8"}

)

);

dt.Rows.Add((new object[] {

"9", "10", "11", "12"}

)

);

// Dummy strings

string strValue1 = string.Empty;

string strValue2 = string.Empty;

string strValue3 = string.Empty;

string strValue4 = string.Empty;

DataSet d1 = new DataSet();

d1.Tables.Add(dt);

// I think you can't use DataTable here, if not you will an error stating that DataTable has no GetEnumerator... correct me if I am wrong.

foreach (DataRow dr in d1.Tables["Test"].Rows)

{

strValue1 = dr["Company / Organisation"].ToString();

strValue2 = dr["Position Held"].ToString();

strValue3 = dr["Representative"].ToString();

strValue4 = dr["Date of Appointment"].ToString();

}

}

Yeap. You will get the results you want :) Back to work.

 

Posted by chuawenching with 5 comment(s)

SQL-CLR - Lesson 1 Playing with SqlMetaData and return OUTPUT values

I am looking through various examples on the internet, sql server 2005 examples and MSDN. I will be sharing on what I have tested on. It is quite fun :) to play with sql clr.

When you build this code below and deploy over sql server 2005, you will find the stored procedure HelloWorld inside Adventureworks\Programmability\Stored Procedures\dbo.HelloWorld.

*I didn't name it usp_HelloWorld since I was just testing it.

Suprisingly, you can't modify this stored procedure inside the SQL Server Management Studio unlike the normal T-Sql Stored Procedures.

In order to execute this, you need to enable CLR Integration inside your sql server 2005. Be default, it is switched off.

How-to enable CLR integration:

- User Interface

http://daron.yondem.com/CategoryView,category,SQL%202005.aspx

- Command Line

http://msdn2.microsoft.com/en-us/library/ms254506(VS.80).aspx

For my 1st lesson, I will be playing with SqlMetaData to create 4 columns and insert values into these columns.

*You can learn this inside SQL Server 2005 examples. I just edit it for my learning purpose.

Check the code samples below:

Hello.cs

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

namespace Microsoft.Samples.Learn

{

public sealed partial class Hello

{

[Microsoft.SqlServer.Server.SqlProcedure]

public static void HelloWorld(out string outGreeting)

{

// Create 4 columns with specific data types

SqlMetaData nameInfo = new SqlMetaData("Name", SqlDbType.NVarChar, 30);

SqlMetaData addressInfo = new SqlMetaData("Home Address", SqlDbType.NVarChar, 50);

SqlMetaData ageInfo = new SqlMetaData("Age", SqlDbType.Int);

SqlMetaData raceInfo = new SqlMetaData("Race", SqlDbType.NVarChar, 20);

SqlDataRecord newRecord =

new SqlDataRecord(new SqlMetaData[]

{

nameInfo,

addressInfo,

ageInfo,

raceInfo

}

);

// Add 1st record

newRecord.SetString(0, "Chua Wen Ching");

newRecord.SetString(1, "3-5, Bandar Kinrara, Puching");

newRecord.SetValue(2, 21);

newRecord.SetString(3, "Chinese");

// Sends a single row result-set to the client

SqlContext.Pipe.Send(newRecord);

outGreeting = "This is a boring output?";

}

}

}

Test.sql

USE [AdventureWorks];

DECLARE @OutputValue NVARCHAR(30);

EXEC HelloWorld @OutputValue OUTPUT;

SELECT @OutputValue as [The Return Value];

Output in VS 2005

Executing selected script from project script file

Name Home Address Age Race (CS 2.0 removes whitespaces, but it is representing 4 columns) 

------------------------------ -------------------------------------------------- ----------- --------------------

Chua Wen Ching 3-5, Bandar Kinrara, Puching 21 Chinese

No rows affected.

(1 row(s) returned)

The Return Value

------------------------------

This is a boring output?

No rows affected.

(1 row(s) returned)

Finished executing selected script from project script file

Posted by chuawenching with no comments

Localhost - Bypass proxy server

Sigh. I forgot that my IE was configured with a proxy server. I kept receiving an error in my IE whenever I tried to access http://localhost. I kept wonder why.

// Error //

ERROR
The requested URL could not be retrieved

--------------------------------------------------------------------------------

While trying to retrieve the URL: http://localhost/

The following error was encountered:

Connection Failed
The system returned:

    (111) Connection refusedThe remote host or network may be down. Please try the request again.

Your cache administrator is webmaster.

--------------------------------------------------------------------------------

Generated Thu, 23 Mar 2006 09:05:38 GMT by proxy.ABC.com.my (squid/2.5.STABLE4)

Why? Why?

Finally I approached my team lead for help. He had called upon my other senior colleague. Actually inside the IE you have to configure like this 

Tools -> Internet Options -> Connections -> Lan Settings -> Enabled Bypass proxy server for local address.

Then this will solve the issue. :)

I am really a noobie to web.

Posted by chuawenching with 6 comment(s)