Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Monday, June 23, 2008

Opening, Reading, and Writing a file

Here is an example of Opening/Reading a file in btnOpen_Click and Writing in btnSave_Click:

protected void btnOpen_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text="c:/temp/temp.txt";
StreamReader sr = null;
try
{
sr = new StreamReader(txtFileName.Text);
txtFileContent.Text = sr.ReadToEnd();
sr.Close();
displayMessage("Successfully opened file");
}
catch (Exception ex)
{
displayMessage(ex.Message);
}
finally
{
sr.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text = "c:/temp/temp.txt";
StreamWriter sw = null;
try{
sw = new StreamWriter(txtFileName.Text);
string[] ar;
ar = txtFileContent.Text.Split(Environment.NewLine.ToCharArray());
foreach (string sIn in ar)
{
if(!string.IsNullOrEmpty(sIn))
sw.Write(sIn.Trim()+Environment.NewLine);
}
}catch (Exception ex)
{
displayMessageLine(ex.Message);
}
finally{
sw.Close();
}
}

Saturday, June 21, 2008

Using a nullable boolean.

It is possible to assign a nullable boolean in C#. The trick is to use ? in the declaration. Once declared, one can use the HasValue() method. An example follows:

protected void Page_Load(object sender, EventArgs e)
{
bool? isUser = null;
lblStatus.Text = isUser.HasValue.ToString(); //displays false
// an int example: Nullable<int> i = null;
}
protected void btnCheckStatus_Click(object sender, EventArgs e)
{
bool? isUser = false;
lblStatus.Text = isUser.HasValue.ToString(); //displays true
}

Thursday, June 5, 2008

Clearing controls in GridView row using delete

Here is how to clear controls in GridView row using delete. Be careful that GridViewDeleteEventArgs is not GridViewDeletedEventArgs!

<asp:GridView ID="gvStolenStatusInfo" runat="server" AutoGenerateColumns="false" AllowPaging="true"
EmptyDataText="No records found." PageSize="10" CssClass="gridView"
OnRowDeleting="gvStolenStatusInfo_RowDeleting" AutoGenerateDeleteButton="true">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:TextBox ID="txtuCode" runat="server" Text='<%# Bind("Code") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:TextBox ID="txtuComment" runat="server" Text='<%# Bind("Comment") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Notice the OnRowDeleting set to x_RowDeleting function and AutoGenerateDeleteButton. Now for the delete function:

protected void gvStolenStatusInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowID = e.RowIndex;
((TextBox)gvStolenStatusInfo.Rows[rowID].FindControl("txtuCode")).Text = string.Empty;
((TextBox)gvStolenStatusInfo.Rows[rowID].FindControl("txtuDate")).Text = string.Empty;
}
References: http://www.codeproject.com/KB/webforms/Editable_GridView.aspx