ClanKiller.com http://forums.clankiller.com/ |
|
c# code snippets http://forums.clankiller.com/viewtopic.php?f=24&t=2314 |
Page 1 of 1 |
Author: | Satis [ Thu Apr 19, 2007 7:26 am ] |
Post subject: | c# code snippets |
Ok, so I have one for javascript, now I'm making one for c# (in an asp.net context). how to tell if a number is even or odd [php] if ((i & 1) != 1) //if odd { rowclass = " class='shaded'"; } or if ((i & 1) == 1) //if even { rowclass = " class='shaded'"; } [/php] multidimensional array initialization I always forget this, it's just different enough from php to confuse me [php] string [,] varname= new string[2,3]; or string[,] varname= new string[,]{ { "whatever","things","doh" }, { "","spacer","" }, { "things","radioheader","" }, } [/php] and a quick how to iterate through it [php]for (int i = 0; i < (varname.Length / 3); i++) { returnstring += varname[i, 0] + " " + varname[i, 1] + " " + varname[i, 2] + "<br>"; }[/php] I'm sure I'll have more later |
Author: | Satis [ Tue Jul 24, 2007 12:47 pm ] |
Post subject: | |
some more stuff.... getting the current executing page [php] Path.GetFileName(Request.FilePath) [/php] do a page redirect [php] Response.Redirect("login.aspx", true); //true means to stop script execution. //otherwise script execution of the current page continues [/php] setting cookies [php] //set cookies HttpCookie usernameCookie = new HttpCookie("username"); HttpCookie passwordCookie = new HttpCookie("password"); usernameCookie.Value = this.Username; passwordCookie.Value = this.EncryptedPassword; usernameCookie.Expires = System.DateTime.Now.AddHours(cookietime); passwordCookie.Expires = System.DateTime.Now.AddHours(cookietime); HttpContext.Current.Response.Cookies.Add(usernameCookie); HttpContext.Current.Response.Cookies.Add(passwordCookie); [/php] killing cookies [php] HttpCookie username_cookie = HttpContext.Current.Request.Cookies["username"]; HttpCookie password_cookie = HttpContext.Current.Request.Cookies["password"]; try { username_cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(username_cookie); } catch{ } try { password_cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(password_cookie); } catch { } [/php] sessions [php] //set sessions Session["username"] = this.Username; Session["password"] = this.EncryptedPassword; Response.Write(Session["username"]); Response.Write(Session["password"]); //kill sessions Session.Abandon(); [/php] |
Author: | Satis [ Wed Jul 25, 2007 6:51 am ] |
Post subject: | |
building a multidimensional array from a database. Multidimensional arrays are one of c#'s major weaknesses and probably the biggest thing I don't like about it. *edit* ok, the non breaking space thing is really annoying [php] string select = @"SELECT count(*) FROM mytable WHERE whatever > 0"; doQuery(select); //this is a method I built that sets an OleDbDataReader object with the query results. dr.Read(); int numItems = Convert.ToInt16(dr.GetValue(0)); dr.Dispose(); conn.Close(); //create the array ... I made it a string... no mixed arrays without using some list type or something int num_fields = 5; string[,] multidimensionalArray = new string[numItems, num_fields]; select = @"SELECT one, two, three, four, five FROM mytable WHERE whatever > 0"; doQuery(select); int i = 0; while (dr.Read()) { //populate the array for (int k = 0; k < num_fields; k++) { multidimensionalArray[i, k] = Convert.ToString(dr.GetValue(k)); } } dr.Dispose(); conn.Close(); //you now have the array. You can iterate through it similarly as setting it Response.Write("<table>"); for (i = 0; i < numItems; i++) { Response.Write("<tr>"); for (int k = 0; k < num_fields; k++) { Response.Write("<td>" + multidimensionalArray[i, k] + "</td>"); } Response.Write("</tr>"); } Response.Write("</table>"); //one gotcha. ... multidimensionalArray.Length() does not give you the number of rows, //it gives you the number of elements, so you HAVE to know how many elements / row. [/php] |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |