Wednesday, June 23, 2010

entered value from child window to parent window

From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.document object model. So if the name of the form in parent window is f1 and the text box where we want the value to be passed is p_name ( in parent window ) then from the child window we can access them by using the object. opener.document.f1.p_name The value for this object can be assigned like this Related Tutorial• Refreshing the parent window• Opening Windowopener.document.f1.p_name.value="Any value";Related Tutorial• Refreshing the parent window• Opening Window We will try to make it interactive so we will assign this to a value entered by the user. Then we will use one input box in child window and name it as c_name. So we can pass the value of the input box of child window to the parent window input box by this line. opener.document.f1.p_name.value = document.frm.c_name.value; We will keep this line inside a function and call this function on click of a button. Inside the function after executing the above line we will add the code to close the child window. Like this .. opener.document.f1.p_name.value = document.frm.c_name.value;self.close();Here is the demo, Click the button ( take care of popup blocker of your browser ) and enter your name in child window opened. Then once closed it will be available at parent window. Your Name Click here to open the child window To open the child window this is the code used in parent window

<form method=post action='' name=f1>

<table border=0 cellpadding=0 cellspacing=0 width=550> <tr>

<td ><font size=2 face='Verdana'>Your Name</font><input type=text name='p_name' size='8'> 

<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " 

onClick=window.open("child3.html","Ratting",

"width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window</a>

</td></tr> </table></form>

Inside the Child window code is here

<html>

<head>

<script langauge="javascript">

function post_value(){

opener.document.f1.p_name.value = document.frm.c_name.value;

self.close();

}

</script>

<title>(Type a title for your page here)</title>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"> <form name="frm" method=post action=''>

<table border=0 cellpadding=0 cellspacing=0 width=250>

<tr><td align="center"> Your name<input type="text" name="c_name" size=12 value=test> <input type=button value='Submit' onclick="post_value();">

</td></tr>


</table></form>

How-to use ClientIDs in JavaScript

with their parent’s ID.


<div id="parent" runat="server">
<asp:TextBox ID="txtUsername" Runat="server" />
</div>

This isn’t really much of a problem when all you are working with is server-side code, but when you start tinkering with JavaScript, things become quite annoying and get an overall feeling of hackyness.

One solution, which I have used time-and-again, is to use a script block at the end of your page where you create a series of variables that contain the actual IDs. You then use these variables to reach the actual elements via JavaScript.

<script type="text/javascript">
var txtUsernameID = '<%= txtUsername.ClientID %>';
var txtPasswordID = '<%= txtPassword.ClientID %>';
</script>

This solution works fine, but it isn’t exactly pretty and it doesn’t weigh well on my conscience.

Thursday, March 11, 2010

Function to return a table from a delimitted (csv) string

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_ParseCSVString]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_ParseCSVString]
GO
create function fn_ParseCSVString
(
@CSVString varchar(8000) ,
@Delimiter varchar(10)
)
returns @tbl table (s varchar(1000))
as
/* select * from dbo.fn_ParseCSVString ('qwe,c,rew,c,wer', ',c,') */
begin
declare @i int ,
@j int
select @i = 1
while @i <= len(@CSVString)
begin
select @j = charindex(@Delimiter, @CSVString, @i)
if @j = 0
begin
select @j = len(@CSVString) + 1
end
insert @tbl select substring(@CSVString, @i, @j - @i)
select @i = @j + len(@Delimiter)
end
return
end
GO

Function to return all non-alphameric characters from a string in a table.

create function GetCtrl
(@str varchar(1000))
returns @t table (offset int, chr int)
as
begin
declare @i int, @j int
select @i = 1, @j = 1
while @i <= len(@str)
begin
select @j = @i
select @i = patindex('%[^a-zA-Z0-9 ]%', substring(@str,@i,len(@str)-@i+1))
if @i = 0
set @i = len(@str) + 10
else
begin
select @i = @i + @j -1
insert @t select @i, ascii(substring(@str,@i,1))
end
select @i = @i + 1
end
return
end
select * from dbo.GetCtrl ('sad%sa*s(sdfg*')
i chr
----------- -----------
4 37
7 42
9 40
14 42

Returns the number of rows in each table in a database.

if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_GetRowsForAllTables]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetRowsForAllTables]

GO

Create Procedure sp_GetRowsForAllTables
@DBName varchar(128) = null
as
set nocount on
if @DBName is null
set @DBName = db_name()
create table #a (TableName varchar(128), norows int null, id int identity(1,1))
declare @id int ,
@maxID int ,
@TableName varchar(128) ,
@FKName varchar(128) ,
@cmd nvarchar(1000) ,
@rc int,
@spcmd varchar(1000)
set @cmd = 'exec ' + @DBName + '..sp_executesql N''insert #a (TableName) select TABLE_NAME from information_schema.tables where TABLE_TYPE = ''''BASE TABLE'''' '' '

exec (@cmd)

select @id = 0 , @maxID = max(id) from #a
while @id < @maxID begin select @id = min(id) from #a where id > @id

select @TableName = TableName from #a where id = @id

set @cmd = 'exec ' + @DBName + '..sp_executesql N''update #a

set norows = (select rows from sysindexes where indid in (0,1) and id = object_id(''''' + @TableName + '''''))'

