ASP.net Archives - Page 4 of 4 - Digital Tool Factory blog ASP.net Archives - Page 4 of 4 - Digital Tool Factory blog

The Digital Tool Factory Blog

How to fix the No connection could be made because the target machine actively refused it 127.0.0.1:25 error

The Problem: You have a brand new Windows 2008 server and you are testing your web application and trying to send an email.  Every time you try to send an email via the web application you get the following error

“System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25”

The Cause: SMTP services are not installed on the server, they do not seem to be installed by default. Continue reading →


12
Feb 10


Written By Steve French

 

How to set up the CheckBoxList control in Asp.net

I recently came up with the need to have a validated CheckBoxList in and Asp.net page.  Over the years I’ve come up with a standard way of setting up validated CheckBoxLists, so I thought I would share the method and start the brand new “Code Samples” section..

The aspx code is as follows

<strong>Section Name</strong>
<asp:TextBox ID="tbBox" runat="server" Width="1" BorderColor="White" BackColor="White" BorderStyle="None" />
<asp:CustomValidator ID="cvBox" ValidateEmptyText="true" runat="server" ControlToValidate="tbBox" Display="none" ErrorMessage="<b>Required Field Missing</b><br />You must select at least one option" SetFocusOnError="true" ValidationGroup="Main" onservervalidate="cvBox_ServerValidate" />
<ajaxToolkit:ValidatorCalloutExtender ID="vceBox" runat="server" TargetControlID="cvBox" Width="300" />

<br />

<asp:CheckBoxList ID=”cblForms” runat=”server” RepeatColumns=”2″ RepeatDirection=”Horizontal”>
<asp:ListItem Text=”Option A” Value=”A” />
<asp:ListItem Text=”Option B” Value=”B” />
<asp:ListItem Text=”Option C” Value=”C” />
<asp:ListItem Text=”Option D” Value=”D” />
</asp:CheckBoxList>

Please note, the control that I am validating is actually a 1 pixel wide textbox that one should not type in. Continue reading →


14
Jan 10


Written By Steve French

 

How to fix “The entry ‘ScriptModule’ has already been added.” error

The Problem: The web application works just fine on the local machine, but when you upload it to the web server you get the error “The entry ‘ScriptModule’ has already been added.”

The Cause: It is a conflict between asp.net versions.

The Solution: Simply comment out the <add name=”ScriptModule” type=”System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″/> line in the web.config file and you should be good to go.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


23
Dec 09


Written By Steve French

 

How to create a fully validated asp.net calendar control

The Problem: You have to have a fully validated date box. The dates must be valid dates and there must be data entered in the field.

The Cause: Not a problem really, I wind up rewriting it slightly differently each time. I thought I would put a definitive version here.

The Solution: Use this code
<asp:TextBox ID=”tbDate” runat=”server” CssClass=”body” />
<ajaxToolkit:MaskedEditExtender ID=”meeDate” runat=”server”
TargetControlID=”tbDate”
Mask=”99/99/9999″
MessageValidatorTip=”true”
OnFocusCssClass=”MaskedEditFocus”
OnInvalidCssClass=”MaskedEditError”
MaskType=”Date”
DisplayMoney=”Left”
AcceptNegative=”Left”
ErrorTooltipEnabled=”True” />
<ajaxToolkit:CalendarExtender ID=”ceDate” runat=”server” TargetControlID=”tbDate” PopupButtonID=”imgDate” />
<asp:RequiredFieldValidator ID=”rfvDate” runat=”server” ControlToValidate=”tbDate”
Display=”none” ErrorMessage=”<b>Required Field Missing</b><br />You must supply a date”
SetFocusOnError=”true” ValidationGroup=”MainB” />
<ajaxToolkit:ValidatorCalloutExtender ID=”vceDate” runat=”server” TargetControlID=”rfvDate” Width=”300″ />

<asp:CompareValidator ID=”compVDate” runat=”server” ValidationGroup=”MainB”
ControlToValidate=”tbDate” Display=”None” ErrorMessage=”<b>Required Field Missing</b><br />You must supply a valid date”
Operator=”DataTypeCheck” Type=”Date” />
<ajaxToolkit:ValidatorCalloutExtender ID=”vceCompDate” runat=”server” TargetControlID=”compVDate” Width=”300″ />
<asp:ImageButton runat=”Server” ID=”imgDate” ImageUrl=”img/icon_calendar.gif” AlternateText=”Click to show calendar” />

That’s it!

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


17
Dec 09


Written By Steve French

 

How to programmatically create an Excel Spreadsheet in ASP.net/ C#

The Problem: You need to create a dynamic excel spreadsheet in C#/ASP.net. There are several ways to accomplish this, but this is one I use when I don’t need to specify the column names order to any great detail. I’ve included it in the How To Fix series simply because it took me more than 15 minutes to find the code, and rules are rules.

