COOKIES saving and reading in .Net

Category : ASP.Net, VB.Net

Here is the basic way to save and then read a cookie:

SAVING A COOKIE:

Private Sub buttSave_Click(ByVal sender as System.Object, _
ByVal e aAs System.EventArgs) Handles buttSave.Click
 
'Create the cookie
Dim usrCookie as New HttpCookie("UserInfo")
'Fill in the data
usrCookie("FirstName") = txtFirstname.Text
usrCookie("LastName") = txtLastname.Text
usrCookie("City") = txtCity.Text
usrCookie("ZIP") = txtZIP.Text
'Set expiration
usrCookie.Expires = DateTime.Now.AddDays(30)
'Add the cookie
Response.Cookies.Add(usrCookie)
End Sub

READING A COOKIE:

'Get cookie
Dim usrCookie Ad HttpCookie = Request.Cookies("UserInfo")
'Get data
txtFirstName.text = usrCookie("FirstName")
txtLastName.text = usrCookie("LastName")

SOMETHING TO REMEMBER:
Cookies ARE case sensitive! userInfoCookie and UserInfoCookie are not the same for cookies.

App_Code classes not showing up – Gotcha!

2

Category : ASP.Net

So you add a class to the App_Code folder but for some reason, you can’t see the class… frustrating right?

Well waste no more time.

For whatever reason (and I would love an explanation if someone knows) the App_Code folder sets elements’ build action to CONTENT. Simply change this to COMPILE and your classes will be visible.

EXCEL : Run, Fill, and Execute Macro… with .Net

Category : VB.Net

Using Visual Basic… if you need help translating to C#, let me know.

