Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, March 29, 2012

Database conn

I have downloaded some code on how to upload images to a sql database and I am getting
this error message

"SQL Server does not exist or access denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: SQL Server does not exist or access denied."

I have opened Enterprise Manager and made sure I was a "user" and everything seems to be ok there.
The connection string looks like this
"server=localhost;uid=sa;pwd=;database=ImageUpload"
Could anyone give me an idea on what to check or try next

Thanksu can try replacing 'localhost' with the ip address
use ipconfig command to know abt ip addr|||What kind of authentication you used? SQL Server authentication or windows integrated?

Try windows integrated authentication.

Database concurrency

I'm wondering whether the following code would work if users are RAPIDLY registering (assumption) WITH the same username.
public bool UsernamExists(string username){string sql ="SELECT true FROM [users] WEHRE username = @.username;";return Convert.ToBoolean(comm.ExecuteScalar());}public bool Signup(User user){bool usernameExists = UsernameExists(user.Username);if( usernameExists )return false;//update or insert sql for user etc blah blah}

If two users try to signup AT THE VERY SAME TIME (DOWN TO THE NANOSECOND), would this technique work? Do I have to wrap it in a transaction, stored procedure??


Thanks.

no that does not guarantee unique usernames.

You should add a unique constraint to your username column in the database to guarantee that duplicates cannot exist.

Then, you can still do your usernameexists check it you want, but you would also want to add an error handler (try/catch) to catch the exception that would occur in the unlikely event that two users simultaneously submitted the same usernames.

|||I know about unique column BUT would the technique I mentioned above work IN TERMS OF CODING? Like 2 users submitting AT THE VERY SAME TIME.|||

Yes the code would run, but...

if you dont have a unique constraint in your db, then you could end up with 2 records with the same username.

|||

Your code will not work because you have error in it

"SELECT true FROM [users]WHERE username = @.username;";

you have no comm object defined ad no parameter added to command. But if you update it you will have no guaranty that you will not have two user with the same name in your database. But you can modify you code to do something like this with transaction (safer) or not:

BEGIN TRAN

IF not exist(SELECT * FROM [users]WHERE username = @.username)
BEGIN
INSERT into [users](username)
Values (@.username)
SELECT 'True' [Result]
END
ELSE
SELECT 'False' [RESULT]

COMMIT TRAN

In this way you will save one round trip to SQL server an everything will be done in one shot and very fast.

Thanks


|||

I know you have to use the comm object, excluded it out for the simplicity.

I thought about wrapping my sql syntax in a transaction with row locking, but I'm not to keen on writing that much sql syntax. I like to keep it minimal.

Is there a way the comm object could "perform" the transaction automatically? Can you show me a simple example?

|||

Hi vze1r2ht

There is no way to make the comm object start a transaction automatically. You will need to start a transaction by explicitly calling SqlConnection.BeginTransaction() and assign the returned SqlTransaction object to a SqlCommand.

You can also perform this in a stored procedure by using BEGIN TRAN.

|||

Kevin Yu - MSFT:

Hi vze1r2ht

There is no way to make the comm object start a transaction automatically. You will need to start a transaction by explicitly calling SqlConnection.BeginTransaction() and assign the returned SqlTransaction object to a SqlCommand.

You can also perform this in a stored procedure by using BEGIN TRAN.

Thank you. Is there any major performance hit when using the SqlConnection.BeginTransaction() technique vs SQL's BEGIN Tran? Because of the massive amounts of transactions I will need to write for my classes, it will alll be inline if it was SQL and NOT stored procedures (hard to maintain).

|||

Hi,

As far as I can see, they are the same. Not much differences.

Sunday, March 11, 2012

database and authorization

i know the basics about how to manipulate SQL server database, i mean the code itself, but there are few other things which i don't understand.

first, databases which i have created by my own, i can use only when
i have the website itself open (i get an icon at the taskbar)
but database such as 'master' which came with the sqlserver, i can acess always even without running the page with visual studio first. i have no idea why.

also, when i try to connect my website using my ip, i can only do it with
the master database.. those i created by my own i can only connect when i enter the URL page as localhost.

I don't understand what's USER INSTANCE for and how do i really set
a database username and password

can someone exaplin a little? thanks, because i am very confused .from the post it is not clear what your problem is. do u mean that u can connect to master database but not to the one u created? please ready the Sticky post at the top to make a more meaningful post if u are interested in getting proper reply....|||Hi

Are you using Visual Studio to look at your database, or SQL Server Management Studio? The second one is the preferred tool. The first is something you are more likely to use if you are a webby developer rather than a database developer.|||I have them both.
I'll try making it clearer, a question in each reply.

I have for example 2 databases, "phynew" which i created and "master"
which comes with sql server.

when i am using the master database i can always connect
whether i type
http://localhost/Learning/Default.aspx
or with ip
http://82.81.184.139/Learning/Default.aspx

but when i am trying to connect "phynew" i need to use the debugger so it will give me a port
i need to type the port in the URL (automatically done when i use the debugger) or else i get a connecting error.
http://localhost:4606/Learning/Default.aspx
when i type
http://82.81.184.139:4606/Learning/Default.aspx
the website doesn't work at all.

therefore i can't give someone else a working URL if i am using own created database.
what's the difference between those databases?

Thursday, March 8, 2012

Database access in custom code

Hi, I'm trying to access a db from the custom code, but it doesn't seem
to be possible, because if i try to preview the report i get errors:
[BC30002] 'DAO.Recordset' type not defined, and some other similar
errors if i try to use SqlConnection ect.
Is it a strange behavior of my reporting services installation?
If not how can I access db from my custom code?I can do it fine so it must be something in your code.
A tip would be to try the customer code out by writing a simple windows
application that uses the code. If this works then the report server should
also work (unless you have security problems).
If you are writing the code in the Code properties of the report then be
sure to add the appropriate references in the references tab.
Craig
"Tur" <cenci.cristiano@.gmail.com> wrote in message
news:1141376485.288962.263920@.v46g2000cwv.googlegroups.com...
> Hi, I'm trying to access a db from the custom code, but it doesn't seem
> to be possible, because if i try to preview the report i get errors:
> [BC30002] 'DAO.Recordset' type not defined, and some other similar
> errors if i try to use SqlConnection ect.
> Is it a strange behavior of my reporting services installation?
> If not how can I access db from my custom code?
>|||Hi Craig, thank you so much for your answer!
I'm writing my custom code in the Code properties of the report, as you
say,
but I'm not sure how to add the appropriate references in reference
tab.
An example:
function MyFun(myStr as String) as String
Dim temp as DAO.Recordset
return "Nothing"
end function
raises an error: "d:\myPath\myRDL.rdl : [BC30002] 'DAO.Recordset' type
not defined."
I'm not sure of what is the problem...|||Hi Craig, thank you so much for your answer!
I'm writing my custom code in the Code properties of the report, as you
say,
but I'm not sure how to add the appropriate references in reference
tab.
An example:
function MyFun(myStr as String) as String
Dim temp as DAO.Recordset
return "Nothing"
end function
raises an error: "d:\myPath\myRDL.rdl : [BC30002] 'DAO.Recordset' type
not defined."
I'm not sure of what is the problem...|||Hi Craig, thank you so much for your answer!
I'm writing my custom code in the Code properties of the report, as you
say,
but I'm not sure how to add the appropriate references in reference
tab.
An example:
function MyFun(myStr as String) as String
Dim temp as DAO.Recordset
return "Nothing"
end function
raises an error: "d:\myPath\myRDL.rdl : [BC30002] 'DAO.Recordset' type
not defined."
I'm not sure of what is the problem...|||Firstly I urge you to try out your custom code from outside of Reporting
Services. This will give you 100 times better error messages plus you will
be able to debug your code which you can't do if you are writing the code in
the reports code.
After you have completed this and its all working fine then, and only then,
copy the code from the function in to the Code properties of the report.
Now the error you are currently getting is to do with a reference that the
report server is not aware of. To add this reference go to the
Report>Report Properties...>References and click on the "..." to add a new
reference.
Choose the DLL that contains the namespace for the DAO.Recordset and add it
to your report.
Craig
"Tur" <cenci.cristiano@.gmail.com> wrote in message
news:1141722816.893159.67500@.j33g2000cwa.googlegroups.com...
> Hi Craig, thank you so much for your answer!
> I'm writing my custom code in the Code properties of the report, as you
> say,
> but I'm not sure how to add the appropriate references in reference
> tab.
>
> An example:
> function MyFun(myStr as String) as String
> Dim temp as DAO.Recordset
> return "Nothing"
> end function
>
> raises an error: "d:\myPath\myRDL.rdl : [BC30002] 'DAO.Recordset' type
> not defined."
>
> I'm not sure of what is the problem...
>

Friday, February 17, 2012

data type DateTime

Hi all!!
I'm developing a project using VS 2005 (C# code) and SQL Server 2005.
I have some problems with the data type 'DateTime'.

In some parts of my project, I have used DataSets and DataAdapters and I can insert in a dataRow a data type 'DateTime' and it is inserted as dd/mm/yyyy hh:mm:ss. That's OK.

But in other parts I'm not using Datatables and DataAdapters and I insert new rows in the database using ExecuteNonQuery, but the data type 'DateTime' in the format dd/mm/yyyy hh:mm:ss is not valid. It only allows formats like mm/dd/yyyy hh:mm:ss, but I want the other format.

Why is dd/mm/yyyy hh:mm:ss working with DataAdapters? Why is dd/mm/yyyy hh:mm:ss not working with ExecuteNonQuery? Is there any way to specify in the Database that I want the format dd/mm/yyyy hh:mm:ss??

Thanks in advance folks,
Javier.

Try to add a setting for DATEFORMAT just before you insert statement, for example:

SET DATEFORMAT dmy; insert into ...

|||Thanks Iori_Jay,
But I've checked that it is necessary to write "SET DATEFORMAT dmy" every time I want to insert a new row in the table that has DateTime attribute. Is there another way of keeping this value within the DB's life??

thanks again|||Since the DATEFORMAT option is a session(means connection to SQL) option, you have to set it in each connection. And within the connection, the option is effective.