The Cause: No cause really, this is a method. The cause of hte problem was being a bit disorganized today.

The Solution: Just copy and paste the following code into your C# code behind and let her rip! Utility.dsGrab is just a function that returns a dataset.

DataTable dt = new DataTable();DataSet ds = Utility.dsGrab("SampleDataSet");dt = ds.Tables[0];GridView gv = new GridView();

if (dt.Rows.Count > 0){gv.DataSource = dt;gv.DataBind();System.IO.StringWriter oStringWriter =     new System.IO.StringWriter();System.Web.UI.HtmlTextWriter oHtmlTextWriter =     new HtmlTextWriter(oStringWriter);Response.ClearContent();Response.ContentType = "application/ms-excel";Response.AddHeader("Content-Disposition", "attachment;     filename=SampleExcel.xls");Response.Charset = "";Response.Buffer = true;EnableViewState = false;gv.RenderControl(oHtmlTextWriter);Response.Write(oStringWriter.ToString());Response.End();}

That’s it! It won’t open up in the browser window either.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


09
Dec 09


Written By Steve French

 

How to fix dropdown boxes in asp.net datagrids

OK, this problem isn’t silverlight, but I did encounter it in my day job. In fact, I’ve encountered it several times, so I thought I would write it up so I can find it again.

The Problem: You write a web page that contains an updatable datagrid, which, when in EditMode, contains a dropdown list containing lookup items. The DropdownBox does not populate, and (later when you get it working) the proper listitem is not selected.

The Cause: Since the DropDownBox down not existing when the page loads, there is nothing to bind.

The Solution: You have to attach your events to some non-standard page events to get it to work. The relevant part of the .aspx page is below (put this in your datagrid)

<asp:TemplateColumn HeaderText=”Tactic Category”>
<HeaderStyle Font-Size=”Large” Font-Bold=”true” />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,”TacticCatName”)%>
</ItemTemplate>
<EditItemTemplate>
<%–Notice the GetCat() routine here as the datasource.–%>
<asp:DropDownList id=”ddTacticCatList” runat=”server” DataSource=”<%# GetCat() %>” DataTextField=”TacticCatName” DataValueField=”TacticCatID”>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Your code-behind page should look like this (in C#)
private void MainCode()
{
string strID = Request.QueryString[“id”].ToString();
//custom code to get the relevant dataset for the entire datgrid
DataSet ds = LMR.TacticSubCatList(strID);
//custom code to bind to the datagrid
Utility.DGDSNullCheck(ds, dgTacticSubCatList, lblTacticSubCatList, “There are no tactic subcategories in the database at this time.”);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MainCode();
}
}

protected void dgTacticSubCatList_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//grab the index
dgTacticSubCatList.EditItemIndex = e.Item.ItemIndex;
MainCode();
}

protected void dgTacticSubCatList_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//get the id value of the selected datagrid item
int intTacticSubCatID = (int)dgTacticSubCatList.DataKeys[(int)e.Item.ItemIndex];

//this gets our textbox, also in the datagrid
TextBox EditText = null;
EditText=(TextBox)e.Item.FindControl(“tbTacticSubCatName”);
string strEditText = Convert.ToString(EditText.Text);

//get the value of our dropdown list
DropDownList dd = (DropDownList)e.Item.FindControl(“ddTacticCatList”);
string strTacticCatID = dd.SelectedValue.ToString();
//do our database update
LMR.TacticSubCatEdit(intTacticSubCatID.ToString(),strTacticCatID,strEditText);
//reset the datagrid
dgTacticSubCatList.EditItemIndex = -1;
//give feedback and rebind
lblFeedback.Text = “Your changes have been applied.”;
MainCode();
}

protected void dgTacticSubCatList_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgTacticSubCatList.EditItemIndex = -1;
MainCode();
}
protected DataTable GetCat()
{
//here is where we get the dataset to populate the dropdown list – it does have to go in an independant function
DataSet ds = LMR.TacticCatList();
return ds.Tables[0];
}

protected void dgTacticSubCatList_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
//we set the selcted index in the ItemDataBound event
string strID = Request.QueryString[“id”].ToString();
string strSubCatID = dgTacticSubCatList.DataKeys[(int)e.Item.ItemIndex].ToString();
Holder.TacticSubCat tsc = LMR.GetTacticSubCat(strSubCatID);
DropDownList dd = (DropDownList)e.Item.FindControl(“ddTacticCatList”);
dd.SelectedIndex =dd.Items.IndexOf(dd.Items.FindByValue(strID));
}
}

That’s all there is to it. The tricky parts are putting the initial dropdown population in a separate function, and setting the index in the ItemDataBound event.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


31
Oct 09


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.