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 :)

how to format price values ? directly in sql server

Posted by gondar | Filed under , , , ,

credits: http://articles.techrepublic.com.com/5100-10878_11-5803705.html

in the select statment change the price column for:

CONVERT(varchar, CONVERT(money, YourFieldName), 1) + ' €'

and when you bind the query/procedure/view, it will return the value already formated

hope it helps :)

100% height in ie8 problem... fixed :)

Posted by gondar | Filed under ,

i always used this, to get a div in 100% height:

[Head]
<style type="text/css">
html, body{height:100%;}
</style>
...

[Body]
<body style="margin: 0;">
...

And then add a style to the div, table, or something else with property width and height at 100%...
but with internet explorer 8, this isn´t enough... then i foud this:

[Head]
<style type="text/css">
html {height:100%; width:100% }
</style>

[Body]
<body style="margin: 0px; height: 100%; width: 100%; padding: 0px">
<form id="form1" runat="server" style="height: 100%; width: 100%;">

Now, you just need to add the same tag, the style property height set to 100%

hope it helps :D

credits: http://forums.asp.net/t/1445134.aspx

using substring with charindex

Posted by gondar | Filed under ,

problem: we need to split the follwing string to cross the references or something else...

String input: Ref1|Ref2|Ref3|Ref4|Ref5|Ref6|Ref7|Ref8|Ref9|Ref10|Ref11|Ref12||||||||
Output we want:

ref1 - now that we have the first element we could insert it on a table or query the table to udpate blah blah blah...
ref2 - ...
ref3 - ...
ref4 - ...
...

so.. how we do this ?! simple :)

declare @string varchar(500),

@SpaceIndex TinyInt,

@temp varchar(500),

@val int

set @val=0

set @temp=''

set @string='Ref1|Ref2|Ref3|Ref4|Ref5|Ref6|Ref7|Ref8|Ref9|Ref10|Ref11|Ref12||||||||'

while @val=0

begin

if len(@string)>0

begin

SEt @SpaceIndex = CHARINDEX('|', @string)

if @SpaceIndex=0 and len(@string)>0

begin

SELECT @temp=@string set @val=1

end

else

begin

SELECT @temp=LEFT(@string, @SpaceIndex -1)

select @string=SUBSTRING(@string,len(@temp)+2,len(@string))

end

print('---> '+@temp)

end

else

set @val=1

end

how to format/rearrange html code automaticaly ?

Posted by gondar | Filed under , , , ,

in visual studio go to:
Tools > Options > Text Editor > HTML > Miscellaneous
and select the "Format HTML on paste", and press the ok button.
after this, if you copy and paste some html code, it will be well formated.

hope it helps.

how can we change a password for a user? (programmatically)

Posted by gondar | Filed under , ,

very often, users forget theyr passwords.. we could add a passwordrecovery control and/or the admin could change it, without problems.

credits for this post: http://forums.asp.net/p/965209/1206792.aspx

1º - in our web.config file, we need to add a second provider (AspNetSqlMembershipProvider > AspNetSqlMembershipProvider_2, for ex.)

2º - change some settings (at least this 2):

enablePasswordReset="true" requiresQuestionAndAnswer="false"

 3º - the code:

//instantiate the 2 provider, wich doesnt require passwordAnswer
MembershipProvider mp = Membership.Providers["AspNetSqlMembershipProvider_2"];

//instantiate the user to changepassword
MembershipUser user = mp.GetUser(__username__, false);

//with the principal provider we get the user information, and pass a resetpassword with the 2 provider
//wich will return a new password generated, and then the new password
Membership.GetUser(__username__).ChangePassword(user.ResetPassword(), "__NewPassword__");

hope it helps [[]]

how to create a excel file using windows forms ?

Posted by gondar | Filed under , ,

the credits for this goes to: http://csharp.net-informations.com/excel/csharp-create-excel.htm

1º - using Excel = Microsoft.Office.Interop.Excel;

2º - Project > Add Reference

3º - in the COM separator select "Microsoft Excel 12.0Object Library"

4º - paste the following code:

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.
Application xlApp;
Excel.
Workbook xlWorkBook;
Excel.
Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp =
new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.
Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Cells[1, 1] =
"hello 1";
xlWorkSheet.Cells[1, 2] = "hello 2";

xlWorkBook.SaveAs(
"c:\\excelFile.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue);xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.
Marshal.ReleaseComObject(obj);
obj =
null;
}
catch (Exception ex)
{
obj =
null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}

hope it helps [[]]

how to clean/shrink a database log file ?

Posted by gondar | Filed under , , ,

many times our databases log files, increase so much that they very often have 5 times or more than database size.

one of the things we can do, is shrink the log file, wich will clean all operation we made with the database that are saved, for security proposes.

ex.: database products

DBCC SHRINKFILE('products_log', 1)
BACKUP LOG products WITH TRUNCATE_ONLY DBCC SHRINKFILE('products_log', 1)

this, will set the actuall size of the database to 1Mb :)

it´s simple

hope it helps

 

how to send a email? (using MailMessage)

Posted by gondar | Filed under

to use the mailmessage to send emails, 1 we need to add:

using System.Net.Mail;
using System.Net;

and then instantiate a new mailmessage class, and then use the following to send a simple mail

MailMessage mail = new MailMessage();
mail.From = new MailAddress("__from_email__");
mail.To.Add("__to_email__");
mail.Subject = "Subject here";
mail.Body = "Message here";
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient("__smtp__");
smtp.Send(mail);

some email providers doesn´t accept an email if it doesn´t have credentials, so we must specify it, it accepts any valid email (and password) that you have, so we need the following:

smtp.Credentials = new NetworkCredential("__email__", "__password__");

and then the complete code is:

MailMessage mail = new MailMessage();
mail.From = new MailAddress("__from_email__");
mail.To.Add("__to_email__");
mail.Subject = "Erro --> Geco";
mail.Body = "Utilizador: ";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("__smtp__");
smtp.Credentials = new NetworkCredential("__email__", "__password__");
smtp.Send(mail);

hope it helps

how to reset the id of a identity column ?

Posted by gondar | Filed under

when we are developing something, most of the time we insert dump data into some tables, and then some testes need to have fresh ids, for some reason, so, how we do that ?

example: reset the id of table product

DBCC CHECkIDENT (product, RESEED,0)

where 0 is the value that when incremented with the value selected (usually is 1) will give the id for the first row inserted, in this case will be 1 (0 + 1)

hope it helps