Thursday, September 25, 2008

Coding for changing form background color using CSS

public void Menu_Color_Selection_Method()
{
string color_name = DropDownList1.SelectedValue.ToString();
string path = "CSS\\";
string filename = color_name.ToString();
string extension = ".css";
stylesheet.Href = path + filename + extension;
Image1.ImageUrl = "images\\" + color_name + "_image.jpg";
}

Monday, September 22, 2008

Coding for Accessing Database File USing Code file with DataTable

Note:Place below code in App Folder



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
///
/// Summary description for Class1
///

public class Class1
{
SqlConnection myconn;
string name,name_copy,ss;
string myConnstr;

public void sql_connection()
{

}
public Class1()
{

}

public DataTable execQuery(string myVarSQL)
{
// added SqlCommand Object 2004-02-02 to control command timeout;
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=ctsi_web;Integrated Security=True");
myConn.Open();
SqlCommand myCmd = new SqlCommand(myVarSQL, myConn);
//myCmd.CommandTimeout = myCmdTimeout;
myCmd.CommandType = CommandType.Text;
SqlDataAdapter myDA = new SqlDataAdapter(myCmd);


DataTable myDT = new DataTable();
myDA.Fill(myDT);
myConn.Close();
return myDT;
}

}

Code For Filename.aspx.cs file



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
Class1 global_declaration = new Class1();
DataTable dt = new DataTable();//creating new Table
protected void Page_Load(object sender, EventArgs e)
{
dt = global_declaration.execQuery("exec sample");//"sample" is a store procedure
foreach(DataRow dr in dt.Rows)//copying all data in datatable and acess from that datatable for Time Consuming
{
string name = dr["Menu_Index"].ToString();
Response.Write("THE VALUES ARE :" + name.ToString());
}

}
}

Coding For Creating Main menu and Sub menus at run time

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class samples : System.Web.UI.Page
{
#region variables_used
static int i;
static string names;
string main_menu_name;
int j;
string[] arr_name = new string[4];
string value;
string index_value;
#endregion

#region SQL_CONNECTION_STRING
SqlConnection con_header = new SqlConnection("Data Source=.;Initial Catalog=ctsi_web;Integrated Security=True");
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ctsi_web;Integrated Security=True");
#endregion

#region Methods_Used

public void sub_menu()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from master_submenus where Menu_Index='" + names.ToString() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{


TableRow tr = new TableRow();
tr.BackColor = System.Drawing.Color.Tomato;


TableCell tc = new TableCell();
tc.ForeColor = System.Drawing.Color.White;
tc.BackColor = System.Drawing.Color.Tomato;
string sub_menu = dr["Sub_Menu_Names"].ToString();
string url = dr["URL"].ToString();
tc.Text = "" + sub_menu.ToString() + "";

string name = dr["Sub_Menu_Names"].ToString();
tr.Cells.Add(tc);

Table2.Rows.Add(tr);

Response.Write("
");



}
//table design style
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.HorizontalAlign = HorizontalAlign.Left;
tableStyle.VerticalAlign = VerticalAlign.Middle;
tableStyle.Width = Unit.Pixel(700);

//aplying design style for table1
foreach (TableRow r in Table1.Rows)
foreach (TableCell c in r.Cells)
c.ApplyStyle(tableStyle);

con.Close();
}
#endregion


protected void Page_Load(object sender, EventArgs e)
{

con_header.Open();//for main menu connection
SqlCommand cmd_header = new SqlCommand("select * from master_mainmenu", con_header);
SqlDataReader dr_header = cmd_header.ExecuteReader();

while (dr_header.Read())
{
//place main menu items in table 1

TableRow tr_header = new TableRow();
TableCell tc_header = new TableCell();

tc_header.BackColor = System.Drawing.Color.Chocolate;
main_menu_name = dr_header["Menu_Names"].ToString();
index_value = dr_header["Menu_Index"].ToString();

//create link for main menu items
tc_header.Text = "" + main_menu_name.ToString() + "";
tr_header.Cells.Add(tc_header);
Table1.Rows.Add(tr_header);
i = i + 1;
names = subOpt.Value.ToString();
}

//calling sub_menu creation Function
sub_menu();

//table design style
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.HorizontalAlign = HorizontalAlign.Center;
tableStyle.VerticalAlign = VerticalAlign.Middle;
tableStyle.Width = Unit.Pixel(200);

//aplying design style for table1
foreach (TableRow r in Table1.Rows)
foreach (TableCell c in r.Cells)
c.ApplyStyle(tableStyle);

//aplying design style for table2
foreach (TableRow rr in Table2.Rows)
foreach (TableCell cc in rr.Cells)
cc.ApplyStyle(tableStyle);
}
}

Tuesday, September 16, 2008

coding for downloading file using c#.net

Using System.Net;

// for Downloading file from net
WebClient c = new WebClient();//for accessing net client
string s = c.DownloadString("http://www.w3schools.com");//read all string from given URL
Response.Write(s.ToString()); //view the page

// path name to download file //paht name to save a file
c.DownloadFile("http://w3schools.com/html/default.asp", "C:\\Documents and Settings\\Administrator\\My Documents\\vijay_programs\\w3schools_index.asp");

coding for Encoding and Decoding

//encode
string data = "a";
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
Response.Write(encodedData.ToString());

//decode

System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(encodedData.ToString());
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
Response.Write(result.ToString());