Thursday, July 31, 2008

ASP.NET Custom GridView with row based contextmenu, single click, double click enabled

Ever wanted to play with such a miraculous gridview that provides you with your own context menu listing various items? Additionally, ever needed single click or double click event for each row of a gridview? It is no more dream only. It is not only possible but also present at your service. Yeah, a custom gridview that can do a number of tasks for you !

1.Single Row Click
2.Double Row Click
3.ContextMenu row-based
4.Ascending/Descending sorting image in the Header elements
5.Fix GridView height when the number of rows <>
6.Built-in Filter Textbox7.Built-in Checkbox per row and per whole GridView

Thanks to the hard-working team from aspalliance.com. They have provided such a rich gridview.

You have better go to the page and explore all by youself. You can find it here:
http://aspalliance.com/articleViewer.aspx?aId=946&pId=-1

Wednesday, July 30, 2008

How to Create DataTable Programmatically in C#, ASP.NET ?

Most of the times programmers fill the DataTable from database. This action fills the schema of the database table into the DataTable. We can bind data controls like GridView, DropdownList to such DataTable. But somtimes what happens is one needs to create a DataTable programmatically. Here I have put the simple method of creating DataTable programmatically in C#.

Update: Responding to comment from one of the readers, I have now posted how to create DataTable in c# with clickable column. As in this post, the DataTable has been bound to an asp.net GridView.

Create a DataTable instance

DataTable table = new DataTable();

Create 7 columns for this DataTable
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Checked");
DataColumn col4 = new DataColumn("Description");
DataColumn col5 = new DataColumn("Price");
DataColumn col6 = new DataColumn("Brand");
DataColumn col7 = new DataColumn("Remarks");

Define DataType of the Columns
col1.DataType = System.Type.GetType("System.Int");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.Boolean");
col4.DataType = System.Type.GetType("System.String");
col5.DataType = System.Type.GetType("System.Double");
col6.DataType = System.Type.GetType("System.String");
col7.DataType = System.Type.GetType("System.String");

Add All These Columns into DataTable table
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
table.Columns.Add(col5);
table.Columns.Add(col6);
table.Columns.Add(col7);

Create a Row in the DataTable table
DataRow row = table.NewRow();
Fill All Columns with Data
row[col1] = 1100;
row[col2] = "Computer Set";
row[col3] = true;
row[col4] = "New computer set";
row[col5] = 32000.00
row[col6] = "NEW BRAND-1100";
row[col7] = "Purchased on July 30,2008";

Add the Row into DataTable
table.Rows.Add(row);
Want to bind this DataTable to a GridView?
GridView gvTest=new GridView();
gvTest.DataSource = table;
gvTest.DataBind();

You are done! Happy dot netting!!
Shout it

Tuesday, July 29, 2008

Oracle Error ORA-01722: Invalid Number

ORA-01722: Invalid Number mostly occurs in runtime. There are some errors in programming that may generate ORA-01722: Invalid Number error. Let me present this formally.

Cause:
The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

Action:
Check the character strings in the function or expression. Check that they contain only numbers, a sign, a decimal point, and the character "E" or "e" and retry the operation

Experience:
I got the error in runtime when I assigned numeric value to a varchar2 parameter passed to a package. There may other so many possible reasons as discussed above. 

At the end, you may also like to read on implementing object relational database features in oracle like nested table, inheritance, collection array, member function etc.

Happy Programming!

Sunday, July 27, 2008

Display Matched Item Value from ASP.NET Dropdown List

How do you display the dropdownlist item of an asp.net dropdown list according to some value you have as the match string? Let me demonstrate this via an example.

Create ListItems in an ASP.NET DropdownList

(I have used the dropdownlist name ddlTest for this example). Items are as follows:

asp.net

c#
vb.net


Traditional Loop Method

string value="asp.net";
for(int i=0; i
{
if(ddlTest.Items[i].Value==value)
{
ddlTest.Items[i].Selected=true;
}
}

You see it is inefficient. First it takes more lines of code. Second the loop increases as the item in the asp.net dropdownlist increases. The more efficient built in way is demonstrated below.

Display the Item Matched with Dynamic Input String

string value="asp.net";
ddlTest.electedValue = ddlTest.Items.FindByValue(value).Value;

The dropdownlist now displays the item having value equal to "asp.net". I have only put an example. The high dynamicity of this technique is seen when you supply some dynamic value to the varibale value used in the example above.


Friday, July 25, 2008

Oracle Error ORA-12704: character set mismatch

One gets much worried by ORA-12704:character set mismatch. It is hard to track what is going on. But the guideline below will give relief.

Causes of Oracle Error ORA-12704: character set mismatch

-The string operands(other than an nlsparams argument) to an operator or built-in function do not have the same character set.

- An nlsparams operand is not in the database character set.

- String data with character set other than the database character set is passed to a built-in function not expecting it.

- The second argument to CHR() or CSCONVERT() is not CHAR_CS or NCHAR_CS.

- A string expression in the VALUES clause of an INSERT statement, or the SET clause of an UPDATE statement, does not have the same character set as the column into which the value would be inserted.

- A value provided in a DEFAULT clause when creating a table does not have the same character set as declared for the column.

- An argument to a PL/SQL function does not conform to the character set requirements of the corresponding parameter.

Action to be taken


Check one of the suspected region in your coding in PL/SQL and find the error occuring part. Then you can fix it and get rid of it! Happy error detection!!

Finally I would like to welcome you to get insight on implementing object relational features in oracle illustrated with a simple ecommerce scenario.

Happy Programming!

Thursday, July 24, 2008

How to Display ASP.NET GridView Header and Footer When No Data is Present?

The default behaviour of asp.net gridview is that it hides both Gridview header and footer when no data is present. You can note the absence of data (that is not a single row is available) by
putting text in EmptyDataTemplate of asp.net gridview.

---- other templates-----No data is present.<\EmptyDataTemplate>
<\asp:GridView>
But still you might want to show header and footer. What can we do? There exists a cheat! Just add an empty row to the datasource and bind your gridview to this datasource. Here goes the code.

DataTable table=new DataTable();
table=GetAllData(); //a function that fills the datatable
if(table.Rows.Count==0)
{
DataRow row = table.NewRow();
row[0] = "none";
table.Rows.Add(row);
gvTest.DataSource = table;
gvTest.DataBind();
}

Now it is nearly done! But one step is remaining. Now the first column of the first row will display "none". We do not want this. So we must hide it. How? Just create the RowDataBound event handler for your gridview.

---- other templates-----
No data is present.
<\EmptyDataTemplate>
<\asp:GridView>
Here goes the code for the rowdatabound event handler:

//invokes when a row is bound to data
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[0].Text=="none")
{
e.Row.Visible=false;
}
}
}

Note: You must explicitly enable footer to be shown. By default it is not shown. This way:
---- other templates-----
No data is present.
<\EmptyDataTemplate>
<\asp:GridView>
Now you are done. Happy programming!!

kick it on DotNetKicks.com

Wednesday, July 23, 2008

Oracle Error: PLS-00323

I encountered Oracle Error: PLS-00323 this morning. It took me time to resolve and fix it. In much cases, PLS-00323 get around you and you have to wander from here to there in Internet. Before going to Internet, you might have been tired of doing all your brains! This is all because of the oracle package that consists of a package specification and its corresponding body definition. Say, you have a package with three variables defined in your sub program and only two defined in your package body. Exactly here you get the error PLS-00323.

PLS-00323: subprogram or cursor 'BANK_GETBYPRIMARYKEY' is declared in a package specification and must be defined in the package body.

Here 'BANK_GETBYPRIMARYKEY' is my sub program.

Fixes: To be sure you can just check the matches in package specification and package body. Happy database programming.

Tuesday, July 22, 2008

How to encrypt decrypt password in Oracle data table?

Programmers sometimes need to store user password and some other fields in database data table in encrypted format. There exists an easy way to do this. I have used in my database. In my system, I have a table called Users which contains fields: UserID, UserName, Password,and Email. I have encrypted the password field.

[While continuing the journey, you may also be interested in implementing object relational database features of Oracle illustrated for a simple ecommerce database.]

With this scenario, I get following issues to be solved.Encrypt input password before inserting into data table.Decrypt password before reading it.

For all these requirements I use two functions, one for encrypting password and another for decrypting password.

--create two function: one for encrypt--the other for decrypt:

FUNCTION encrypt( iValue in varchar2, iKey in varchar2 )
return varchar2
is
vEncrypted varchar2(4000);
begin
-- encrypt input string
dbms_obfuscation_toolkit.desencrypt (input_string => iValue,
key_string => iKey,
encrypted_string => vEncrypted);
return vEncrypted;
end;/

FUNCTION decrypt( iValue in varchar2, iKey in varchar2 )

return varchar2
is
vDecrypted varchar2(4000);
begin
-- decrypt input string
dbms_obfuscation_toolkit.desdecrypt (input_string => iValue,
key_string => iKey,
decrypted_string => vDecrypted);
return vDecrypted;
end/

Monday, July 21, 2008

Master Pages for consistent design of ASP.NET pages

Master pages are the best way if you want consistent look of you website over all pages. Think of all those web sites you surf all day and see the constant part of the pages: header, footer and site maps, and so on. This can be brought very easily and effectively with the help of master pages. A very good series of introductory master page tutorial can be found here.

http://www.asp.net/learn/master-pages/tutorial-01-cs.aspx

This is the first tutorial on the series. The later ones will teach many tips and tricks. Don't take it simply. It can do wonder to your sites. You will find there code package too. Enjoy!

Sunday, July 20, 2008

Web site/application design made easy for programmers

Programmers find it boring job to get with website designs. It would be a great aid if he/she could concentrate on programming logic. I have found so many programmers who think design part should not be a good programmer's job.

See here the web site design part made easy- if not easiest. I found this website to be very useful for helping design get better. There are thousands of free design templates. Choose any of them you find suitable for your website/web application and bring the cascade style sheet and templates into your design. You will get higher speed of development. Enjoy!

http://www.opendesigns.org/

Visit the site, get around it and praise the great open source community!

Popular Posts

Recent Articles