I Love LINQ!
I started using LINQ about 8 months ago, before that I was strictly a stored procedure kind-of-guy. LINQ has made my programming life so much easier, no longer do I have to sit there writing SP’s for every database query I require.
These days my database creation steps look like this:
1. Create database in SQL Server Management Studio
2. Open the Server Explorer in Visual Studio and connect to my database
3. Add a LINQ to SQL Classes file to my project
4. Drag the database tables from the Server Explorer into the LINQ to SQL file
5. Create any associations / inheritance between tables (Right click on the table object, hover over the Add menu option)
6. Save my project
Once these steps have been finished and after I press the save button Visual Studio automatically creates all the classes required for database interaction.
Accessing the database and tables via LINQ is also super easy. Lets say we have a program that sells hotdogs to people. Here’s how I would add a hotdog sale to the database:
hotdogDBContext db = new hotdogDBContext();
sale newSale = new sale();
s.price = 2;
s.seller = "Frank"
s.date = DateTime.Now;
db.sale.InsertOnSubmit(newSale);
db.submitChanges();
Now lets say we want to grab all of Franks sales from the database:
hotdogDBContext db = new hotdogDBContext();
IEnumerable<sale> franksSales = from s in db.sales
where s.seller == "Frank"
select s;
IEnumerable<sale> franksSalesData = franksSales.ToList();
foreach(sale s in franksSalesData)
{
Response.Write(s.Seller + " " + s.price + " " s.date);
}
Pretty simple isn’t it.
LINQ is just another great way for programmers to save time and make coding a little more enjoyable.
Wednesday, April 28, 2010
Read More
Dynamic AJAX Accordian Control
Took me a while to figure this out, so I though I’d share! Here’s the code you require to create a Dynamic AJAX Accorian Controls in C#.
Put the following in your CSS file:
accordionHeader
{
border: 1px solid #2F4F4F;
color: white;
background-color: #313C53;
font-family: Arial, Sans-Serif;
font-size: 12px;
font-weight: bold;
padding: 5px;
margin-top: 5px;
cursor: pointer;
}
.accordionContent
{
background-color: #313C53;
border: 1px solid #2F4F4F;
border-top: none;
padding: 5px;
padding-top: 10px;
}
.accordionLink
{
background-color: #D3DEEF;
color: white:
}
This is the code you want in the Page_Load() section.
//Create our Accordian and Accordian Pane Objects
AjaxControlToolkit.Accordion accordian = new AjaxControlToolkit.Accordion();
AjaxControlToolkit.AccordionPane acordianPane =
new AjaxControlToolkit.AccordionPane();
// Configure Accordian Properties
accordian.ID = hostelId;
accordian.FadeTransitions = true;
accordian.FramesPerSecond = 10;
accordian.TransitionDuration = 500;
accordian.AutoSize = AjaxControlToolkit.AutoSize.None;
accordian.HeaderCssClass=”accordionHeader” ;
accordian.ContentCssClass = “accordionContent”;
accordian.RequireOpenedPane = false;
accordian.SuppressHeaderPostbacks = false;
accordian.SelectedIndex = -1;
// Create the Header Content and Main Content
Literal headerContent = new Literal();
headerContent.Text = “Accordian Header”;
Literal content = new Literal();
content.Text = “Accordian Content”
// Add Header and Conent to the Accordian Pane
acordianPane.HeaderContainer.Controls.Add(headerContent);
acordianPane.ContentContainer.Controls.Add(content);
accordian.Panes.Add(acordianPane);
// Add the Accordian to the ScriptManager (this code works with masterpages)
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
ScriptManager.Controls.Add(accordian);
So there you have it, an easy way to create a dynamic AJAX Accordian control at runtime!
Wednesday, April 28, 2010
Read More