Sunday, August 26, 2012

Get computer names in the Network

I wanted to get all the computer's names as I was doing a till balancing program, related to a supermarket (POS application). On a report I wanted to filter the daily balance.

Daily balance of all stations and station wise. I thought this might helpful, if I share the code snippet with you.


private List ComputersInNetwork()
{
List computerList= new List();
using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
{
foreach (DirectoryEntry computers in root.Children)
{
foreach (DirectoryEntry computer in computers.Children)
{
if ((computer.Name != "Schema"))
{
computerList.Add(computer.Name);
}
}
}
}
computerList.Add("All");
computerList.Sort();
return list;
}

Happy coding.
Monday, July 23, 2012

Illegal characters in path (XmlDocument.Load())

This occurs due to not understanding the expected parameter type for the XmlDocument.Load("filename") method. This method expect a file name.

Look at the following example, this throw the above error.

eg:
//getting a soap response
string sXml = nc.CurrencyExchangeRatesForWeb();
XmlDocument doc = new XmlDocument();
doc.Load(sXml);

To avoid this error, we can use XmlDocument.LoadXml("string") method, following example shows the solution,

eg:
string sXml = nc.CurrencyExchangeRatesForWeb();
XmlDocument doc = new XmlDocument();
doc.LoadXml(sXml);

Happy coding
Tuesday, July 17, 2012

Get File Name from path string

I have seen many young developers are struggle with getting
the only the file name from the full path.

Here is a simple code snippet to get the file name.

string path = "C:\\TEMP\\temp.bmp";
string fileName = System.IO.Path.GetFileName(path);

Happy coding.
Thursday, September 08, 2011

Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

Tuesday, February 01, 2011

Gridview RowEditing checkbox value

We know if we have manually binds the data then, we can access the checkbox or what ever the control by it's id.
as follows

CheckBox ck = (CheckBox)GridView1.Rows[e.NewEditIndex].Cells[8].FindControl("yourCheckBoxControl");



 



What if you auto bided the data to a gridview,



CheckBox ck = (CheckBox)GridView1.Rows[e.NewEditIndex].Cells[8].Controls[0];

Wednesday, January 26, 2011

DropDownList - Set selected value


The following code snippent shows how to set the selected item of a
DropdownList


 


DropDownList1.ClearSelection();
ListItem li = DropDownList1.Items.FindByValue("20");
DropDownList1.Items.FindByText(li.Text).Selected = true;


I have seen many beginners are struggling to set the selected value  dynamically,
Hope this is very helpful.

Sunday, January 16, 2011

The report you requested requires further information

This issue is a common one who starts working on Crystal Reports with Dataset (.Net)

loginerror

 

Ill show a sample code snippet where you can generate this issue.

                 ReportDocument report = new ReportDocument();    

BAgent bAgent = new BAgent();
DataSet dsStatus = new DataSet();
dsStatus = bAgent.GetStatus();

string reportPath = Server.MapPath("/Agent/Reports/GetStatusRpt.rpt");
report.Load(reportPath);

report.SetDataSource(dsStatus);// this is the line cause for this issue
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.RefreshReport();
 



Here is the solution, use the datset table instead of the dataset


 

                 ReportDocument report = new ReportDocument();    

BAgent bAgent = new BAgent();
DataSet dsStatus = new DataSet();
dsStatus = bAgent.GetStatus();

string reportPath = Server.MapPath("/Agent/Reports/GetStatusRpt.rpt");
report.Load(reportPath);
                 report.SetDataSource(dsStatus.Tables[0]); 
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.RefreshReport();

 


Hope you got rid from this issue. Enjoy coding.

Thursday, January 13, 2011

GridView Hidden Column Data Access

Again I faced an issue, as usual before .net version 2.0 I set a column's visibility property of a gridview to false.
If you are a developer by now you know why I did that. I know you guessed it. I just wanted to use that column's value
in one of my calculations, and didn't want to show it to the user.

Grid is working fine it hides the column I wanted to hide, I am happy!., Suddenly I realized that I don't get the value from
the hidden (visibility= false) column's cell value in the following method.

 

 protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //this is the column I set false visibility
}


I was looking my coding deeply I am sure this is correct 100%, then what has gone wrong?

With 2.0 and above versions on .Net when you set a column of a gridview to visibility false


it doesn't  bind the data to this hidden column. You must be really disappointed hearing this


news, even I did. Wait you don't have to worry so long.



Don't set your column's property visibility to false in the design view. If you do so it will

call before the databind. When there is no column databing is not take place and ignores this hidden column.


Obvious we  don't get data (you are lucky only you didn't get the data, worse case this might throw an error)



Trick to overcome this issue,



RowCreated event of a GridView takes place after the data binding, so we can do something like following

code snippet.



 protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[8].Visible = false;
}



This hides the column, still you can access the cell value,



 protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //I get the cell value, I am happy!
}


 


