Showing posts with label event. Show all posts
Showing posts with label event. Show all posts

Wednesday, March 28, 2012

Problems hitting SqlServer from Global.asax Application_Start

Hi,

In my application I Application_Start event in Global.asax I am wanting to populate an Application state variable to hold data from a database.

I can use the exact code elsewhere in the application but when I try to open my SqlConnection object I get the error "SQL Server does not exist or access denied."

I'm using NT security and impersonation.

I've put a trace on the server and my user name isn't appearing as trying anything so it is as if it cannot find the server.

Are there any issues with trying to open a connection from Application_Start?

Thanks.

Can you post your source code here?

|||There was an atricle about this in the latest MSDN magazine. Basically, asp.net won't do impersonation in the application start event. Your best bet is to grab the data the first time that you need it in regular code.

Monday, February 20, 2012

problem with Update command with parameters

Hi,

i try to update field 'name' (nvarchar(5) in sql server) of table 'mytable'.
This happens in event DetailsView1_ItemUpdating with my own code.
It works without parameters (i know, bad way) like this:
SqlDataSource1.UpdateCommand = "UPDATE mytable set name= '" & na & "'"

But when using parameters like here below, i get the error:
"Input string was not in a correct format"

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
DetailsView1.ItemUpdating
SqlDataSource1.UpdateCommand = "UPDATE mytable set name= @.myname"
SqlDataSource1.UpdateParameters.Add("@.myname", SqlDbType.NVarChar, na)
SqlDataSource1.Update()
End Sub

I don't see what's wrong here.
Thanks for help
Tartuffe

Try the following:

SqlDataSource1.UpdateParameters.Add("@.myname", SqlDbType.NVarChar, 5).value = na

|||

Set the updatecommand and parameter types in the sqldatasource.

In the detailsview.Updating event, you can play with the e.NewValues, e.OldValues collections to make any changes you want.

Or you can make your changes in the sqldatasource.updating event and play with the parameters directly via e.Command.Parameters.

Much cleaner.

|||

Thanks to reply.

I tried this:

SqlDataSource1.UpdateParameters.Add("@.myname", SqlDbType.NVarChar, 5).value = na

but in the Intellisense, 'value' doesn't appear, and when i force it in the code, i ger the strange error:

'value' is not a memeber of 'integer' ...

|||

Hi tartuffe2,

SqlDataSource.UpdateParameters.Add() will return the index value of the added item in your Sql Datasource Parameter Collection, thus you cannot assign a new value to it.

As to add a new value into a parameter collection, i think your origional way is correct ( SqlDataSource.UpdateParameters.Add("@.parametername",SqlDBType.NVChar,na) ). I cannot see any errors in the code you origionally posted. Have you tried the methodMotley suggested? That maybe is able to solve your problems. thanks

problem with update command

Hi,

i try to update field 'name' (nvarchar(5) in sql server) of table 'mytable'.
This happens in event DetailsView1_ItemUpdating with my own code.
It works without parameters (i know, bad way) like this:
SqlDataSource1.UpdateCommand = "UPDATE mytable set name= '" & na & "'"

But when using parameters like here below, i get the error:
"Input string was not in a correct format"

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
DetailsView1.ItemUpdating
SqlDataSource1.UpdateCommand = "UPDATE mytable set name= @.myname"
SqlDataSource1.UpdateParameters.Add("@.myname", SqlDbType.NVarChar, na)
SqlDataSource1.Update()
End Sub

I don't see what's wrong here.
Thanks for help
Tartuffe

Hi There,

Try exclude the'@.' in your add("@.myname"...

Example:

SqlDataSource1.UpdateCommand = "UPDATE mytable set name= @.myname"
SqlDataSource1.UpdateParameters.Add("myname", SqlDbType.NVarChar, na)

Hope it helps!

|||

thank you, that's the solution.