Friday, January 28, 2011

Add controls dynamically

 Explain how to add controls dynamically to the form using C#.NET.The following code can be called on some event like page load or onload of some image or even a user action like onclick
protected void add_button(Button button)
{
try
{
panel1.Controls.Add(button); // Add the control to the container on a page
}
catch (Exception ex)
{
label1.Text += ex.Message.ToString();
}
}

STORED PROCEDURE

Following steps may helpfull for you to write the stored procedure:
  Q : Need a stored procedure which will help you to insert data in a database table.
( here database name is "anil"  and  table name is "emp". which consists the fields  eno,ename)

stored procedure formate:
USE  [database name]
CREATE PROCEDURE  [procedurename]
@fieldname1 datatype= _ ,
AS
BEGAN
"query"
END

Example 1:
Open query analyser in the sql server and write the following
USE  anil
CREATE PROCEDURE STP_EMP
@ENO
varbinary(MAX) = null,
@ENAME varchar(50) = null
 AS
BEGIN
 INSERT INTO emp(eno,ename) VALUES(@ENO,@ENAME)
END
Exicute the above procedure you will get the "Command exicuted Successfully msg".

Now Open the Default.aspx.cs page and write the following code:

SqlConnection con = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=anil;Integrated Security=True");

con.Open();
SqlCommand cmd = new SqlCommand("STP_EMP",con);// Call the procedure Name.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ENO",SqlDbType.Int).Value = TextBox1.Text;(or) 1;
cmd.Parameters.Add("@ENAME", SqlDbType.VarChar).Value = TextBox2.Text; (or )name ;
cmd.ExecuteNonQuery();
con.Close();

Tuesday, January 4, 2011

Redirecting to another page with some command argument

<asp:link button id="lnk1" runat="server"   NavigateUrl='<%# "page2.aspx?id=" + Eval("ID") %>'>More.....</link button>

Monday, December 13, 2010

Move to Top in the page

When ever your web site page size is very high , Users feel time wast for always scrolling up and down directions.
So if you give one step up link its give additional help to your site .for this you can use following code :

<a href="#top">top</a>
href="#top" field will help you move up in that page.

Thursday, December 9, 2010

Substring

string  s = "bloger";

//and then i want to remove the last chars "er"


string  g = s.Substring(0, s.Length - 2);


it will give     only  'blog'

to cut  first letter ,we start the sub string  from 1
string  g = s.Substring(1, s.Length - 2);

it will give  'log'  by removing first 2 letters and last 2 letters(chars)

Tuesday, December 7, 2010

PayPal

For integrating PayPal into your project ,Please go through the following link there you can get the complete code for the PayPal .And also the successive steps to follow how to integrate PayPal to your Application

Click here  : PayPal Link

Sunday, December 5, 2010

FileUpLoad control

 ASP.NET 2.0 has a FileUpLoad control, which allows developers to drop the control on a page and let it browse a file and upload it on the server.
To create a control, simply drop the FileUpload control from Toolbox to a Web page. The following code adds the FileUpLoad control:
<asp:FileUpLoad id="FileUpLoad1" runat="server" />
To support file upload, we need to add a Button control:
<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />
Now on this button click event handler, we need to call SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded.
protected void UploadBtn_Click(object sender, EventArgs e)
{
if (FileUpLoad1.HasFile)
{
FileUpLoad1.SaveAs(@"C:\anil\" + FileUpLoad1.FileName);
Label1.Text = "File Uploaded: " + FileUpLoad1.FileName ;
}
else
{
Label1.Text = "No File Uploaded.";
}
}


Now if you run the sample, browse a file, and click on Upload File button, the output looks like below Figure .

Figure   FileUpLoad control in action
Restricting File Types
You can restrict the FileUpload control to upload a file type. For example, I wanted to upload images (Jpgs and Gifs) only so I put a validator, which allows Gifs and Jpegs only.
<asp:RegularExpressionValidator
id="FileUpLoadValidator" runat="server"
ErrorMessage="Upload Jpegs and Gifs only."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$"

ControlToValidate="FileUpload1">
</
asp:RegularExpressionValidator>
Figure 3. Validating images only.
By this code we can upload the the file only up to size of 4MB . If you trying to upload more than 4 MB it shows error  and this is  why? 

The default size of files uploaded by the FileUpload control is 4MB. So if you try to upload the files larger than 4MB, it won't let you do so. To do so, you need to change the default file size in machine.config.comments file for maxRequestLength of httpRuntime tag.  This number is in KB.
<httpRuntime
executionTimeout = "110" [in Seconds][number
maxRequestLength = "4096" [number]
requestLengthDiskThreshold = "80" [number]
useFullyQualifiedRedirectUrl = "false" [true|false]
minFreeThreads = "8" [number]
minLocalRequestFreeThreads = "4" [number]
appRequestQueueLimit = "5000" [number]
enableKernelOutputCache = "true" [true|false]
enableVersionHeader = "true" [true|false]
apartmentThreading = "false" [true|false]
requireRootedSaveAsPath = "true" [true|false]
enable = "true" [true|false]
sendCacheControlHeader = "true" [true|false]
shutdownTimeout = "90" [in Seconds][number]
delayNotificationTimeout = "5" [in Seconds][number]
waitChangeNotification = "0" [number]
maxWaitChangeNotification = "0" [number]
enableHeaderChecking = "true" [true|false]
/>