Enjoy working on Gridview control,

Tuesday, January 11, 2011

ASP.Net Gridview column totals

Most of the developers use repeater control, in case where they have to show the totals of a list  instead gridview control.
This is not that difficult to show the totals on the gridview itself on footer template. I will show the steps how to do this in this post.

 

Drag and drop a Gridview on your .aspx and bind the data.

eg: 

grd.DataSource = ds;
grd.DataBind();

on above code snippet grd is the my Gridview name and ds is my dataset. (you can use any datasource as your convenience)

Set the ShowFooter property to True of the GridView, this will show the footer on the Gridview.

Write the RowDataBound  event as follows, you can change the way you want. (in the following scenario I have not showed the declaration of chkAmount and transCharge 
decimal type variables, these two are class level varibles)

 

 

        protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
chkAmount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ChkAmount"));
transCharge += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "TransCharge"));
}
else if (e.Row.RowType == DataControlRowType.Footer) // this is the footer identification
{
e.Row.Cells[4].Text = "Totals:";
// display totals on cells (in my case I have selected last 3 columns)
e.Row.Cells[6].Text = chkAmount.ToString("N2");
e.Row.Cells[5].Text = transCharge.ToString("N2");

}
}



 



Hope this is really easier when it comes to show totals on Gridview footer area . Please refer the following image which is the output of my above coding effort.



gridview-column-totals

Monday, September 20, 2010

Rename IIS7 application (Virtual directory)

This command will rename an existing directory on IIS7,

C:> %systemroot%\system32\inetsrv\appcmd set app "Default Web Site/OldApplicationName" -path:/NewApplicationName




You just have to change OldApplicationName and NewApplicationName as per your requirement.
Tuesday, August 03, 2010

.NET Entity Data Model template missing

I may be bit late, but I thought I must give a try on a MVC Architecture Model.
So, as I do always started to write a sort of "Hello World" app. When I try to create the ADO.NET Entity Data Model, I could not find ADO.NET Entity Data Model template.
Is something wrong here? yep, we need to install Microsoft Visual Studio 2008 Service Pack 1 (iso). Here we go I have ADO.NET Entity Data Model template under my Data category.

There is more than one way...
Wednesday, July 28, 2010

Page load twice

I know you may have already frustrated of this issue. Even I was really fed up when I came a cross this weird issue. I was facing this issue when I test my web application with Firefox 3.6.6, I did not check with any previous versions (and I found no issues with IE and some other browsers). Believe me it takes a lot of time to cure this. There are many possibilities but mine was a bad CSS practice, you may be wondering how CSS reloads the whole web page.
This was the issue in my CSS code background-image:url(''), having a empty image URL.
I thought reloading the page twice an issue with my coding on page behind, but ultimately it was a CSS issue.

Let me tel me why an empty URL gives such a major issue. Once the page loads the firefox still tries to find the empty URLs so page loads twice but IE just replaces the empty URL with null value.

