Showing posts with label asp. Show all posts
Showing posts with label asp. Show all posts

Wednesday, March 21, 2012

multiple values per table cell?

I am converting a site from asp to .net and the previus writer did something I never saw.(I am somewhat new to programming all together). He put multiple values in a single cell of the database. I have always learned to use a seperate column for each value.
Is this a classic asp thing and there are better ways now? Or is this something I should do myself? The columns type is text and the contents look like this:
<Details Expertise="bla bla" Description="We are a three-divisional company ...," WebSite="www.blabla.com" AccountLevel="Free" AccountStatus="Active" WorkHomeZIP="22222" ContractStartDate="01/01/2003" ContractEndDate="12/31/2003"><Address Location="Work" State="Oh" ZIP="22222"/><Email Location="Work" Value=""/><Phone Location="Fax" Code="9801" Ext=""/></Details>
Putting aside the question of the style being used for a moment, the contents of the column is not *really* multiple values, but a single XML string. This allows it to be easily read into a XmlDocument object and parsed.

As for the reason behind this approach, the only one I can thing of would be to store dynamic categories of information that is not known at design time. While none come to mind immediately, I'm sure that there might be times when this approach is quite valid. It is *not*, however, a classic ASP thing, but a database design decision. As to whether you should change the approach, I would if the attributes in the XML string were always the same. Better to use a database to do what databases were intended to do ;)

HTH

Monday, March 19, 2012

Multiple users logging into a database

I'm developing an application in ASP.Net that uses SQLServer. The database person in the team wants to have each user have a seperate login to the database so he can tell who has done what changes in the database. Is this a good practice? Or are their performance\security issues with this model?For system performance it is better to use one account (to use one connection string) and connection pooling. My solution is as follows. Create your own users and log-in system. You can save current username or id to Session. Then create a table system_events to your database. Everytime user makes something you can insert a new row that describes user action. Also, and it is maybe even better, you can use plain text logs for this.|||We implemented this feature and what we did was write a stored procedure to check the old value against the new values being updated in the database and then it writes only the changes to a seperate table designed for tracking changes. We then wrote a page to display change history by date range in a D-Grid. It works very well and there has been no noticable performance hit since implementation.

CREATE PROCEDURE AddChangeHistory
(
@.propertyID INT,
@.userName VARCHAR(75), --Assigned in session when a user logins
@.tableName VARCHAR(75),
@.fieldName VARCHAR(80),
@.fieldChangeVal varchar(200)
)

AS

DECLARE @.fieldInitVal varchar(200)

BEGIN TRAN
CREATE TABLE #tempVal
(
fieldInitVal VARCHAR(200)
)

INSERT INTO #tempVal exec('SELECT TOP 1 ' + @.fieldName + ' FROM ' + @.tableName + ' WHERE propertyID=' + @.propertyID + '')

