Saturday, February 25, 2012

access Sql from asp.net

hi,

i have few forms in my form.i dont want to repeat coding for database connectivity.i want to make it as standard in one file.so that in future if any changes in database i can access that file and modify it.

im using global.aspx file here

Sub Session_Start(ByVal senderAsObject,ByVal eAs EventArgs)

' Fires when the session is started

Session("ConnectionPath") ="DRIVER={SQL server};SERVER=localhost;UID=sa;PWD=;DATABASE=mas;"

EndSub

now in my forms how can i access this connection path without repeatin the above code again in my dim con = new sql(...) i dont want to do like this..

You need to use the web.config...

add this

<?xmlversion="1.0"?>
<configurationxmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<addname="ConnectionString"connectionString="Data Source=<SERVERNAME>;Initial Catalog=<DATABASENAME>;UID=<USERNAME>;Password=<PASSWORD>;"providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
......
</system.web>
</configuration>

Then in you code behind do this to access is...

Dim conn as SqlConnection =New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


|||

Or better you create a public shared method and create it there.

|||

Error 101 Value of type 'System.Configuration.ConnectionStringSettingsCollection' cannot be converted to 'String'.

im getting this error

|||

Since you have added the connection string in session variable, use the below code to access it Dim conn as SqlConnection = New SqlConnection(session("ConnectionPath"))

|||

hi, thankyou,i manged to solve it with all ur advices...

i have a doubt in procedure.is it possible to retive value of a column from a table inside procedure?someth like this

cREATE PROCEDURE a @.val varchar(50)
AS
declare @.type char(2)
set @.type='select id from dbo.Handle_Master where name = @.val'
if @.type='A'
Begin .....

if @.Type='B'

Begin...

someth like this...is that possible??if can!! how ?i tried like this but no error and no result also...

|||

wat i felt here is instead of accesing from table ,i planned to pass the value as parameter...jus changed the logic...its workin fine...

No comments:

Post a Comment