set @cmd = @cmd + ' where #a.id = ' + convert(varchar(10),@id) + '''' exec (@cmd)

if @rc <> 0 or @@error <> 0
begin
raiserror('failed %s',16,-1,@TableName)
return
end
end
select * from #a
drop table #ago

Wednesday, September 24, 2008

RELATIONSHIP BETWEEN OBJECTS


Association

In an object-oriented solution, we find an organisation of objects, each object playing a
set of roles and each role handling a set of responsibilities.
Playing a role, a person has some contract with the external world to provide a set of
services. A taxicab driver plays his role and is responsible to ply passenger for a fair
fixed by the authorities. If a person approaches a taxi in a stand or calls one by radio, the
taxi cab driver or taxi company is obliged to transport the person from the origin to a
desired destination. To request for the services of a taxicab, we have to associate with the
taxi driver by making eye contact or calling him over a phone. We cannot request a
service from an entity before establishing a contact, i.e., before forming an association .
In the society, an entity implicitly or explicitly announces the services offered by it. An
object, which is a model of a real world entity, exposes its interfaces announcing to other
objects the services it offers. Viewing differently, the interfaces announce the
responsibilities of an object. Another object in the application that can establish an
association with this object can send a message (aka request) requesting the object for
some services.
Thus, association between two objects is necessary before they can communicate with
each other. When an object sends a message to a second and for its own behavior
depends on the resulting state of the second object that has acted on the request or the on
the value returned, there is a dependency by the first object on the second.

Whole-Part Relationship – A Special Type of Association
In an association, an object seeks services of another object to achieve a result that it
could not have achieved on its own. But, the modeling of the object itself is complete.
When an object’s own definition does not structurally depend on the associated object,
but has a reference to the other object, we call the relationship as association.
There are several cases where we build an object by assembling other objects. We form a
whole by using different parts, though the parts themselves may be useful as independent
objects. A taxicab has parts like steering system, braking system, engine, body, wheels
etc. To behave like a taxicab, it needs all the parts. For the cab to move faster, the engine
has to accelerate. A passenger asks the driver to move faster or slower; the taxi sends a
message through the accelerator pedal to the engine to rotate faster or slower. We don’t
say that the engine is rotating faster, but that the car is speeding. Seen from outside, we
see the whole object, and the parts are hidden. The structure and behavior of the whole
depends on the parts. When a behavior is requested, the whole delegates the
responsibility to appropriate parts.
The whole is aware of its parts as it requests for different services from the parts. A part
need not be aware of the whole. It would respond to the requests from the whole. Being
part of the whole, and not being visible to other objects, it receives requests only from the
whole whose part it is.
Whole-part relationships are classified as ‘aggregation’ and ‘composition ’ based on how
closely the parts relate to the whole.

Aggregation
When the parts are not created for the sake of whole and can be interchanged during the
life of the whole, it is aggregation. The parts could be manufactured separately and
assembled to create the whole. Parts of an automobile are manufactured separately and
are changed during the life of the vehicle. Also, a part may be used with another whole –
Explaining Object Oriented Analysis Concepts to Managers of an Organization
19
a wheel that is used in a car may be used in a bullock cart too( Seven years ago, I went to
buy a tyre for my Maruti car. The sh op keeper asked if I needed the tyre for a bullock
cart. He said that no tax would be levied on this purchase, if it was for a bullock cart).
In some cases, a part may be shared by several whole objects simultaneously. If we
consider a sports team with several players, these players may be members of several
teams at a time. Several computers may share one monitor. It is possible to use a TV set
as a TV receiver and an AV player for DVD.
As the parts are not created for the sake of whole and also may be shared, the whole does
not have the responsibility of creating the parts and does not have right to delete the parts.
The whole does not own its parts.

Composition
Composition is a special kind of aggregation.
The tyre of a car is manufactured using steel re-enforcer and rubber. We do not
recompose the parts during the use of the tyre nor do we separate the parts after it is
manufactured. When we compose a word, we use several characters and when the word
is deleted all the characters in the word are also deleted. In composition, the parts are
created for the whole and are deleted with the whole.
In a composition relationship, the whole owns its parts; it has the responsibility to create
and delete the parts. Hence, the parts cannot be shared with other whole objects.
Model of a whole-part relationship (aggregation Vs. composition) depends on the domain
and context. E.g., for an end user, a laptop computer would be composed of its parts and
a desktop computer aggregation of its parts. For a manufacturer of the PCs, laptop and
desktop computer both would be aggregation.

Inheritance
Often we use words in a way that the meaning of one concept is part of another. If we say
that a person travels by an automobile, the meaning does not change if he travels by a car,
bus, or any other automobile vehicle. In our thought process, we use a grouping
mechanism to understand and use concepts. This is a way of knowing something new in
terms of something we know. What we already know has the essential features to
represent the concept at that level. Without this process we could not build knowledge.
When we “buy vegetables, fruits, groceries, and clothes” we have generalized some
concepts.
When we say that a car is an automobile, we understand that all the structural and
behavioral properties of the concept of automobile are applicable to a car. We do not
have to define and understand these concepts again. This simplifies our understanding
and communication. The car can have some additional property, uncommon to other
automobiles. It is easier to say that a car is a special kind of automobile having all the
properties of a common automobile and some stated additional features. This extends the
concept of an automobile. When we define a bus, again we can use the already defined
model of automobile and add features specific to a bus.
When we define a new concept in terms of already understood concepts, we inherit the
features of the previously known concept and specialize it by adding some new features.
As we have inherited some features of the existing model, should there be some changes
to the original concepts, it would affect all the specialized concept too. E.g., if the
concept of automobile assumed it to run on IC engines, all the inherited vehicles would
Dinesha and Bhat
20
also have IC engines – not battery powered motors to move them. If there be a change in
the concept of automobiles to change from IC engine to electrical motors, it would affect
all the inherited concepts.
With inheritance we can define a new concept in terms of already known concepts, which
helps build knowledge incrementally, reusing existing knowledge/model.
Contrast this with association where we use another object by knowing its responsibilities
and interfaces.

About Me

Bangalore, Karnataka, India