I have found it handy to drag the split bar down to get two panes of the same file.
Monday, January 20, 2020
Viewing Different Locations in the Same Visual Studio File
I frequently want to view two parts of a file at the same time. Like if I am copying or moving code from one place to another in the same file.
I have found it handy to drag the split bar down to get two panes of the same file.
Sometimes I want to view more than two places in a file at once or the two blocks of code I am looking at are really long, so I would like to see them side by side. For that, I select from the menu Window => New Window.
I have found it handy to drag the split bar down to get two panes of the same file.
Wednesday, September 26, 2018
CallerMemberName (C# 5.0 and above)
CallerMemberName (C# 5.0 and above)
propertyName));
}
When the CallerMemberName attribute is applied to an argument, the calling method/property/etc name will be supplied. This is useful for snippets, and for implementing non-volatile sections of code without needing to fill-in specifics.
Example:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([ CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Thursday, April 14, 2016
Error When Using Distinct - ORA-01791: not a SELECTed expression
The Problem
I had a query that was working, but when I added a DISTINCT to it started throwing an error. The error I was receiving was ORA-01791: not a SELECTed expression. It didn't give me which field it didn't like, so I was confused.
The Solution
What I found was that once DISTINCT was in play all the fields in my ORDER BY had to be in the SELECT. All of the ORDER BY fields had to be fields and not calculations.
I had a query that was working, but when I added a DISTINCT to it started throwing an error. The error I was receiving was ORA-01791: not a SELECTed expression. It didn't give me which field it didn't like, so I was confused.
The Solution
What I found was that once DISTINCT was in play all the fields in my ORDER BY had to be in the SELECT. All of the ORDER BY fields had to be fields and not calculations.
Friday, March 18, 2016
How to insert an ampersand into an Oracle field
I was trying to insert a value into a varchar2 field. The value contained an ampersand. Something like "A&B Home Remodeling". It kept giving me an error like &B was a variable.
What worked for me was storing it like this:
'A'||'&'||'B'
It like that much better. I also saw suggestions to chr(38). I did not try that, so I am not sure if it works, but I am guessing it would be very similar to what I did. Something like:
'A'||chr(38)||'B'
Wednesday, December 16, 2015
Updating a Session Variable With JavaScript
Although JavaScript is client side code with an AJAX call you can set or update a session variable on the server when client side code fires.
In my case I couldn't do a post back, since I could not have a form on the page, so I needed to do a reload. I still needed the values from the page, which reload does not preserve. Because it is a reload it shouldn't preserve those values, but for my purpose I needed one of them.
What I ended up doing was using JQuery to make an AJAX call to a handler page.
Once the AJAX call returns then I call the reload. The code looks similar to this:
$.ajax(
{
cache: false,
url: sURL,
datatype: "html"
}).done(function (data) {
if (data != "") alert(data);
//alert("Should reload");
window.location.href = window.location;
window.location.reload(true);
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert("fail");
alert(thrownError);
});
sURL is the address of the handler page. You will need to pass a parameter to the handler page to tell it that you want to call the routine in the handler that sets the session variable. The handler code in ASP.NET with C# would look like this:
static HttpContext _context;
public void ProcessRequest(HttpContext context)
{
string retval = "";
_context = context;
switch (context.Request["method"])
{
case "SetSession":
Session[context.Request["SessionVarName"])] = context.Request["SessionValue"])
break;
default:
context.Response.ContentType = "text/plain";
retval = "Invalid method specified";
break;
}
context.Response.Write(retval);
}
In my case I couldn't do a post back, since I could not have a form on the page, so I needed to do a reload. I still needed the values from the page, which reload does not preserve. Because it is a reload it shouldn't preserve those values, but for my purpose I needed one of them.
What I ended up doing was using JQuery to make an AJAX call to a handler page.
Once the AJAX call returns then I call the reload. The code looks similar to this:
$.ajax(
{
cache: false,
url: sURL,
datatype: "html"
}).done(function (data) {
if (data != "") alert(data);
//alert("Should reload");
window.location.href = window.location;
window.location.reload(true);
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert("fail");
alert(thrownError);
});
sURL is the address of the handler page. You will need to pass a parameter to the handler page to tell it that you want to call the routine in the handler that sets the session variable. The handler code in ASP.NET with C# would look like this:
static HttpContext _context;
public void ProcessRequest(HttpContext context)
{
string retval = "";
_context = context;
switch (context.Request["method"])
{
case "SetSession":
Session[context.Request["SessionVarName"])] = context.Request["SessionValue"])
break;
default:
context.Response.ContentType = "text/plain";
retval = "Invalid method specified";
break;
}
context.Response.Write(retval);
}
Tuesday, November 10, 2015
Oracle Scheduled Jobs
TO CREATE A JOB:
Begin
Dbms_scheduler.Create_job (
Job_name => 'RUN_JOB_TO_RUN',
Job_type => 'PLSQL_BLOCK',
Job_action => 'BEGIN JOB_TO_RUN(); END;',
Start_date => To_timestamp_tz('10-NOV-2015 13:00:00','DD-MM-YYYY HH24:MI:SS'),
Repeat_interval => 'FREQ = DAILY; BYHOUR=13',
comments => 'Some job to run.',
Enabled => True);
End;
TO UPDATE A JOB:
begin
Dbms_scheduler.Set_attribute
( name => 'RUN_JOB_TO_RUN'
, Attribute =>'REPEAT_INTERVAL'
, value =>'FREQ=DAILY; BYHOUR=2');
End;
Thursday, September 24, 2015
Format Strings to Use a Percent Sign (%) Without the Formatter Multiplying the Value by 100
I had an application that needed to display a value with a percent sign (%) after it. The value had already been converted from decimal to integer, or in other words had already been multiplied by 100. I did not have the option to just append a % at the end of the string. All the object gave me access to was the format. I tried "#0%", which gave me what I wanted, except that it multiplied the value by 100, so 20% became 2000%.
After a little research I found that using an escape character gave me what I wanted.
ToString("#0\\%")
After a little research I found that using an escape character gave me what I wanted.
ToString("#0\\%")
Wednesday, September 9, 2015
C# Equivalent of DoEvents
I have been looking for a long time for an equivalent to Visual Basic's DoEvents statement.
DoEvents allows your code to yield execution to other threads in order to share processing time. This could be do in a loop to allow you to process an iteration of the loop and when done yield. This is often done to attempt to keep the UI responsive. After each iteration you would call DoEvents thus allowing the UI some processing time.
I was studying today about multi threading and asynchronous processing when I came across some code that did a loop and called Thread.Sleep(0) at the end of each iteration. Supposedly this yields execution without causing the loop to execute longer, since the sleep is for zero milliseconds. Since the thread goes to sleep, other threads can then take over and release control when it is done.
I think you can use DoEvents in C# by using the the Visual Basic library, but from what I have seen people saying that is not recommended. This is why I have been looking for a native C# method of doing this.
Visual Basic
This is useful if DoFileStuff() updates the UI. DoEvents() would then let go of control, so the UI could take a turn and refresh itself.
DoEvents allows your code to yield execution to other threads in order to share processing time. This could be do in a loop to allow you to process an iteration of the loop and when done yield. This is often done to attempt to keep the UI responsive. After each iteration you would call DoEvents thus allowing the UI some processing time.
I was studying today about multi threading and asynchronous processing when I came across some code that did a loop and called Thread.Sleep(0) at the end of each iteration. Supposedly this yields execution without causing the loop to execute longer, since the sleep is for zero milliseconds. Since the thread goes to sleep, other threads can then take over and release control when it is done.
I think you can use DoEvents in C# by using the the Visual Basic library, but from what I have seen people saying that is not recommended. This is why I have been looking for a native C# method of doing this.
Visual Basic
For Each file In files DoFileStuff()
Application.DoEvents()
Next
This is useful if DoFileStuff() updates the UI. DoEvents() would then let go of control, so the UI could take a turn and refresh itself.
C#
foreach (var file in files) {The thread goes to sleep and immediately wakes up ready to be scheduled for execution when it is its turn again.DoFileStuff();Thread.Sleep(0);}
Saturday, January 24, 2015
HttpContext.Current Null in Asynchronous Methods
I created a page that had some logic to dynamically create the page. It was doing a lot of fetches from the database and then calculating how to display each batch of data that was retrieved. The page became slow.
To speed it up I converted the most time intensive loop to a parallel for each. Inside the loop it was using HttpContext.Current. Once I made the loop Parallel HttpContext.Current was null inside things called inside the loop..
It appears that if you leave the main thread of execution it no longer has a reference to the current context.
I found that HttpContext.Current can be set as well as read. Outside the parallel loop I captured HttpContext.Current in a local variable. I then accessed the variable in the loop. The other things I was calling inside the parallel for each needed HttpContext.Current, so I set HttpContext.Current to the local variable. The other methods were able to read HttpContext.Current as long as they were on the same thread as the calling method.
To speed it up I converted the most time intensive loop to a parallel for each. Inside the loop it was using HttpContext.Current. Once I made the loop Parallel HttpContext.Current was null inside things called inside the loop..
It appears that if you leave the main thread of execution it no longer has a reference to the current context.
I found that HttpContext.Current can be set as well as read. Outside the parallel loop I captured HttpContext.Current in a local variable. I then accessed the variable in the loop. The other things I was calling inside the parallel for each needed HttpContext.Current, so I set HttpContext.Current to the local variable. The other methods were able to read HttpContext.Current as long as they were on the same thread as the calling method.
Tuesday, November 25, 2014
Making a DIV Dynamically Adjust to the Size of the Content and Not Fill the Entire Parent
I had created an HTML table with three cells (td). The first cell was 20% of the whole the second 60% and the third 20%. In the second cell I place a div. I needed the div to not fill the entire cell, but stretch or shrink based on how much content is in it. The following code is what I started with. The div would fill the cell, even if empty.
table>
tr>
td style="width: 20%">
/td>
td style="width: 60%;text-align:center;">
div id="divMiddleCell">
Dynamic Content
/div>
/td>
/tr>
/table>
I altered it as follows to add display: inline-block tot he style property of the div and it made the div dynamically shrink and grow.
table>
tr>
td style="width: 20%">
/td>
td style="width: 60%;text-align:center;display: inline-block">
div id="divMiddleCell" ">
Dynamic Content
/div>
/td>
/tr>
/table>
table>
tr>
td style="width: 20%">
/td>
td style="width: 60%;text-align:center;">
div id="divMiddleCell">
Dynamic Content
/div>
/td>
/tr>
/table>
I altered it as follows to add display: inline-block tot he style property of the div and it made the div dynamically shrink and grow.
table>
tr>
td style="width: 20%">
/td>
td style="width: 60%;text-align:center;display: inline-block">
div id="divMiddleCell" ">
Dynamic Content
/div>
/td>
/tr>
/table>
Thursday, March 13, 2014
How to do Select Top in Oracle
In SQL Server I often do a SELECT TOP 10 FROM [TableName].
To get the bottom ten I found that I could ORDER BY DESC.
In Oracle things are a bit different. In Oracle they have a ROWNUM system field. To get the first ten records you just have to get the ROWNUMs that are 10 and below like the following:
SELECT * FROM [TableName] WHERE ROWNUM >= 11
To get the bottom ten I found that I could ORDER BY DESC.
In Oracle things are a bit different. In Oracle they have a ROWNUM system field. To get the first ten records you just have to get the ROWNUMs that are 10 and below like the following:
SELECT * FROM [TableName] WHERE ROWNUM >= 11
Control 'gv' of type 'GridView' must be placed inside a form tag with runat=server.
I have gotten this error several times:
Control 'gv' of type 'GridView' must be placed inside a form tag with runat=server.
Each time I forget the solution and after I fix it I eventually forget why I was getting it. This time is not different. I can't remember why it started happening, but the solution is to create a method like this.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Sometimes & nbsp; Doesn't Work and I Have to Use & #160; Instead
I found recently that sometimes when I use an HTML & nbsp; it literally prints & nbsp; instead of a nonbreaking space.
Blogger is having trouble taking what I want to say, so I have to put a space between the ampersand and the nbsp; characters to get it to print correctly.
One instance that gave me trouble with the nbsp was between a td tag, even though I can include other HTML markup in between the opening and closing tag.
After some head scratching and reading I tried using a & #160; and that worked.
I guess HTML has two different ways of expressing non breaking spaces.
Blogger is having trouble taking what I want to say, so I have to put a space between the ampersand and the nbsp; characters to get it to print correctly.
One instance that gave me trouble with the nbsp was between a td tag, even though I can include other HTML markup in between the opening and closing tag.
After some head scratching and reading I tried using a & #160; and that worked.
I guess HTML has two different ways of expressing non breaking spaces.
Thursday, February 6, 2014
ASP.Net Web Forms Application Can't Find Namespace
I was creating a test Web Forms application and added some existing code files.
I first added them to the app_code folder, but in there they could not find the OracleDataClient namespace. I found something that said to move the code file out of app_code and that fixed the Oracle problem.
Then when I would reference classes in those files the page code did not recognize the namespace.
After a while of searching I found someone who said to right click on the files and view properties.
The property they said to look at was Build Action. My file was set to Content and I saw to change it to Compile. That fixed it.
I first added them to the app_code folder, but in there they could not find the OracleDataClient namespace. I found something that said to move the code file out of app_code and that fixed the Oracle problem.
Then when I would reference classes in those files the page code did not recognize the namespace.
After a while of searching I found someone who said to right click on the files and view properties.
The property they said to look at was Build Action. My file was set to Content and I saw to change it to Compile. That fixed it.
Wednesday, January 15, 2014
DataReader Problems Boxing and Unboxing
After I researched the problems and implemented the solutions in this post http://brookssoftwaredev.blogspot.com/2014/01/net-datareader-not-closing-connection.html I found that I was still getting timeouts while trying to obtain a connection from the pool.
I researched more and found that doing normal conversion like
int.Parse(reader["FieldName"]);
caused boxing and unboxing of the values. I was doing this on several values for many records.
I did some research and found that the boxing and unboxing causes the garbage collector to bog down. I found that the DataReader has special methods to avoid the boxing and unboxing. These methods are things like GetChar, GetDouble, GetDateTime, and GetInt32. From what I read these stop the boxing and unboxing and prevent the garbage collector from thrashing.
Once I implemented those methods my timeouts on the connection pool went away. It appears as though the garbage collector couldn't clean up the connections, since it was busy with all of the rogue objected from the DataReader boxing and unboxing.
I realized that using a DataReader left the connection open and there needed to be precautions taken, but there are several other things that you must deal with when using a DataReader, so use caution.
I researched more and found that doing normal conversion like
int.Parse(reader["FieldName"]);
caused boxing and unboxing of the values. I was doing this on several values for many records.
I did some research and found that the boxing and unboxing causes the garbage collector to bog down. I found that the DataReader has special methods to avoid the boxing and unboxing. These methods are things like GetChar, GetDouble, GetDateTime, and GetInt32. From what I read these stop the boxing and unboxing and prevent the garbage collector from thrashing.
Once I implemented those methods my timeouts on the connection pool went away. It appears as though the garbage collector couldn't clean up the connections, since it was busy with all of the rogue objected from the DataReader boxing and unboxing.
I realized that using a DataReader left the connection open and there needed to be precautions taken, but there are several other things that you must deal with when using a DataReader, so use caution.
Saturday, January 11, 2014
.Net DataReader Not Closing Connection After Closed
As I research using the .Net DataReader I kept seeing information online that said things like "When the command is executed, the associated Connection object is closed when the associated DataReader object is closed."
There had been concerns that the data reader would tie up the connection too long. There was also concern that we were closing the connection explicitly, especially since the creation of the reader was abstracted into another class. The class using the reader would not have access to the command or connection that created it.
In various different places I have worked we have used this and were confident that the DataReader would clean itself and its connection up, based upon what we had read.
We did a small test page recently and it turns out that by default it leaves the connection open.
We opened a connection and created a command tied to that connection and then used that command to create a DataReader. We did command.ExecuteReader() to create the reader.
As we researched further we found that ExecuteReader has an optional parameter of a CommandBehavior enumeration. If you pass CommandBehavior.CloseConnection then it closes the connection when the reader closes.
There had been concerns that the data reader would tie up the connection too long. There was also concern that we were closing the connection explicitly, especially since the creation of the reader was abstracted into another class. The class using the reader would not have access to the command or connection that created it.
In various different places I have worked we have used this and were confident that the DataReader would clean itself and its connection up, based upon what we had read.
We did a small test page recently and it turns out that by default it leaves the connection open.
We opened a connection and created a command tied to that connection and then used that command to create a DataReader. We did command.ExecuteReader() to create the reader.
As we researched further we found that ExecuteReader has an optional parameter of a CommandBehavior enumeration. If you pass CommandBehavior.CloseConnection then it closes the connection when the reader closes.
Thursday, September 26, 2013
Dynamically Created Control Not Firing Events in ASP.Net
I have an aspx page where when someone changes the value in a Drop Down List the database is queried and records are displayed in an HTML table. As the table is created I add a Button control to each row. I have tried this with a CheckBox as well with similar results. I also wire up a click event for each button when it is created. All the events for the Buttons point to the same event.
The grid is created properly, with the button on each line. When I click one of the buttons the Page_Load event fires, but the click event does not.
When I dynamically create a button outside the grid from the Drop Down List's SelectedIndexChanged event it also does not fire the click event.
When I move the button creation code to the Page_Load event wrapped in an If (!IsPostback) block the Click event works properly.
It appears that if the control is not created in Form_Load or Form_Init the dynamic control's events do not fire. I have not found a solution to be able to do what I want, but I thought what I found in my research so far might be useful to someone.
The grid is created properly, with the button on each line. When I click one of the buttons the Page_Load event fires, but the click event does not.
When I dynamically create a button outside the grid from the Drop Down List's SelectedIndexChanged event it also does not fire the click event.
When I move the button creation code to the Page_Load event wrapped in an If (!IsPostback) block the Click event works properly.
It appears that if the control is not created in Form_Load or Form_Init the dynamic control's events do not fire. I have not found a solution to be able to do what I want, but I thought what I found in my research so far might be useful to someone.
Monday, August 5, 2013
How to Get SQL Report Services Report Built With Report Builder to Display No Page Breaks on Screen, But Still Break When Printed
I created a report with Report Builder 3. My users are us to seeing reports displayed on screen with no page breaks. It looks like the default is to have page breaks. I found that in the report properties there are a couple of properties to manage page breaks.
In Report Builder, right click outside the report body and click Report Properties. In the properties window there is a page section when you have it sorted by property category. In that section there is a property called InterActiveSize. If you set the height and the width of this property both to be 0 the report is displayed on screen in one long page with no breaks.
The other property is PageSize. This has to do with the printed page. It is right below the margin property where you can control margins. I think this only controls them for the printed page.
In Report Builder, right click outside the report body and click Report Properties. In the properties window there is a page section when you have it sorted by property category. In that section there is a property called InterActiveSize. If you set the height and the width of this property both to be 0 the report is displayed on screen in one long page with no breaks.
The other property is PageSize. This has to do with the printed page. It is right below the margin property where you can control margins. I think this only controls them for the printed page.
Friday, August 2, 2013
Data Bound GridView and Asp.Net Ajax UpdatePanel
I tried loading a couple of grids through an update panel with a timer to try to get the rest of the page to display to the user quickly and do the heavy lifting of these two grids in the background.
I wrapped the two grids in an update panel and added a timer. I put code in the tick event of the timer. I loaded the page and it came up quickly and moments later the grids filled in. Moments after that all of my dynamic data i have created with template fields was erased. The main data that was being bound was still there.
As I researched it I found that the RowCreated event was firing repeatedly. I viewed the call stack and saw that it was being fired by system code that I could not see and not by my code.
I tried using session variables to get it to routed around the multiple calls, but the damage was being done before it got to the RowCreated event. The event also was getting null values for ID fields I was using to populate the grid correctly, which is why the dynamic data was not being displayed.
I did a couple of hours of research, but found nothing specific to my problem, but I did start stumbling upon two properties of the update panel, ChildrenAsTriggers and UpdateMode. Honestly I don't entirely understand UpdateMode yet, but ChildrenAsTriggers got me thinking that maybe the grid was triggering a refresh of its own somehow after I got everything set the way I wanted it.
I set ChildrenAsTriggers="false". It required that I have UpdateMode="Conditional". I think this also required that I have an explicit Trigger tag for the update panel that was set to the Tick event of my timer.
After all that, it appears to be working.
I wrapped the two grids in an update panel and added a timer. I put code in the tick event of the timer. I loaded the page and it came up quickly and moments later the grids filled in. Moments after that all of my dynamic data i have created with template fields was erased. The main data that was being bound was still there.
As I researched it I found that the RowCreated event was firing repeatedly. I viewed the call stack and saw that it was being fired by system code that I could not see and not by my code.
I tried using session variables to get it to routed around the multiple calls, but the damage was being done before it got to the RowCreated event. The event also was getting null values for ID fields I was using to populate the grid correctly, which is why the dynamic data was not being displayed.
I did a couple of hours of research, but found nothing specific to my problem, but I did start stumbling upon two properties of the update panel, ChildrenAsTriggers and UpdateMode. Honestly I don't entirely understand UpdateMode yet, but ChildrenAsTriggers got me thinking that maybe the grid was triggering a refresh of its own somehow after I got everything set the way I wanted it.
I set ChildrenAsTriggers="false". It required that I have UpdateMode="Conditional". I think this also required that I have an explicit Trigger tag for the update panel that was set to the Tick event of my timer.
After all that, it appears to be working.
Friday, June 28, 2013
IIS Routing and Paths With 404 in Them
We had a problem today with a path routing. We had a path similar to this http://localhost:54612/mydir/invoice/40414
It appeared that this path was never getting to the code, but being bounced back by IIS. The URL http://localhost:54612/mydir/40399 worked just fine.
After further research we found that it was number at the end with 404 in it that was breaking us. http://localhost:54612/mydir/30404 also broke.
The code we were using was
RouteTable.Routes.Add(new RegexRoute(@"^/mydir/invoice/(?\d+)/?$", new PathRouteHandler("~/myotherdir/file.aspx")));
Once we changed it to
RouteTable.Routes.Add(new Route(@"mydir/invoice", new PathRouteHandler("~/myotherdir/file.aspx")));
it worked.
We had to change the catching code to check the query string instead of the route path, but it worked.
We have yet to discover why it was behaving badly with invoice numbers containing 404. I would love to read your thoughts if you have run across such a thing.
It appeared that this path was never getting to the code, but being bounced back by IIS. The URL http://localhost:54612/mydir/40399 worked just fine.
After further research we found that it was number at the end with 404 in it that was breaking us. http://localhost:54612/mydir/30404 also broke.
The code we were using was
RouteTable.Routes.Add(new RegexRoute(@"^/mydir/invoice/(?
Once we changed it to
RouteTable.Routes.Add(new Route(@"mydir/invoice", new PathRouteHandler("~/myotherdir/file.aspx")));
it worked.
We had to change the catching code to check the query string instead of the route path, but it worked.
We have yet to discover why it was behaving badly with invoice numbers containing 404. I would love to read your thoughts if you have run across such a thing.
Subscribe to:
Posts (Atom)

