1: protected void Page_Load(object sender, EventArgs e)
2: {
3: String UserID, UserName, UserEmail, UserFullName;
4: //==== Value Assignment ================================================
5: //----USER ID
6: UserID = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
7: txtUserName_Def.Text = UserID;
8: int backslashIndex = UserID.IndexOf("\\") + 1;
9: int userCharNum = UserID.Length;
10: UserID = UserID.Substring(backslashIndex, userCharNum - backslashIndex);
11: //----USER NAME & USER EMAIL
12: String strConnectionString = ConfigurationManager.ConnectionStrings["EmpSale2009ConnectionString"].ConnectionString;
13: SqlConnection myConnection = new SqlConnection(strConnectionString);
14: myConnection.Open();
15: SqlCommand command = new SqlCommand("SELECT userName, userEmail, FullName FROM users WHERE userName='" + UserID + "'", myConnection);
16: command.ExecuteNonQuery();
17: SqlDataReader myreader = command.ExecuteReader();
18: while (myreader.Read())
19: {
20: UserName = (string)myreader["userName"];
21: UserEmail = (string)myreader["userEmail"];
22: UserFullName = (string)myreader["FullName"];
23: txtUserName_Def.Text = UserFullName;
24: }
25: myreader.Close();
26: myConnection.Close();
27: }
Sunday, November 27, 2011
c# connect to SQL database through webconfig
Wednesday, June 30, 2010
search database for a value
CREATE PROC SearchAllTables
(
@SearchStr nvarchar(100)
)
AS
BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)
WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)
IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
END
Monday, May 10, 2010
Dynamic Cross-Tabs/Pivot Tables
CREATE PROCEDURE crosstab
@select varchar(8000),
@sumfunc varchar(100),
@pivot varchar(100),
@table varchar(100)
AS
DECLARE @sql varchar(8000), @delim varchar(1)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + ' WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM ' + @table + ' WHERE '
+ @pivot + ' Is Not Null')
SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )
SELECT @delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )
WHEN 0 THEN '' ELSE '''' END
FROM tempdb.information_schema.columns
WHERE table_name='##pivot' AND column_name='pivot'
SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' +
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN '
+ @delim + convert(varchar(100), pivot) + @delim + ' THEN ' ) + ', ' FROM ##pivot
DROP TABLE ##pivot
SELECT @sql=left(@sql, len(@sql)-1)
SELECT @select=stuff(@select, charindex(' FROM ', @select)+1, 0, ', ' + @sql + ' ')
EXEC (@select)
USAGE:
execute crosstab
'select ItemNo from vtbl_ModelStock Group By ItemNo', 'sum(ModelStockQty)', 'CompanyID', 'vtbl_ModelStock'
SET ANSI_WARNINGS ON
SOURSE:
http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables
Friday, May 07, 2010
microsoft office access was unable to create an mde database
"microsoft office access was unable to create an mde database"
Solution:
- Run MSACCESS.EXE /decompile
- Edit a form that contains a code/event
- On Microsoft VB code click Debug then Compile the project - this will remove all the unnecessary codes.
- Save it back then recreate an MDE
Wednesday, November 04, 2009
Credit Card Number validation C#
public bool IsValidNumber(string cardNumber)
Friday, September 25, 2009
Replication Techniques
There are three different replication techniques available. They are snapshot replication, merge replication and transactional replication.
Snapshot replication is a single unidirectional push of data. When updated data is fed from the publisher to the subscribers, all of the data is sent each time.
Merge replication is a bidirectional replication that transmits data either in real time, or on a schedule. Merge replication is the only bidirectional replication technique available.
Transactional replication is unidirectional from the publisher to the subscribers. Data can be sent on a schedule or in real time. As data is transmitted to the subscriber, all data changes are processed in the order they were made on the publisher.
Friday, July 24, 2009
Thursday, July 23, 2009
search for column in DB
Monday, July 20, 2009
send query from email from database
Tuesday, December 02, 2008
email Query result
Search for Active Directory information
Monday, December 01, 2008
Wednesday, November 26, 2008
ASP.NET Validator Controls
Validation Controls.
http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=46
With ASP.NET, there are six(6) controls included. They are:
- The RequiredFieldValidation Control
- The CompareValidator Control
- The RangeValidator Control
- The RegularExpressionValidator Control
- The CustomValidator Control
Validator Control Basics
All of the validation controls inherit from the base class BaseValidator so they
all have a series of properties and methods that are common to all validation controls.
They are:
- ControlToValidate - This value is which control the validator is applied
to.
- ErrorMessage - This is the error message that will be displayed in the validation
summary.
- IsValid - Boolean value for whether or not the control is valid.
- Validate - Method to validate the input control and update the IsValid property.
- Display - This controls how the error message is shown. Here are the possible
options:
- None (The validation message is never displayed.)
- Static (Space for the validation message is allocated in the page layout.)
- Dynamic (Space for the validation message is dynamically added to the page if validation fails.)
- None (The validation message is never displayed.)
The first control we have is the RequiredFieldValidation Control. As it's obvious,
it make sure that a user inputs a value. Here is how it's used:
Required field: <asp:textbox id="textbox1" runat="server"/> |
Inside the validator tag, we have a single *.
The text in the innerhtml will be shown in the controltovalidate if the control is not valid.
It should be noted that the ErrorMessage attribute is not what is shown.
The ErrorMessage tag is shown in the Validation Summary (see below).
The CompareValidator Control
Next we look at the CompareValidator Control. Usage of this CompareValidator is for confirming new passwords, checking if a departure date is before the arrival date, etc. We'll start of with a sample:
Textbox 1: <asp:textbox id="textbox1" runat="server"/><br /> |
Another usage of the ComapareValidator is to have a control compare to a value. For example:
Field: <asp:textbox id="textbox1" runat="server"/> |
The data type can be one of: Currency, Double, Date, Integer or String. String being the default data type.
The RangeValidator Control
Range validator control is another validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Sample:
Enter a date from 1998: |
The regular expression validator is one of the more powerful features of ASP.NET.
Everyone loves regular expressions. Especially when you write those really big nasty
ones... and then a few days later, look at it and say to yourself. What does this
do?
Again, the simple usage is:
E-mail: <asp:textbox id="textbox1" runat="server"/> |
Here is a webpage to check regular expressions.
The CustomValidator Control
The final control we have included in ASP.NET is one that adds great flexibility to our validation abilities. We have a custom validator where we get to write out own functions and pass the control value to this function.
Field: <asp:textbox id="textbox1" runat="server"> |
OnServerValidate. These are the tell the validation control which functions
to pass the controltovalidate value to. ClientValidationFunction is usually a javascript
funtion included in the html to the user. OnServerValidate is the function that
is server-side to check for validation if client does not support client-side validation.
Client Validation function:
<script language="Javascript"> |
Sub ServerValidate (objSource As Object, objArgs As ServerValidateEventsArgs) |
Validation Summary
ASP.NET has provided an additional control that complements the validator controls.
This is the validation summary control which is used like:
<asp:ValidationSummary id="valSummary" runat="server" |
The validation summary control will collect all the error messages of all the non-valid
controls and put them in a tidy list. The list can be either shown on the web page
(as shown in the example above) or with a popup box (by specifying ShowMessageBox="True")
Now you know how to use the Validator Controls in ASP.NET! Have fun!
I will also upload a sample of all the validator controls to the code sample section.
Acknoledgment: Professional ASP.NET (published by Wrox) was used a reference.
It's a good book!
Tips to remember
- If you are doing server-side validation, make sure the button onclick method has
a Page.IsValid if statement or it will look like your validators aren't doing anything
- Don't forget to wrap everything in the <form runat=server> tag.
ListBox max value received from database field.
asp.net function : receives value from one of the database field that acts as an max value for a listbox. useful when ordering quantity needs to be restricted to available quantity.
----ASP.NET ----
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:ListBox ID="ListBox1" SelectionMode="Single" Rows="1" runat="server" DataSource='<%# ddlItems((string)(Eval("QtyLeft").ToString())) %>'>asp:ListBox>
ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
asp:TemplateField>
protected ListItemCollection ddlItems(String max)
{
ListItem li = null;
ListItemCollection liC = new ListItemCollection();
int intmax = Convert.ToInt32(max);
for (int i = 0; i <= intmax; i++)
{
li = new ListItem();
li.Text = ""+ i;
liC.Add(li);
li = null;
}
return liC;
}