SELECT @.fieldInitVal = (SELECT top 1 * FROM #tempVal)

DROP TABLE #tempVal

IF(LOWER(@.fieldInitVal) <> LOWER(@.fieldChangeVal))
BEGIN
INSERT INTO ChangeHistory(propertyID, userName, tableName, fieldName, fieldInitVal, fieldChangeVal)
VALUES (@.propertyID, @.userName, @.tableName, @.fieldName, @.fieldInitVal, @.fieldChangeVal)
END

IF(@.@.ERROR <> 0)
BEGIN
ROLLBACK TRAN
END
ELSE
BEGIN
COMMIT TRAN
END
GO


SPROC to update my database
CREATE PROCEDURE dbo.oPK_UpdateAdminHours
(
@.Role AS varchar(100),
@.UserName AS varchar(100),
@.vcManager AS varchar(50),
@.vcAssistant1 AS varchar(50),
@.vcAssistant2 AS varchar(50),
@.vcLeasingManager AS varchar(50),
@.vcMarketingDirector AS varchar(50),
@.vcLeasing1 AS varchar(50),
@.vcLeasing2 AS varchar(50),
@.vcLeasing3 AS varchar(50),
@.vcLeasing4 AS varchar(50),
@.vcLeasing5 AS varchar(50),
@.bMon_Fri AS bit,
@.cDisplaySummer_Winter AS char(1),
@.vcOfficeHoursSummerMon AS varchar(25),
@.vcOfficeHoursWinterMon AS varchar(25),
@.vcOfficeHoursSummerTue AS varchar(25),
@.vcOfficeHoursWinterTue AS varchar(25),
@.vcOfficeHoursSummerWed AS varchar(25),
@.vcOfficeHoursWinterWed AS varchar(25),
@.vcOfficeHoursSummerThur AS varchar(25),
@.vcOfficeHoursWinterThur AS varchar(25),
@.vcOfficeHoursSummerFri AS varchar(25),
@.vcOfficeHoursWinterFri AS varchar(25),
@.vcOfficeHoursSummerSat AS varchar(25),
@.vcOfficeHoursWinterSat AS varchar(25),
@.vcOfficeHoursSummerSun AS varchar(25),
@.vcOfficeHoursWinterSun AS varchar(25),
@.vcTimeZone AS varchar(50),
@.PropertyID AS INT
)
AS
--Calling the AddChangeHistory SPROC to check the values before updating the Database
exec AddChangeHistory @.propertyID, @.userName, 'vcManager',@.vcManager
exec AddChangeHistory @.propertyID, @.userName, 'vcAssistant1',@.vcAssistant1
exec AddChangeHistory @.propertyID, @.userName, 'vcAssistant2',@.vcAssistant2
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasingManager',@.vcLeasingManager
exec AddChangeHistory @.propertyID, @.userName, 'vcMarketingDirector',@.vcMarketingDirector
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasing1',@.vcLeasing1
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasing2',@.vcLeasing2
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasing3',@.vcLeasing3
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasing4',@.vcLeasing4
exec AddChangeHistory @.propertyID, @.userName, 'vcLeasing5',@.vcLeasing5
exec AddChangeHistory @.propertyID, @.userName, 'bMon_Fri',@.bMon_Fri
exec AddChangeHistory @.propertyID, @.userName, 'cDisplaySummer_Winter',@.cDisplaySummer_Winter
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerMon',@.vcOfficeHoursSummerMon
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterMon',@.vcOfficeHoursWinterMon
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerTue',@.vcOfficeHoursSummerTue
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterTue',@.vcOfficeHoursWinterTue
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerWed',@.vcOfficeHoursSummerWed
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterWed',@.vcOfficeHoursWinterWed
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerThur',@.vcOfficeHoursSummerThur
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterThur',@.vcOfficeHoursWinterThur
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerFri',@.vcOfficeHoursSummerFri
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterFri',@.vcOfficeHoursWinterFri
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerSat',@.vcOfficeHoursSummerSat
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterSat',@.vcOfficeHoursWinterSat
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursSummerSun',@.vcOfficeHoursSummerSun
exec AddChangeHistory @.propertyID, @.userName, 'vcOfficeHoursWinterSun',@.vcOfficeHoursWinterSun
exec AddChangeHistory @.propertyID, @.userName, 'vcTimeZone ',@.vcTimeZone

--Put your Update code here (removed for space)


Hope this helps.|||Thank you so much. That is what I needed.

Monday, March 12, 2012

multiple tables

hi,

I have 3 tables: Unix, Windows & Sybase

there are 3 dropdownlists in .asp page,

dropdown1=<displays the lists of table> (Unix, Windows & Sybase) variable as @.table

dropdown2=<displays the list of Columns from the dropdown1 selected table> variable as @.server

dropdown3=<displays the list of Row Header from the dropdown1 selected table> variable as @.user

how shal i define the select statement for this:

it should be something like,

Select * from @.table WHERE Server= @.server AND Row-Header = @.user

I appreciate if you could kindly help me in getting the correct syntax, thanx in advance.

Hi,

the tablename cannot be defined at runtime without using dynamic Sql, compose your SQLString first (e.g. in a stored procedure) then execute it via sp_executesql:

DECLARE @.SQLString VARCHAR(4000)

SET @.SQLString ='Select * from ' + @.table + ' WHERE Server= ''' + @.server + ''' AND Row-Header = ''' + @.user + ''''

But keep in mind that dynamic sql can be dangerous, read the aarticle of Erland first: http://www.sommarskog.se/dynamic_sql.html


HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de
--

|||

hi,

Thanks a million Jens,

I have done the same way as you explained ( First line: DECLARE @.SQLString VARCHAR(4000) Second line: SET @.SQLString ='Select * from ' + @.table + ' WHERE Server= ''' + @.server + ''' AND Row-Header = ''' + @.user + '''').executed the .asp page: it shows only the dropdown & no table. Appreciate your help, many thanks again for this:

-

execution script part:
-
<script runat="server">
Sub OSClick1(ByVal sender As Object, ByVal e As EventArgs)
'EXEC "@.SQLString"'
End Sub
</script>
-
processing script part:
-
<asp:Button ID="btnOSClick1" runat="server" Text="Get" Width="90px" OnClick="OSClick1" /><br />
<br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource4">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Unix.Primary %>"
SelectCommand="CREATE PROCEDURE general_select @.table nvarchar(127), @.server nvarchar(127), @.user nvarchar(127),&#13;&#10;DECLARE @.SQLString VARCHAR(4000)&#13;&#10;SET @.SQLString ='Select * from ' + @.table + ' WHERE Server= ''' + @.server + ''' AND Row-Header = ''' + @.user + ''''&#13;&#10;'">
<SelectParameters>
<asp:ControlParameter ControlID="txtOS" Name="table" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="txtSrvUnix" Name="server" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="txtUnixDes" Name="user" PropertyName="SelectedValue" />
<asp:Parameter Name="SQLString" />
</SelectParameters>
</asp:SqlDataSource>
-

|||Hi,

I am not very familiar with those new SQLDataSource mechanism, but you do not need to create the procedure as a Select Command. Create the procedure before and do just execute the procedure as the Select Command. In plain C# code you will have to assign the Commandtype = StoredProcedure first to the Select command (You will probably have to do this here also) and then just write something like SelectCommand="general_Select"

HTH, Jens K. Suessmeyer.


http://www.sqlserver2005.de

Friday, March 9, 2012

Multiple SqlDataSource Controls on a Page

Thanks for your help.

I'm just getting into ASP.NET 2.0 and started using the SqlDataSource control to declaritively connect to a database. I was wondering if I have two SqlDataSource controls on one page with two select commands querying the same database, will the that create to seperate instances of opening and closing the connection to the database, or will it open the database once, execute both queries, and then close the connection. It seems that if it creates two separate connections, it would still be more efficient to to connect to the database programmatically using ADO.NET. You would only need to make one trip to the database, pull down all of your data, and then close the connection.

Adrian

I believe it will open two connections. Which is what you would probably want anyhow. That would allow SQL Server to prioritize and minimize response time.

That said, most of the time you can write code to do things more efficiently than depending on a generic library. It's a trade off, and often the code you may generate may not actually be more efficient unless you are also able to spend the time to implement the more advanced features of the library, in this case, the controls ability to selectively databind and cache data.

Wednesday, March 7, 2012

multiple SQL commands at once

I'm running asp.net 2.0 and acessing MSSQL 2K. I am trying to run a query in which I need to set up variables first. I tried the following group of commands as shown, passing it to the SqlDataReader object, but it failed.. does anyone know how i can pass multiple SQL commands?

MainQuery = "declare @.MinGrades as Table(GradeID Bigint) " & _
" INSERT @.MinGrades SELECT MIN(CreditGrades.SplitID) AS Expr1" & _
" FROM CreditGrades INNER JOIN" & _
" CreditGradeSplits ON CreditGradeSplits.CreditGradeSplitID = CreditGrades.SplitID " & _
" WHERE (CreditGrades.x0x30 = - 1 OR " & _
" CreditGrades.x0x30 >= 0) AND (CreditGrades.x1x30 = - 1 OR " & _
" CreditGrades.x1x30 >= 0) AND (CreditGrades.x2x30 = - 1 OR " & _
" CreditGrades.x2x30 >= 0) AND (CreditGrades.x3x30 = - 1 OR " & _
" CreditGrades.x3xNOD >= 0) " & _
" GROUP BY CreditGradeSplits.CreditGradeGroupID" & _
" Select * from @.MinGrades "Looks good to me. What error are you getting? Please copy/paste the stack trace if possible as well.|||

Looks good to me. What error are you getting? Please copy/paste the stack trace if possible as well.

I suspect you realize that you don't really need to do that query in 3 commands, and can combine them into a single select.