This is a problem I am faced with a lot and below is the easiest solution I have found.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { btnBack.Attributes.Add("onclick", "javascript:history.go(-1); return false;"); } }
This is a problem I am faced with a lot and below is the easiest solution I have found.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { btnBack.Attributes.Add("onclick", "javascript:history.go(-1); return false;"); } }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string output = e.Row.Cells[4].Text; //do something and return the value to the cell e.Row.Cells[2].Text = output; } }
Format currency with no decimal places
<asp:BoundField DataField="priceAmount" HeaderText="Price" DataFormatString="{0:$###,##0}"/>
Format currency with decimal places
<asp:BoundField DataField="priceAmount" HeaderText="Price" DataFormatString="{0:C}"/>
ASPX Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" > <Columns> <asp:BoundField DataField="pid" HeaderText="ID" ItemStyle-CssClass="generic_gridview_hidecolumn" HeaderStyle-CssClass="generic_gridview_hidecolumn"/> <asp:BoundField DataField="agentRef" HeaderText=" Agent Ref" /> <asp:BoundField DataField="Address1" HeaderText="Address" /> <asp:BoundField DataField="suburb" HeaderText="Suburb" /> <asp:BoundField DataField="priceAmount" HeaderText="Price" /> <asp:CommandField ButtonType="Link" SelectText="Edit" ShowSelectButton="True" /> </Columns> </asp:GridView>
Code Behind
protected void BindGrid() { int iRecsFound = 0; SqlConnection conn; DataSet ds = new DataSet(); SqlDataAdapter adapter; //read the connection string from web.config string connectionString = ConfigurationManager.ConnectionStrings[ "DefaultDatabase"].ConnectionString; //Initialize connection conn = new SqlConnection(connectionString); //Generate the SQL StringBuilder SQL = new StringBuilder(); SQL.Append(" SELECT pid, agentRef, address1, suburb, priceAmount"); SQL.Append(" FROM property"); SQL.Append(" ORDER BY agentRef"); //Create the adapter adapter = new SqlDataAdapter(SQL.ToString(), conn); //lets see if this baby works? try { //fill the dataset adapter.Fill(ds, "property"); //bind the grid to the dataset GridView1.PageSize = 8; GridView1.DataSource = ds; GridView1.DataBind(); iRecsFound = ds.Tables[0].Rows.Count; } catch (Exception ex) { lblError.Text = ex.Message.ToString(); } finally { conn.Close(); } }
To get the current date in a SQL query use the GETDATE() function.
SELECT * FROM homeopens
WHERE opendate > GETDATE()
A FULL OUTER JOIN simply returns ALL records on both sides. The example below would return all Customers and all Transactions regardless of any match between them.
SELECT Customers.Name, Transaction.Amount, Transaction.Date
FROM Customers FULL OUTER JOIN Transaction
ON Customer.CustomerID = Transaction.CustomerID;
A RIGHT OUTER JOIN retrieves data that is common in both tables AND any records from table two (the one on the right hand side of the join) that doesn’t have a corresponding record in the left table.
The example below will return results for Transactions even if no Customer record is associated to the Transaction. (Not a great example I know, cos when would you ever have a transaction with out a customer? Unlikely I know, but you see the principle, right)
SELECT Customers.Name, Transaction.Amount, Transaction.Date
FROM Customers RIGHT OUTER JOIN Transaction
ON Customer.CustomerID = Transaction.CustomerID;
A LEFT OUTER JOIN retrieves data that is common in both tables AND any records from table one (the one on the left hand side of the join) that doesn’t have a corresponding record in the right table.
The example below will return results for customers even if no transaction is associated to the customer.
SELECT Customers.Name, Transaction.Amount, Transaction.Date
FROM Customers LEFT OUTER JOIN Transaction
ON Customer.CustomerID = Transaction.CustomerID;
An INNER JOIN retrieves data that is common in both tables. I.e. In this case the join is on the DepartmentID field and results will be returned where that field is matched in both tables.
SELECT Employees.Name
FROM Departments
INNER JOIN Employees ON Departments.DepartmentID =
Employees.DepartmentID
WHERE Departments.Department LIKE ‘%Personnel’
protected string HomeOpens() { string rtn = ""; //Declare the opjects SqlConnection conn; SqlCommand comm; SqlDataReader r; //read the connection string from web.config string connectionString = ConfigurationManager.ConnectionStrings[ "DefaultDatabase"].ConnectionString; //Initialize connection conn = new SqlConnection(connectionString); //create the command StringBuilder SQL = new StringBuilder(); SQL.Append(" SELECT homeopens.opendate, homeopens.startTime, homeopens.endTime, homeopens.pid, "); SQL.Append(" property.address1, property.address2, property.suburb, property.postcode, "); SQL.Append(" propertyImages.imageFileName "); SQL.Append(" FROM property"); SQL.Append(" INNER JOIN homeopens ON homeopens.pid = property.pid"); SQL.Append(" INNER JOIN propertyImages on propertyImages.pid = property.pid"); SQL.Append(" WHERE propertyImages.imageNo = 1"); SQL.Append(" AND homeopens.opendate > GETDATE()"); SQL.Append(" ORDER BY homeopens.opendate, homeopens.startTime"); comm = new SqlCommand(SQL.ToString(), conn); string openDate = "<ul style='color: red;'>"; try { //open the connection conn.Open(); r = comm.ExecuteReader(); while (r.Read()) { openDate = openDate + "<li>" + r.GetDateTime(0).ToString("dddd, d MMMM yyyy") + " from " + r.GetString(1) + " to " + r.GetString(2) + "</li>"; } openDate = openDate + "</ul>"; r.Close(); rtn = openDate; } catch (Exception ex) { lblHomeopens.Text = ex.Message.ToString(); PanelHomeopens.Visible = true; } finally { conn.Close(); } return rtn; }