As I mentioned above this may not the exact cause for your problem but one of the following might cause your issue. Please visit the following URL (http://www.110mb.com/forum/how-to-stop-firefox-dual-pageloads-t27704.0.html) to see more possibilities. Hope this guides you to find the issue.
Monday, July 19, 2010

must be placed inside a form tag with runat=server

I got to explain the scenario I cameacross this issue, as this might vary for some other scenarios. I got this error when I try to override the PreRender event of the masterpage's
Content Holder (idea was to get the html of that area what evel loding in to that).


ContentPlaceHolder cph = (ContentPlaceHolder)Master.Controls[0].FindControl("master_content_holder");
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var htmlTxtWr = new HtmlTextWriter(sw);
SmtpUtil smtpUtil = new SmtpUtil();
cph.RenderControl(htmlTxtWr);

I got this error exactly on the last line of code. As error message states our all controlls must be inside a form tag with runat = server (note you can't have form tags on child pages, do not try it, it won't work). I did a small dirty work as to fix this issue.
I just overrided the VerifyRenderingInServerForm mehtod on child page. For me it worked properly, as i didn't have any verification to be done on my page. Be carefull before use.
Thursday, July 15, 2010

Drop emails in a folder


Instead sending an email we can configure the web.config to drop the mail in a local folder.
This is a good feature when it come to testing email on your application while developing.

Just add the following on the web.config

   1:     <system.net>
   2:       <mailSettings>
   3:         <smtp deliveryMethod="SpecifiedPickupDirectory">
   4:        <specifiedPickupDirectory pickupDirectoryLocation="C:\testmails\"/>
   5:         </smtp>            
   6:       </mailSettings>       
   7:      </system.net>


 
Following code will drop an email in your folder

   1: var smtpClient = new SmtpClient();
   2: var message = new MailMessage("from@from.com", "to@to.com");
   3: message.Subject = "This is the mail subject";            
   4: message.Body = "this is the mailm body";
   5: smtpClient.Send(message);         
 
 
Double click on the file in your C:\testmails\ folder it will open up on outlook
as an email.
 

Thursday, July 08, 2010

Take asp.net web site offline App_Offline.htm

Do not need to do any setting on ISS, instead just move in an HTMl file named
App_Offline.htm . This will stop responding to any web request but as the response
this file passes until you remove it from the root or rename it.

Tuesday, June 29, 2010

SQL DATE FORMAT

You might have comeacross, situations where the date doesn't show the way you want. Hope this table will help you to get sorted out this issue.






























































































DATE FORMATS
Date Format Query (current date: 12/30/2006) Show
1 select convert(varchar, getdate(), 1) 12/30/06
2 select convert(varchar, getdate(), 2) 06.12.30
3 select convert(varchar, getdate(), 3) 30/12/06
4 select convert(varchar, getdate(), 4) 30.12.06
5 select convert(varchar, getdate(), 5) 30-12-06
6 select convert(varchar, getdate(), 6) 30 Dec 06
7 select convert(varchar, getdate(), 7) Dec 30, 06
10 select convert(varchar, getdate(), 10) 12-30-06
11 select convert(varchar, getdate(), 11) 06/12/30
101 select convert(varchar, getdate(), 101) 12/30/2006
102 select convert(varchar, getdate(), 102) 2006.12.30
103 select convert(varchar, getdate(), 103) 30/12/2006
104 select convert(varchar, getdate(), 104) 30.12.2006
105 select convert(varchar, getdate(), 105) 30-12-2006
106 select convert(varchar, getdate(), 106) 30 Dec 2006
107 select convert(varchar, getdate(), 107) Dec 30, 2006
110 select convert(varchar, getdate(), 110) 12-30-2006
111 select convert(varchar, getdate(), 111) 2006/12/30
Friday, June 25, 2010

SQL Split Function

I was just trying to do a split in a stored procedure, and found this
link. This is really helpfull comparing with other resources available on the web.

http://sqltutorials.blogspot.com/2007/09/sql-function-split.html

Below is Split Function in SQL

DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='SQL,TUTORIALS'
SET @Delimiter = ','
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)
END

Result
- SQL
- TUTORIALS
Sunday, May 23, 2010

Dotnetnuke System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission

 

I got this error when I try to install Dotnetnuke 05.04.02 on my computer on Wndows 7 and IIS7. There was no proper solution for this on the web, but I just change the application pool identity to NetworkService and application worked as expected.

Steps :
Go to IIS and select Application Pools.
Select the DNN web pool on right hand pane.
On Actions press Set Application Pool Default link.
Change the Identity under Process Model to NetworkService

ApplicationPool

Wednesday, May 19, 2010

Use a Web Proxy for Cross-Domain XMLHttpRequest Calls


I had an issue when calling web service through AJAX from 3rd party URL.
Here is the solution

http://developer.yahoo.com/javascript/howto-proxy.html

Friday, May 07, 2010

Using JQuery on blogspot

As I use JQuery these days, I just though how I can use JQuery on my blog.
I tried to do this and found It’s very easy to use it with blogger. Just a few steps
make this possible. You might be wondering where you can place the JQuery file in
blogger. Google has host these file on the following path “http://ajax.googleapis.com/ajax/libs/jquery/”. Just have to select the version you prefer to use.

Just see the following steps.

Step 1:
Add the following code between header tags of your blog’s HTML.

<
head>
<
script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js' type='text/javascript'/>
</
head>

Example:
1

Step 2:
Add the functions in between header tags as follows,

<
head>
<
script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js' type='text/javascript'/>

<
script type="text/javascript">
    function
MouseOn() {
        $(document).ready(function() {
        $("img").toggleClass("newClass");         
            //$("#testPara").append("Shiran the greatest" + count );

       
});
    }
</script>
</
head>

Example :
22

 

Step 3: (Optional)
If there are any CSS involved embed them also in between header tags as follows

<
head>
<
script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js' type='text/javascript'/>

<
script type="text/javascript">
    function
ChangeCSS() {
        $(document).ready(function() {
             //here this will toggle CSS classes on an event
 
        $("img").toggleClass("newClass");       
          
       
});
    }

<style type="text/css">
.toggler {width: 100px; height: 200px;}

.newClass {width: 500px; height: 866px;}
</style>

</script>
</
head>


Need to call the above ChangeCSS() JS function where this need to be done.


Example:
this maight be in post or a HTML/JavaScript gadget

<img class="toggler" src="http://1.bp.blogspot.com/_8fX3E/S-L0RblzI/AAAADtQ/nB8kJYc/s200/108_f0.jpg" onmouseover="ChangeCSS()"/>


Give your visitors a shock.

My Achievements

Member of

Blog Archive

Followers

free counters