Add JQuery, Javascript, CSS to ASPx User Controls parent

Category : ASP.Net, C#, JQuery/JScript

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

C# – How to get Page Life Cycle Events

Category : ASP.Net, C#

Use “override” keyword and make sure that your AutoEventWireup is set to True in Markup.

And there you go!

Ciao!

Controlling the SRC of an IFRAME from code behind.

Category : ASP.Net, C#, VB.Net

Make sure to add the ” runat=’server’ ” in the markup.

 myIFrame.Attributes["src"] = "http://www.google.com";

Cannot open database “” requested by the login. The login failed. Login failed for user ‘IIS APPPOOL\DefaultAppPool’.

Category : C#, VB.Net

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’

How to get the parameters from your Stored Procedures in code behind.

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

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();

How to get a list of your databases Stored Procedures from code behind.

Category : ADO / SQL, C#

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();

Creating a TABLE in the code behind.

Category : 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!

Important difference between for each and for next loops.

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

C# Vs. VB – Arrays….

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