Here are some code snippets I use allot, they may be useful to someone. Trying to get them into one central source I can access from everywhere. Copy & Paste for the win.
Here is the basic code for a SubRoutine, a Function, and the PageLoad Sub:
Protected Sub SubRoutineName(ByVal variable As String, Optional ByVal variable2 As Integer = 0)
‘Code
End Sub
Protected Function FunctionName(ByVal variable As String, Optional ByVal variable2 As Integer = 0) As Boolean
‘Code
Return variable3
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘Code
End Sub
Programmatically Create a SQL Data Source:
Dim SQLDataSource1 As New SqlDataSource
SQLDataSource1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings(”ConnectionString”).ConnectionString
Programmatically Insert with a SQL Data Source:
SQLDataSource1.InsertCommand = “INSERT INTO [Table_Name] ([Table_Field1], [Table_Field2]) VALUES (’string’,'” & variable & “‘)”
SQLDataSource1.Insert()
Programmatically Delete with a SQL Data Source:
SQLDataSource1.DeleteCommand = “DELETE FROM [Table_Name] WHERE [Table_Field1] = ‘” & variable & “‘ AND [Table_Field2] = ’string’”
SQLDataSource1.Delete()
Programmatically Update with a SQL Data Source:
SQLDataSource1.UpdateCommand = “UPDATE [Table_Name] SET [Table_Field1] = ’string’ WHERE [Table_Field2] = ‘” & variable & “‘”
SQLDataSource1.Update()
Programmatically Create a Data Source selection with a LinqToSQL Class: (requires LinqToSQL Class file)
Dim Database As New DatabaseDataContext ‘DatabaseDataContext is your LinqToSQL Class Context
Dim selection = From s In Database.DatabaseTable _ ‘Pick Data Table
Where s.FieldName1 = “string1″ _ ‘Where Clause
And s.FieldName2 = “string2″ _ ‘AND
Or s.FieldName3 = variable1 _ ‘OR
Order By s.FieldName Ascending _ ‘Orderby
Select c ‘Finally… Select
Programmatically Assign Data Source: (LinqToSQL)
Dim DropDownList1 As DropDownList ‘Can Assign to Dropdown, Listview, Gridview, Formview, Repeater, ect.
DropDownList.AppendDataBoundItems = True ‘True or False, Append data to Items in .net Markup. If False the list will clear first.
DropDownList.DataSource = selection ‘Assign Datasource
DropDownList.DataTextField = “FieldName1″ ‘Assign TableField to TextField for ListItems: CheckBoxList, DropDownList, Bulleted List, ect.
DropDownList.DataValueField = “FieldName2″ ‘Assign TableField to ValueField for ListItems
DropDownList.DataBind() ‘Bind Data
Dim ListView1 As ListView
ListView.DataSource = selection ‘Assign Datasource
ListView.DataBind() ‘Bind Data
The following were used for specific situations, but may be useful in the future.
.NET Enter Button Submit Fix for Listview:
Because .Net uses a single form to process an entire page often when a user presses enter to submit a form it will submit the first button click on the page. This is usually not the desired function on pages with multiple form fields and buttons. We can fix this by encapsulating the input fields in question inside of an <asp:Panel> </asp:Panel> set of tags and setting the “DefaultButton” attribute to the ID of our desired Submit Button. Here is an Example…
<asp:TextBox ID=”TextBox1″ runat=”server” Text=”TextBox1″></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Buttton1″ />
<asp:Panel id=”Panel1″ runat=”server” DefaultButton=”Button1″>
<asp:TextBox ID=”TextBox2″ runat=”server” Text=”TextBox1″></asp:TextBox>
<asp:Button ID=”Button2″ runat=”server” Text=”Button2″/>
</asp:Panel>
Without the <asp:Panel> tag and the “DefaultButton” attribute any time a user presses enter white the cursor is in TextBox2 the form will submit Button1, but with the code above it will submit Button2 as would be desired in most situations.
However if we have a Listview, Formview, or any other control with an (Edit)(Insert)ItemTemplate(s) the buttons inside cannot be used as a DefaultButton in a panel, your page will crash and throw an error refering to an IButton or something. Usually you would place the <asp:Panel> tag inside of your EditItemTemplate, but in this situation I needed the entire FormView and a few textboxs outside of the Formview. I used the folowing method to work around this issue. I created a button at the bottom of the page and used the attribute style=”display:none” to make the browser hide the button, but still render it for use as my DefaultButton. I then created a Subroutine to run for the button’s OnClick action that simply calls the FormView’s update function.
Here is the .NET psuedo Markup for the aspx page:
<asp:Panel id=”Panel1″ runat=”server” DefaultButton=”Button1″>
<!–HTML Markup–>
<!–FormView Markup–>
<!–ASP.NET Markup–>
<asp:Button ID=”Button1″ runat=”server” Text=”Text” OnClick=”SubRoutine” style=”display:none”/>
</asp:Panel>
And the Code-Behind Subroutine:
Protected Sub SubRoutine()
FormView.UpdateItem(1) ‘Boolean for Validation; 1 = yes, 0 = no
End Sub
GetFromDatabase Function:
I used the following function to pull a single database field from a table. This function is used throught my code allot to pull specific fields for display formatting. My SQL Data Sources were created onpage with <asp:SQLDataSource> tags, and then refered to through the code-behind page when calling the function.
Here is the function:
Protected Function GetFromDatabase(ByVal field As String, ByVal source As String) As String
Dim result As String = String.empty ‘Declares Return Variable
Dim SQLDataSource1 As SqlDataSource = page.FindControl(source) ‘Declare SQL DataSource
Dim dv As DataView = _
CType(SQLDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.DataView) ‘Assigns Datasource to new DataView object
If dv.Table.Rows.Count >= 1 Then ‘If Dataview has more than 0 Records
Dim dr As DataRow = dv.Table.Rows(0) ‘Select the First Row
result = dr.Item(field).ToString.Trim(” “) ‘Assign the field passed to the function, Trim Whitespace
End If
Return result ‘Return the Requested Field
End Function
Thats it for now. I’m Sure I’ll have more in the future, may even clean this up some.