|
Just getting back to the this stored procedure. Just a little confused.
The stored procedure is called sp_InsertMember.
What it does is to add the member details, like first and last name, DOB, username and password to the tblMember table. If it @@ERROR is equal to 0, then it loops through the roles string passed as a parameter. The stored procedure used to add the member role is called sp_MemberInRoles_InsertMemberRole, and this I call using the EXECUTE function.
Now my questions are:
In sp_InsertMember I start off with a BEGIN TRANSACTION. If the member was added successfully, where do I add the COMMIT TRANSACTION? Before or after looping through the string of roles?
In the sp_MemberInRoles_InsertMemberRole I also have a BEGIN TRANSACTION and a COMMIT TRANSACTION. Lets say the member was added succesully, then it starts to loop through the string of roles. And lets say the member ahs 3 different roles. After the inserting the first role, it encounters an error, so in sp_MemberInRoles_InsertMemberRole it does a ROLLBACK TRANSACTION. Hoe far does it do this rollback? Will it remove the first role it added? What I want it to do is if the member was inserted successfully, and there is an error with inserting roles, that it removes all roles added, plus the member details.
Here is a modified copy of sp_Member_InsertMember:
ALTER PROCEDURE [dbo].[sp_Member_InsertMember]
(
-- input parameters here
)
AS
BEGIN
DECLARE @ErrorCode int
SET @ErrorCode = 0
DECLARE @TranStarted bit
SET @TranStarted = 0
DECLARE @RemainingRoles nvarchar(4000);
SET @RemainingRoles = @Roles;
DECLARE @CommaIndex int;
SET @CommaIndex = CHARINDEX(',', @RemainingRoles);
DECLARE @CurrentRole sysname
IF (@@TRANCOUNT = 0)
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
IF (EXISTS(SELECT MemberUsername
FROM dbo.Members
WHERE MemberUsername = @Username))
BEGIN
SET @ErrorCode = -2 -- Username already exists
GOTO CleanUp
END
INSERT INTO Members
(
-- column names
)
VALUES
(
-- input parameters
)
IF @@ERROR <> 0
BEGIN
SET @ErrorCode = -1
GOTO CleanUp
END
-- Loop thru roles string
WHILE(@CommaIndex <> 0)
BEGIN
SET @CurrentRole = LEFT(@RemainingRoles, @CommaIndex - 1);
EXECUTE dbo.sp_MemberInRoles_InsertMemberRole @@IDENTITY, @CurrentRole
SET @RemainingRoles = RIGHT(@RemainingRoles, LEN(@RemainingRoles) - @CommaIndex);
SET @CommaIndex = CHARINDEX(',', @RemainingRoles);
END
-- If all is cool
IF (@TranStarted = 1)
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION
END
RETURN 0
CleanUp:
IF (@TranStarted = 1)
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END
My sp_MemberInRoles_InsertMemberRole has the exact same structure, but just different input values and column names. Also, please comment on the wat that I have doe things here.
Regards,
ma se
South Africa
|
|
|
|
|
We can't directly pass array to stored procedure. But the trickish thing is to make the selected ids as a string seperated by commas(,) as "id1,id2,id3 etc. And pass this value to the stored procedure as a varchar in the stored procedure
Code can be written as
CREATE proc UspTestArray
@ids Varchar(100)
As
DECLARE @sql as varchar(500)
Set @sql = 'SELECT * FROM tablename WHERE id in (' + @ids + ')'
Exec(@sql)
go
http://www.sqlservercentral.com/columnists/aGrinberg/thearrayinsqlserver2000.asp[^]
"Aim to go where U have never been B4 and Strive to achieve it"
http://groups.yahoo.com/subscribe/dotnetforfreshers
http://himabinduvejella.blogspot.com
|
|
|
|
|
I use would use this technique:
First create a user function (one of my many tools):
--==========================================
CREATE FUNCTION tools_Splitter
(
@IDs VARCHAR(1000)
)
RETURNS @TableIDs TABLE(ID INT)
AS
BEGIN
-- Append comma
SET @IDs = @IDs + ','
-- Indexes to keep the position of searching
DECLARE @Pos1 INT, @pos2 INT
-- Start from first character
SET @Pos1=1
SET @Pos2=1
WHILE @Pos1 < Len(@IDs)
BEGIN
SET @Pos1 = CharIndex(',' , @IDs, @Pos1)
INSERT INTO @TableIDs SELECT Cast(Substring(@IDs,
@Pos2, @Pos1-@Pos2) AS BIGINT)
-- Go to next non comma character
SET @Pos2 = @Pos1+1
-- Search from the next charcater
SET @Pos1 = @Pos1 + 1
END
RETURN
END
--==================================================
Then your sproc could look like this:
declare @UserRoles varchar(1000)
declare @UserID int
SET @UserRoles=''
--Build a comma sep list of roles to insert
select @UserRoles=@UserRoles+Convert(varchar(20),OrderID)+',' from tblRoles WHERE 1=1
--Then insert the roles calling the tool you created earlier...
INSERT INTO tblRoles (@UserID,tools_Splitter(@UserRoles))
|
|
|
|
|
Try one of these aproaches:
Pass the comma, semicolon (whatever character that never can be a part of role name) separated string of the roles.
Alternatively you may call the same sproc several times within one transaction (to ensure data integrity).
Second approach requires several client - Sql Server roundtrips, but it a bit easier to implement.
Best regards,
-----------
Igor Sukhovhttp://sukhov.net
|
|
|
|
|
hi..
HOw can i store User's photo in sql db?
how can i upload photo by ASP.net page?
computer science..Ainshams UNV..
IS group.
|
|
|
|
|
See reply in ASP.Net forum.
|
|
|
|
|
Hi
I have a database containing several Tables. In a winfoarm, when I want to fill the dataset using data adapter, one of them generates this error (on the Fill statement)...
The strange part is taht when I delete the oleDataAdapter control from the form and create a new one, and then generate dataset from that, this error won't raise...
But the next time I open the solution, again the error continues to raise, unless I recreate the adapter!!!
Could any one tell me what is wrong (while I have recreated the data Table in the database and that didn't help...)
My data Table has two unicode right-to-left Text filds and two Memo fields...
Thanks
|
|
|
|
|
Sorry if this is a dumb question. I have a database that uses a number of user defined types, for example varchar(10) is aliased as "T_TableKey".
I would like to work with same aliases in C# for consistency, but there seems to be no equivalent to the C++ typedef statement.
Any suggestions?
TIA
|
|
|
|
|
I am using database which content are more than 100 000 rows.
In VB 6, with open data connection, I can view my data after 2 sec. but in VB .NET I am using DataSet but while data are loading to dataset it's take me 30 sec. and set DataGridView.DataSource = MyDataSet takes me next 10 .sec and waiting 40 sec. is too much.
Is posible using open conection in .Net and read only rows what I just need to view alike in VB 6?
DataSet is not just the best solution if you using large Database....
Can any body help me with this problem... thx
|
|
|
|
|
Pius__X wrote: I am using database which content are more than 100 000 rows.
That's not large. The database I'm working on at the moment grows at a rate of around a million rows per week.
Pius__X wrote: Is posible using open conection in .Net and read only rows what I just need to view alike in VB 6?
Since I've never used VB6 I can't answer that exact question. However, If you want to get data out and processing as it arrives rather than wait for the whole set to arrive first then you need a Data Reader. It provides a fast, forward only view of the data.
ColinMackay.net
Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?
|
|
|
|
|
DataReader is fast, but how to bind DataReader with DataGrid?
Because bind DataReader with DataGrid manually is too compliacetd.. may be not possible.....
manually - I thing, read rows which should be currently view in DataGrid.... not all rows which content database and when you scroll DataGrid then read next rows..... OH MY WEAK ENGLISH
Colin Angus Mackay <-- "million rows per week" are you using DataGrid for viewing these to many rows?
thx
|
|
|
|
|
Pius__X wrote: "million rows per week" are you using DataGrid for viewing these to many rows?
Good grief, No! I was just suggesting a new perspective on what you think of as a large database. In fact, SQL Server 2005 Enterprise Edition can handle a database that is up to a billion terrabytes in size. (Remember that SQL Server can handle many databases also)
Pius__X wrote: DataReader is fast, but how to bind DataReader with DataGrid?
If you must use a data grid then you have some other solutions. For example: Populate the data grid with less data. This is common, but requires extra work if you don't want the user to notice. However, normally, there would be page forward/backward buttons for the user to scroll through the data.
ColinMackay.net
Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?
|
|
|
|
|
Is it posible access to database whitout DataSet and DateReader and use open connectivity or there are only thease two ability?
Or how to set DataGrid to view data which sould be currently view in DataGrid, here is example.....
I have DataGrid where are show 10 rows, without using scroll bars, when I scroll down next 10 rows then some object read next 10 rows from databaze and show them to DataGrid... How to do this things in .NET?????
thx
|
|
|
|
|
can anybody tell me how i can connect vb.net with mysql.
byeeeee
Aavesh
|
|
|
|
|
Have you looked at mysql's website?
|
|
|
|
|
hi
i went on the mysql website.but i'm very novice in dotnet.so didn't get anything please send me the code or steps to connect.
Aavesh
|
|
|
|
|
Access MySQL from NET application can be done as same as MS SQL Server by using MySQL Connector for Net. Just download that connector from MySql website and see their samples enclosed.
|
|
|
|
|
|
hi
plz can u help me
I make the data base employee form
this form company
i but all information in the data base and make in vb form to make a small project
i went to help and give the code for use a search button
to search in the data base and give me the information for the employee who`s i wont to search it
and thanks or ur help
|
|
|
|
|
hi, I have a dataset, DataSet1, populated from a database table named "Sections" which is bounded to DataGrid1.
I want to remove a row if a user selects it in the datagrid and presses the remove button. here is the vb-code:
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
If DataGrid1.CurrentRowIndex >= 0 Then
DataSet1.Tables("Sections").Rows.Remove(DataSet1.Tables("Sections").Rows(DataGrid1.CurrentRowIndex))
MessageBox.Show(DataSet1.HasChanges)
OleDbDataAdapterSections.Update(DataSet1, "Sections")
DataSet1.AcceptChanges()
End If
End Sub
But messagebox shows False, and the row is not actually removed from the database, ALTHOUGH DataGrid1 shows that the row has removed from dataset!
(I use the same method i.e. DataSet1...Rows.Add(..) and then the oleDbDataAdapterSections.Update(..) to add rows which works perfectly!! But the added rows are not removed using the same code from the database...)
I really dunno the reason and got confused!
-- modified at 17:50 Friday 31st March, 2006
|
|
|
|
|
i want to company that if today date is greater than do step 1 and if not than do step 2 and i m doing this
IF dbo.FormatDate(GETDATE()) > dbo.FormatDate('28/04/2006')
--Step 1
ELSE
--Step 1
but its not proper working i can do this with year, month and day individual comparison buts not proper way to acheive task.
can anyone tell me that what's the mistake in above code
dbo.FormatDate() is a function which extract only date from date and time.
Thanks
|
|
|
|
|
I'm guessing (becuase you've not provided the code) that FormatDate is a UDF that returns some value that you can test against. I don't know why you need to do this as you can check greater than/less than on a date already.
IF GETDATE() > '2006-04-28'
BEGIN
-- Do stuff
END
ELSE
BEGIN
-- Do other stuff
END
I would recommend that you always use ISO dates (YYYY-MM-DD) - that way you don't get messed around with cultural specific problems.
ColinMackay.net
Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?
|
|
|
|
|
Thanks for ur comments i have solved the problem with this code
SET Dateformat dmy
IF GETDATE() > dbo.FormatDate('24/04/2006')
BEGIN
PRINT 'A'
END
ELSE
BEGIN
PRINT 'B'
END
Thanks
Best Regards,
Tariq Mahmood
|
|
|
|
|
Hello
I'm trying to connect to SQL Server2000 that in another computer in local area network.
but it says login failed for user "Guest"
- I added my windows account (MyDomain\MyWindowsUserName) to SQL Server.
- I use windows only authentication.
- both computers are Windows XP Pro-service pack 2
- When I add Guest windows user to SQL server, it works fine.. But I don't want to use Guest account.
- both computers are in same WorkGroup
- I added user name to server computer with exactly same user name and password.
any help... thanks
|
|
|
|
|
denizmercan wrote: I added user name to server computer with exactly same user name and password.
Have u added username in Sqlserver Database Users?
"Aim to go where U have never been B4 and Strive to achieve it"
http://groups.yahoo.com/subscribe/dotnetforfreshers
http://himabinduvejella.blogspot.com
|
|
|
|