can i catch a "personalized" error from sql server in a asp.net app?

Posted by gondar | Filed under , ,

sure, just need to use the RAISERROR in sql server.

in your ASP.Net application, inside a try{...} add your query, and in the catch prepare a label to receive the Error Message, that will be the message from SqlServer.

example:

try
{
DataBase.Execute("declare @temp int" +
"select @temp=0" +
"if @temp=0" +
"RAISERROR('[Error Message]', 16, 1)");
}
catch (Exception ex)
{
lblInfo.Text = ex.Message;
}

after executing this the text of the label will be: [Error Message]

hope it helps.

simplifying gridview events, part 1

Posted by gondar | Filed under , ,

problem: when i'm binding in a gridview, some product name, and theyr prices, i would like to add a column with a textbox and a button/image button to add that product with some quantity to my order, how do i do that?

example:

my solution/example is:

1º - we must add the column, of type templatefield, and then select the GridView > Edit Templates > select the column, and insert in the "item Template" the textbox and the image button, after that select imagebutton and write something in the property "CommandName" like "addbutton"

2º - Add event RowCreated, to the GridView, and insert the following code:

if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton AddP = (ImageButton)e.Row.Cells[10].FindControl("imgBtAddP");
AddP.CommandArgument = e.Row.RowIndex.ToString();
}

this code, is needed to give the correct ID to each row, or else, it would be almost impossible to get the id of the button that was clicked, i mean, the rowid of the button clicked.

3º - now, add the event RowCommand to the GridView, and insert the following code

if (e.CommandName.ToLower().Equals("addbutton"))
{
// get the index of the row
int
index = Convert.ToInt32(e.CommandArgument);

// get the value from the textbox
string quantity = ((TextBox)gView.Rows[index].Cells[5].FindControl("txtQuantity")).Text;

// if you define Key when binding the GridView, if you need to get the key of that line use the following
gView.DataKeys[index].Value;
}

and with this, you can do a lot of things.. just need to explore it.

Hope it helps :)

paging and sql server, using Row_Number()

Posted by gondar | Filed under ,

imagine you need to select only some data from a table, where that data, when is ordered by some field, probably the id, if is sequential, wont be as you would like to select the rows beetween position 5 and 15, for example, so in this cases we need to use Paging, one options is this i'll show:

declare @temp table (field1 datetime,field2 nvarchar(10))

declare @start int,

@end int

select @start=1,@end=20

while @start<=@end

begin

insert into @temp values (getdate()+@start,'Line '+cast(day(getdate()+@start)+@end as nvarchar(5)))

select @start=@start+1

end

select * from (

select *, row_number() over(order by field1) as 'Id' from @temp

)as tab

where Id between 5 and 15

hope it helps :)

for a better understanding about Database Normalization

Posted by gondar | Filed under , ,

check out this web page, it has a very simple and all explayned example:
http://www.phlonx.com/resources/nf3/

or download the printable version of it ---> The3NormalForms.pdf (436.53 kb)

other example here:
http://www.bus.iastate.edu/mennecke/533/s03/normalization_examples.htm

hope it helps
[[]]

How do i create a table with primary keys and foreign keys ?

Posted by gondar | Filed under , , , , ,

1º - lets create a simple table, with a primary key field:

create table __TableName__
(
Field_Id int
,
Name nvarchar(20
),
constraint __PK_Name__ primary key (Field_Id
),
constraint __FK_Name__ foreign key (Field_Id) references __ReferencedTable__
)

- in this example, the field "Field_Id" is a primary key in a foreign table, and for some reason, we need it to be unique at this table to,
- after add the PK, we should add the FK, in this way, we guarantee that no one can delete the row in the "__ReferencedTable__" WHEN, there is a row at this table with that "Field_Id"

2º - creating a table with 2 or more primary keys, that are from foreign tables:

create table __TableName__
(
Field_Id int,
Field2_Id int,
constraint
__PK_Name__ primary key (Field_Id,Field2_Id),
constraint
__FK_Name1__ foreign key (Field_Id) references __ReferencedTable__
,
constraint
__FK_Name2__ foreign key (Field2_Id) references __ReferencedTable2__

)

hope it helps
[[]]

JayRock

Posted by gondar | Filed under
JayRock, didn´t read about it yet, only know that uses Json to call webservices..

http://jayrock.berlios.de/

DoJo

Posted by gondar | Filed under

Dojo, another "Jquery" library
http://www.dojotoolkit.org/

 

Json

Posted by gondar | Filed under

Json - Javascript Object Notation, it lets you access your server, sending data, partially, wich uses AJAX.
http://www.json.org/

examples:
http://www.ibm.com/developerworks/web/library/wa-ajaxintro11.html

Jquery

Posted by gondar | Filed under

Jquery, very great to turn your web application richer (the famous RIA - Rich Internet Applications) and to make simple changes, smoother, wich is always nice to the end user.

check it out
http://jquery.com/

books, in the page of Jquery, you will find some, i've bought this one


http://www.manning.com/bibeault/

how limit a asp:TexBox, with textmode set to multiline, a maxlenght value ?

Posted by gondar | Filed under , , ,

in a singleline textbox, is easy, just set the maxlenght value.. but when the textbox is set to multiline, all we no to do is go to source code and add the following to the asp:textbox tag:

 onkeypress='return (this.value.length <= 200);'

where the 200 is the maximum characters you want to allow users to write

hope it helps :)