This is my preferred method. (Microsoft has controls such as ScriptManager and ScriptManagerProxy but like nearly everything they do, I believe its overkill) To me, this is the simplest method.
The Problem: You want to convert or to create a user control that has scripts and/or css.
Solution: Within the Page_Load event of the user control, simply include the following:
//For Scripts - notice we add this script call to the PARENT HEADER
LiteralControl jQuery = new LiteralControl();
jQuery.Text = "<script type=\"text/javascript\" src=\"js/jquery.js\"></script>";
Page.Header.Controls.Add(jQuery);
//For CSS - You could add Media under attributes as well.
HtmlLink stylesLink = new HtmlLink();
stylesLink.Attributes["rel"] = "stylesheet";
stylesLink.Attributes["type"] = "text/css";
stylesLink.Href = "css/yourPagesCSS.css";
Page.Header.Controls.Add(stylesLink);
Hope this helps someone! t
Use “override” keyword and make sure that your AutoEventWireup is set to True in Markup.

And there you go!
Ciao!
Make sure to add the ” runat=’server’ ” in the markup.
myIFrame.Attributes["src"] = "http://www.google.com";
Category : C#, VB.Net
Tags: iis

Cannot open database “” requested by the login. The login failed.
Login failed for user ‘IIS APPPOOL\DefaultAppPool’.
I got this error when I added an app onto IIS 7 and was trying to hit my local Database. Here is the fix:
1) In IIS7 – Click ‘Application Pools’ (left side)

—
2) On Top Right – Click ‘Set Application Pool defaults’

—
3) Finally, set ‘Process Model > Identity’ to ‘Local System’

Category : ADO / SQL, ASP.Net, C#
Tags: C#, SQL

THE KEY LINE BELOW IS : SqlCommandBuilder.DeriveParameters(cmd);
SqlConnection c = new SqlConnection(TextboxConnString.Text);
//NAME OF STORED PROC
string sql = DropdownStoredProcs.Value.ToString();
SqlCommand cmd = new SqlCommand(sql, c);
cmd.CommandType = CommandType.StoredProcedure;
c.Open();
SqlCommandBuilder.DeriveParameters(cmd);
foreach (SqlParameter param in cmd.Parameters)
{
if ((param.Direction == ParameterDirection.Input) || (param.Direction == ParameterDirection.InputOutput))
{
//Notice that I can get the Name of the parameter as well as it's DBType...
string paramDescription = param.ParameterName + " | " + param.SqlDbType.ToString();
DropdownSPParameters.Items.Add(paramDescription, param.ParameterName);
}
}
c.Close();
Category : ADO / SQL, C#
Tags: C#, SQL
This particular example fills a dropdown list with the list of Stored Procedures and takes it’s connection string from a textbox.
SqlConnection c = new SqlConnection(TextboxConnString.Text);
c.Open();
string sql = "SELECT name AS spname FROM sysobjects WHERE (xtype = 'p') ORDER BY name";
SqlCommand cmd = new SqlCommand(sql, c);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dtStoredProcs = new DataTable();
adapter.Fill(dtStoredProcs);
if (dtStoredProcs.Rows.Count > 0)
{
DropdownStoredProcs.Items.Clear();
foreach (DataRow dr in dtStoredProcs.Rows)
DropdownStoredProcs.Items.Add(dr["spname"].ToString(), dr["spname"]);
}
c.Close();
SCENARIO:
Created a simple login that checked user name and password against a database and then sent user to the proper page if verified. (also writing a ‘logged in’ session variable)
Within the pages Page_Load event, I check for the proper session variable and if not present send them back to login screen.
Additionally, I added a logout link that wiped the variable and sent them back to login….
THING all appeared to work great… UNTIL I tried to navigate straight to a page AFTER logging out. The page was able to be accessed EVEN though I had logged out AND my Page_Load event was not even getting called.
====
REASON/SOLUTION:
The pages were being cached and therefore did not need to check the Page_Load event which would have caught the fact that the user was not properly logged in…. The solution is to place the following code in your Load event.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
** This is not a proper solution for a large solution with a lot of pages but for this purpose I only had one Default page with user controls … if you have many pages you should use the auth ticket option.
Category : ASP.Net, C#
Tags: ASP.Net, C#
This worked well for me. I created the TABLE in markup [MAKE SURE ITS AN ASP:TABLE with the runat server tag].
Then you can manipulate it from code-behind like so:
TableRow tr = new TableRow();
TableCell tc = new TableCell();
Label label2 = new Label();
label2.Text = "LAJLKFJDSLJFLKDF";
tc.Controls.Add(label2);
tr.Cells.Add(tc);
MyTable.Rows.Add(tr);
Hope this helps!
Category : C#, VB.Net
Tags: C#, VB.Net
For Each loops is a read-only loop! In other words, you cannot mutate the reference you are referring to.
A For Next loop allows mutation, plus I like For Next’s because they allow me to determine if I am on the final loop or not…
Examples,
For x as integer = 0 to myStringArray.Count – 1
myStringArray(x) += “add this to end”
if x = myStringArray.Count – 1 then myStringArray(x) += “, and this is the last element!”
Next
cheers!
t
Category : C#, VB.Net
Tags: C#, VB.Net
I would just like to point out two areas where both C# and VB.Net beat the other one in the usage of arrays.
C# PRO:
You can redefine your array simply in C# by simply reinitializing the array:
//Declare array
int[ ] myInts = new int[20];
//Now reinitialize like this
myInts = new int[40];
In VB you have to reinitialize with the ReDim keyword
Dim myInts(20)
ReDim myInts(40)
Not a big deal but I give the better implementation to C# on this one…however.
===========================================
In VB.Net you can actually reinitialize an array AND MAINTAIN the data within the current array!
This is accomplished by using the ‘Preserve’ keyword. (this is not available in C#)
'Declare array
Dim myInts() As Integer = { 1,2,3,4 }
'Resize array and keep the data with the Preserve keyword
ReDim Preserve myInts(20)