1 – First thing you will need to do is to add the Microsoft Excel Com object.

  • Right click your project and click Add Reference.
  • Click the COM tab – and scroll down until you find the Microsoft Excel Object Library (depending on your installed version – the exact number may be different but mine was Microsoft Excel 11.0 Object Library  — I have Office 2003 installed
  • Click OK and step one is finished!

2 – Create an excel document, setup any formatting, record your macro and save the document.

***
Now for me, I had an ASP app that I added an ASPX page to and did the code behind there.  I simply drug one button to my canvas because that was all I needed at the time.  THEN in that buttons click event (to automatically get it’s click event… double click on the button and you will be taken to the code behind and you will be in the click method) I created the code.

4 – Make a reference to your Office COM object.

Reference your Office com object in the top of your code behind like this:

Imports Microsoft.Office.Interop

5 – The Code.

In the button click event we make some variables — for me I made some that you may not need.
ALTHOUGH YOU WILL NEED a datagrid full of data to use the following code.

I created a Dataset from some data that I had lying around.
Dim Districts As DataSet = m_pDistrict.GetAllDistricts

Then created a table from that set and then a datagrid to bind the table to
Dim dt As DataTable = Districts.Tables(0)
Dim dg As New DataGrid

Following that I created my three Excel objects (fairly explanatory)
Dim oExcel As Excel.ApplicationClass
Dim wkbk As Excel.Workbook
Dim wksheet As Excel.Worksheet

Then I bound my data to my datagrid.
dg.DataSource = dt
dg.DataBind()

Now start excel and open the workbook.
oExcel = CreateObject(“Excel.Application”)
oExcel.Visible = True
wkbk = oExcel.Workbooks.Open(Server.MapPath(“/book1.xls”))
wksheet = wkbk.Worksheets(“Sheet1″)  <–NOTE that Sheet1 is the name of your worksheet in your excel doc… if you named it something else, then you need to reflect that here!!!

*** ALSO – the .OPEN above reflects the fact that I am using an ASP app.  If you are using a desktop app you can simply use something like C:\book1.xls  or Application.StartupPath…

NOW I loop through all my data and create a two dimensional OBJECT that will then be filled with my data.  I will then create an Excel Range object that I will resize to the size of my OBJECT that holds my data.  Once that is done, I simply assign the OBJECT to the Range.  (that may sound confusing but the code below will help…)

Dim iRowCount As Integer = dt.Rows.Count
Dim iColCount As Integer = dt.Columns.Count
Dim oData(iRowCount, iColCount) As Object

Dim iRow As Integer, iCol As Integer

For iRow = 0 To iRowCount – 1

For iCol = 0 To iColCount – 1

If Not IsDBNull(dt.Rows(iRow).Item(iCol)) Then
oData(iRow, iCol) = dt.Rows(iRow).Item(iCol)
Else
oData(iRow, iCol) = “”
End If

Next

Next

Dim XRange As Excel.Range = wksheet.Range(“A1″)
XRange = XRange.Resize(iRowCount, iColCount)
XRange.Value = oData

Finally, I call the .Run command with the name of the Macro and BOOM!  We are done.
oExcel.Run(“CreateHFZChart”)

=======

I am sure if you are reading this then you probably see the great utility of this piece of code.  This could be used to produce reports for users who then wish to manipulate the data for their businesses purposes.  And that is without the added functionality of being able to run the Macro.

The limitations on this code would be that the user would have to make sure NOT to save over your master document.  Although, this could probably easily be worked around by having a spare backup document that takes the place of the final document name each time the application is fired.

Anyway, this is a nice useable piece of functionality brought to you by .Net…. hope this helps someone!

t

Nullable object must have a value… why? what? huh?

Category : VB.Net

Ok, here is the situation, you have a method in a class that accepts a number of parameters.

myMethod (byval myint as integer, byval mydouble as double, etc.)

Now in your class you declare variables that you will send to that method but you want them to be Nullables because you are getting them from a database.

So you declare:

dim myInteger2 as Nullable(of Integer)
dim myDouble2 as Nullable(of Integer)

Then you test them from your dataset.

If Not mydatasetpieceofData("TheInteger") is DBNull.Value then
myInteger2 = mydatasetpieceofData("TheInteger")

etc….

Now you pass these variables onto your method….

myMethod( myInteger2, myDouble2 )

Run it and you will get an error that says “Nullable Object must have a value.

If you are an idiot like me, you will search and search the internet and finally find the answer. THEN DO NOTHING and forget about it in 2 weeks UNTIL YOU RUN INTO THIS STUPID ERROR AGAIN!!!!!!

Or if you are smart, you will bookmark this post or copy the answer …whatever… WRITE THIS DOWN SOMEWHERE!!! It is one of those stooopid errors that will drive you crazy and waste TONS of your time!!!!

** ANSWER:
————————————————————————————–
YOUR METHOD HAS TO LOOKING FOR NULLABLES! Our method needs to look like this:

myMethod (byval myint as Nullable(Of Integer), _
byval mydouble as Nullable(Of Double),etc.)

This may seem obvious to some, but it has occured in our workplace several times — it’s just ONE OF THOSE THINGS that you have to watch out for.  So I hope this helps….and oh ya, WRITE THIS DOWN!!!!

Create a pie chart, quick and dirty.

Category : VB.Net

Using VB.Net and Visual Studios 2005

Ok this app. wont have Dundas charts shaking in their boots (except with laughter), but it should give you a good starter into the Graphics class and it’s FillPie function.  (I dont know why but everytime I think FillPie, I smile… ?)

Anyway, using the code supplied you can choose several colors, choose their percentages, and then (once you have reached 100%) finally have the FillPie (smile) function fire and see your glorious pie (ok, smile again…very sorry) drawn in front of your eyes.

To me this was a good starter into graphics and some simple graphics functionality, I hope it will help you as well.

The final app looks like this:
Pie Chart Application‘ Draws a pie chart.
Public Sub DrawPieChart()

‘Grab the surface to draw the pie upon (here we are drawing it on our panel for contrast)
Dim surface As Graphics = Panel1.CreateGraphics

‘Where in the panel?
Dim location As Point = New Point(80, 60)

‘How big of a pie?
Dim size As Size = New Size(420, 420)

‘used for our draw pie function
Dim percentTotal As Integer = 0

‘Iterating through our list of structs
For percent As Integer = 0 To colorList.Count – 1

‘Grab and name the method for the brush – this wasnt necessary but it makes the brush look better
Dim myColor As Color = colorList(percent).colorName

‘Okay, within the Graphics class we have a method called FillPie that takes a Brush, Rectangle (this is where we tell location and size),
‘The starting point for the angle (our first point is 0 ( 0 * 360 / 100 )), and our ending point for our angle (how much percent we assigned to each color)
‘and we are done!
surface.FillPie( _
New SolidBrush(myColor), _
New Rectangle(location, size), CType(percentTotal * 360 / 100, Single), CType(colorList(percent).colorPercent * 360 / 100, Single))

percentTotal += colorList(percent).colorPercent

Next
Return
End Sub

=========FULL SOURCE CODE HERE==========