Thursday, December 2, 2010

Clear all the TextBoxes in a form

What are we cooking today?
How to clear TextBoxes in a form that uses masterpages. (C#)


Recipe

This procedure clears all the textboxes in a form. Please note this works when using a masterpage:
  
    public void ClearTextboxesInForm(string strContentPlaceHolder)
    {
        foreach (Control control in Form.Controls) 
        {
            if (control.ID == strContentPlaceHolder) 
            {
                foreach (Control ctrl in control.Controls)
                {
                    if (ctrl is TextBox)  //---Find the textboxes 
                    {
                        ((TextBox)ctrl).Text = "";
                    }
                }
            }
        }
    }

Wednesday, September 22, 2010

Block search engines


What are we cooking today?
How to block Search Engines from indexing site.


Recipe
  1. Add a robot.txt file with instructions to prevent all robots from indexing the page
    User-agent: * Disallow: /
  2. I added a tag in the links to prevent from indexing too.
    Sample http://www.mylink.com rel="nofollow">My Link
  3. I added a metatag :  

    Other details

    To exclude all robots from the entire server
    User-agent: * Disallow: /
    To allow all robots complete access
    User-agent: * Disallow:
    Or create an empty “/robots.txt” file.
    To exclude all robots from part of the server
    User-agent: * Disallow: /cgi-bin/ Disallow: /tmp/ Disallow: /private/
    To exclude a single robot
    User-agent: BadBot Disallow: /
    To allow a single robot
    User-agent: WebCrawler Disallow: User-agent: * Disallow: /
    More on robots --> http://www.robotstxt.org/


Friday, September 10, 2010

MS Access connection on 64 bit

What are we cooking today?
Connecting to an Access Database that was working OK on a 32 bit OS but was moved to a 64bit OS like Windows Server 2008.

If you use Microsoft.Jet.OLEDB.4.0 when your server has 64 bit, it just don't work.

Recipe

Go to 



http://www.microsoft.com/downloads/en/details.aspx?FamilyID=C06B8369-60DD-4B64-A44B-84B371EDE16D&displaylang=en




download and install the Microsoft access database engine .exe and change the connection string that fits your case.


The one that worked for me was:




"...If you are application developer using ODBC to connect to Microsoft Office Access data, set the Connection String to
 “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file” ..."


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!

Thursday, July 29, 2010

Roundtrip XML - Datagrid

What are we cooking today?
Need to load from XML into a datagridview, allow the user to modify the gridview and then copy from the datagridview back into the same XML document.

RECIPE


Imports System.Data
Imports System.Xml

From XML to datagridview
  Try
           Dim  strPathToXML as string = "...path to Xml doc .../myXML.xml"
           Dim xmlFile As XmlReader
           Dim ds As New DataSet

            xmlFile = XmlReader.Create(strPathToXML, New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            myDataGrid.DataSource = ds.Tables(0)
             xmlFile.Close() '---you need this closed to be able to write

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


Let the user modify the datagridview
Here modify a value in the DataGridview


From datagridview to XML 
   Dim mydataset As New DataSet
        Try
            mydataset = DatagridviewToDataset(myDataGrid)
            mydataset.WriteXml(strPathToXML) 
 
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    
 Function DatagridviewToDataset 
Public Function DatagridviewToDataset(ByVal dgv As DataGridView) As System.Data.DataSet
        Dim ds As New DataSet
      
        Try
            '---Add a new table to the dataset
            ds.Tables.Add("myTable")

            '---Add the columns
            Dim col As System.Data.DataColumn

            '---For each colum in the datagridveiw add a new column to your table
            For Each dgvCol As DataGridViewColumn In dgv.Columns
                col = New System.Data.DataColumn(dgvCol.Name)
                ds.Tables("myTable").Columns.Add(col)
            Next

            '---Add the rows from the datagridview
            Dim row As System.Data.DataRow
            Dim colcount As Integer = dgv.Columns.Count - 1

            For i As Integer = 0 To dgv.Rows.Count - 1
                row = ds.Tables("myTable").Rows.Add

                For Each column As DataGridViewColumn In dgv.Columns
                    row.Item(column.Index) = dgv.Rows.Item(i).Cells(column.Index).Value
                Next

            Next

            Return ds
        Catch ex As Exception
            MessageBox.Show("Error Converting from DataGridView" & ex.InnerException.ToString, _
            "Error Converting from DataGridView", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End Try
    End Function


Enjoy it!