Statistics
Visits: 105081
Page Visits: 139147
Active visitors (monthly): 5364
Feed Count: 45
Most visited page: Adding value as a technical analyst
Archive : Sep 2009

Camel-Casing TextBox Text

In a textbox, the text needs to be displayed as Camel-cased

Camel-case is where the first letter of every word is capitalized. Eg, How Are You? This can be done using the text-transform style as follows:


<asp:TextBox runat="server" style="text-transform: Capitalize;"/>

Permalink | 1 Comment | Leave your comment
 
Commented by Madhanlal JM at 10-Aug-2010 09:14 AM
Nice tweaking..

Handling OnDblClick Event of GridViewRow

I have a GridView that displays a list of names. On double-clicking a name, the name needs to be set in a textbox. This needs to happen w/o any postback

The RowCreated event of GridView should add a javascript event handler for the OnDblClick event as follows: ASPX Page:


<asp:TextBox ID="txtSearch" runat="server"/>
<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false"
	 OnRowCreated="gvMain_RowCreated">
	<Columns>
	    <asp:BoundField HeaderText="Name" DataField="Name" />
        </Columns>
</asp:GridView>

RowCreated Event Handler:

protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("OnDblClick", 
			    "setSearch(" + e.Row.RowIndex + ");");
    }
}

The Javascript for the SetSearch() is below:
<script language="javascript" type="text/javascript">

    function setSearch(row) {
        var tbl = document.getElementById("gvMain");
        var txt = document.getElementById("txtSearch"); 
        txt.value = tbl.rows[row+1].cells[0].innerText;
    }

</script>
Since GridView is rendered as table, the individual cell can be accessed as tbl.rows[rowIndex].cells[cellIndex].innerText

Permalink | 1 Comment | Leave your comment
 
Commented by Nisha at 29-Mar-2010 11:01 AM

Hello Vijay,
I have read your forum at "http://forums.asp.net/p/1525995/3681124.aspx" i am looking for a share-point like feature. You can click Action => Edit as Datasheet. I need to implement this feature in ASP.Net web applicaiton. Please let me know if you have any solution workarround for this. my email id is: knisha77@gmail.com. Thank you in advance for your help.

SQL for Matrix Reports

I have a list of students in the student table. I have a list of exams in the exam table. There is a score table with the following columns studentID, examID and score. I want to create a matrix report with students in the rows and exams in the columns

The report will look like this:

Student NameExam 1Exam 2
Vijay7383
Rahul7964
If we can get the SQL select statement as SELECT StudentName, Exam 1, Exam 2 FROM TempTable, we can accomplish this matrix report. The whole point is writing a stored procedure to create this matrix table. Below is the SQL stored procedure for creating the matrix. This involves cursors and dynamic SQL:

CREATE PROCEDURE [dbo].[GetScoreMatrix] AS

CREATE TABLE #Temp
(
   StudentName VARCHAR(50)
)

DECLARE @sTable VARCHAR(1000)
DECLARE @sCol VARCHAR(1000)
SET @sTable = 'ALTER TABLE #Temp ADD '

DECLARE cExam CURSOR FOR 
SELECT ExamName FROM Exam

OPEN cExam
DECLARE @sExamName VARCHAR(50)

FETCH NEXT FROM cExam INTO @sExamName

WHILE @@FETCH_STATUS = 0
BEGIN

SET @sCol = @sTable + '[' + @sExamName + '] INT NULL'
EXEC (@sCol)

FETCH NEXT FROM cExam INTO @sExamName
END

CLOSE cExam
DEALLOCATE cExam


INSERT INTO #Temp (StudentName)
SELECT StudentName FROM Student


DECLARE cScore CURSOR FOR
SELECT s.StudentName, e.ExamName, x.Score FROM 
STUDENT s INNER JOIN Score x
ON s.StudentID = x.StudentID
INNER JOIN Exam e
ON x.ExamID = e.ExamID


OPEN cScore

DECLARE @sStudName VARCHAR(50)
DECLARE @iScore INT
DECLARE @sUpdate VARCHAR(1000)

FETCH NEXT FROM cScore INTO @sStudName, @sExamName, @iScore

WHILE @@FETCH_STATUS <> -1
BEGIN

SET @sUpdate = 'UPDATE #Temp  '
SET @sUpdate = @sUpdate + 'SET [' + @sExamName + '] = ' + CAST(@iScore AS VARCHAR(3))
SET @sUpdate = @sUpdate + ' WHERE StudentName = ''' + @sStudName + ''''

EXEC (@sUpdate)

FETCH NEXT FROM cScore INTO @sStudName, @sExamName, @iScore 

END

CLOSE cScore
DEALLOCATE cScore

SELECT * FROM #Temp

DROP TABLE #Temp
GO


Permalink | No Comments | Leave your comment
 

Confirm Backdating

I have a calendar control. If the user wants to backdate (ie, SelectedDate < Today), then I want to display a confirm messagebox asking the user: 'Are you sure you want to backdate?'

Calendar Control has a OnDayRender event that you can handle. In the eventhandler, for all date before today's date, add the javascript: confirm() function to the OnClick attribute as follows:


protected void cal_DayRender(object sender, DayRenderEventArgs e)
{
	if(e.Day.Date < DateTime.Today)
		e.Cell.Attributes.Add("OnClick", "return confirm('Are you sure
					you want to backdate?');");
}

Permalink | 1 Comment | Leave your comment
 
Commented by Rahul at 10-Feb-2010 05:15 AM
Can you provide more clarity on this??

Application_End() and Server.MapPath()

I wrote the following code in Application_End. It appeared as if Application_End was not firing


XmlDocument statDoc = new XmlDocument();
string path = Server.MapPath("~/App_data/Stat.xml");
statDoc.Load(path);
statDoc.SelectSingleNode("/Stats/Stat[@id=\"Visits\"]").Attributes["Count"].Value = Application["Visits"].ToString();
statDoc.SelectSingleNode("/Stats/Stat[@id=\"Hits\"]").Attributes["Count"].Value = Application["Hits"].ToString();
statDoc.Save(path);

With the help of this forum post, I was able to solve the mystery. Server.MapPath() does not work within Application_End() or Session.End()

Permalink | No Comments | Leave your comment
 

Inserting Image into the database

How do I insert an image into the database?

In the ASPX page, have a asp:FileUpload control and a Submit button. On clicking the submit button, the following code inserts into the table - Image and column - ImageStream, of type varbinary


<asp:FileUpload ID="fup" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Hello" OnClick="btnSubmit_Click" />


protected void btnSubmit_Click(object sender, EventArgs e)
{
    byte [] bytes = fup.FileBytes;

    SqlConnection conn = new SqlConnection("Data Source=localhost; Database=Pubs; Integrated Security=true;");
    conn.Open();
    string sql = "INSERT INTO IMAGE (ImageStream) VALUES (@bytes)";
    SqlCommand comm = new SqlCommand(sql, conn);
    comm.Parameters.AddWithValue("@bytes", bytes);
    comm.ExecuteNonQuery();

}

Permalink | 1 Comment | Leave your comment
 
Commented by Ravi at 10-Oct-2009 06:17 PM
This is very helpful to me