Monday, August 30, 2010

ProperCase

What are we cooking today?
A very simple function to put the first letter in Uppercase


Recipe
label1.text=  StrConv(txtFName.Text, VbStrConv.ProperCase)

Friday, August 13, 2010

Datagridview to text file

What are we cooking today?
A way to get what you have in a datagridview into a .txt file

Recipe

Public Shared Function DatagridviewToTextFile(ByVal strFileWithPath As String, ByVal dgv As DataGridView) As String
        Dim fsStream As New FileStream(strFileWithPath, FileMode.Create, FileAccess.Write)
        Dim swWriter As New StreamWriter(fsStream)
        Dim LineToWrite As String = String.Empty

        Try
            For rowIndex As Integer = 0 To dgv.Rows.Count - 1
                LineToWrite = String.Empty
                For colIndex As Integer = 0 To dgv.Columns.Count - 1
                    LineToWrite &= "," & dgv.Rows(rowIndex).Cells(colIndex).Value.ToString
                Next
                LineToWrite = LineToWrite.Remove(0, 1) 'remove the first comma
                swWriter.WriteLine(LineToWrite)
            Next
            swWriter.Flush()
            swWriter.Close()

            Return "File was saved succesfully!"
        Catch ex As Exception
            Throw New Exception(ex.Message)
            Return "Error writting to file -" & ex.Message
        End Try

    End Function





Thursday, August 12, 2010

A delicious copy from table to table in SQL SERVER (2008)

What are we cooking today?
Copy from a table1 into a table2
Copy from the result set from a stored procedure into an existing table2
Copy from table1 into a table2 that does not exist yet (to be created)

Recipe

---When the Table2 (target table) was previously created.
INSERT INTO Table2 (fname, lname)
SELECT fname, lname
FROM Table1

---When the Table2 (target table) was previously created. [From an stored procedure]
INSERT INTO Table2 (fname, lname)
exec spGetInfo


---When the table2 (target table) WAS NOT created.
SELECT fname, lname
INTO Table2
FROM Table1


Enjoy it!