Web Forms :: "using" Statement Always Be Used When Creating A Connection To And Accessing Or Saving?
Jan 9, 2011
I have the following code in vb codebehind for updating, inserting, and reading from an sqldatabase using the "Using" statement. I have noticed in some forums where instead of using "Using Objconnectin as New SqlConnection", a plain declaration such as "Dim objConnection as New SqlConnection" is used. What is the difference in just declaring them and using the "Using" statement. Also, are the following approaches correct. First the DataReader:
What is the most correct way to save your connection string when using the n-tier approach ? Right now I have two windows app and one web app using my DLL library, what I'm currently doing is to create a static method inside my dal "GLOBALDAL" inside I'm making this check:
Code: if (System.Web.HttpContext.Current != null) ... if httpContext.Current is null That's mean that one of my windows applications doing the request, if so I get the CN from a file otherwise it's mean its the website knocking on my door so I get the CN from: Code: System.Web.HttpContext.Current.Application["ConnectionString"] I am thinking of changing it all and just set properly in my DA layer that expose the CN as a string. But I fear of security issues. So what is the correct way?
I have the following code that creates a new Excel file in my C# code behind. When I attempt to save the file I would like the user to select the location of the save.
In Method #1, I can save the file my using the workbook SaveCopyAs without prompting the user for a location. This saves one file to the C:Temp directory.
Method #2 will save the file in my UsersDocuments folder, then prompt the user to select the location and save a second copy. How can I eliminate the first copy from saving in the UsersDocuments folder?
currently i have db name , server name & coonection credentials for sql daTAbase hardcoded in webconfig but i need to create a dynamic connection string in which db name will change currenly i have a main .aspx with a button & on its click event i get the connection string frm webconfig and move on to next page
wht i need is in the main page i need to populate all the db of a particullar server in a dropdown the user will first select the db frm drpdown & click the button & with the db name frm dropdown connection string shld be formed
I have a single column returning SQL data source and I want to use the data for a simple if statement, but for the live of me I can't figure out how to do it. *oh for the old days, I wrote the same thing in simple asp in about 45 seconds*
[code]....
For the life of me I can't figure out how to get that data into a variable or any way to access it. I know its probably simple. I have tried functions, etc off the net but everything I try results in another error.
I have a gridview control that displays expenses claims received from clients. I have a template field that contains a checkbox - if this is ticked, a value from the gridview is added to a variable and this is outputted to a label control.
What I then want to do is to update the database according to which row has been ticked. I have so far the following code that adds each value to an array, which I was then hoping to access in the sql query i.e. Update table set value = value where id = (id's added to array)..
Code as follows - so far I'm just displaying the count of values in the array to make sure they are actually being added:
I have heard that saving connection strings and stored procedure names in web.config file of our application is not safe. It is a good practice to store the connection string in a config file rather than as a hard coded string in our code and if we need to change it,then it makes our job easier. how to protect our code in web.config?
To give some background details, I have created a website for a friend that has a few pages that require a database in the background. I've used Visual Studio templates for most of it, so there's the standard ASPNETDB.MDF file that's created for users and groups, a login page and a page to view and manage those users etc etc. The login page uses the standard controls for logging in, retrieving lost passwords and so on. Being standard controls, by default they get the connection string from the web.config file. Upon attempting to upload the site to my host (1and1.co.uk in case anyone else has any dealings with them) I discovered that storing the connectionstring doesn't work. 1and1 support say that the sql databases can only be accessed using scripts like this (this is an example of theirs):
<html> <title>Queries from the MS-SQL database with ASP</title> <body bgcolor="FFFFFF"> <h2>Query from table <b>products</b> with ASP</h2> <% Set conn = Server.CreateObject("ADODB.Connection") conn.open "PROVIDER=SQLOLEDB;DATA SOURCE=mssqxxx.1and1.com;UID=dboxxxxxxxxx;PWD=xxxxxxxx;DATABASE=dbxxxxxxxxx" 'This code block will create a recordset Set rs = Server.CreateObject("ADODB.Recordset") SQL = "select * from products" rs.open SQL, conn 'will iterate to display the records got from the database While Not rs.EOF response.write(rs("id") & " " & rs("price")) rs.MoveNext Wend 'closes the connection rs.close conn.close Set rs = Nothing Set conn = Nothing %> </body> </html>
So my question, is it possible to get the standard asp.net controls to access a database through a script like this?
I've created 2 multi dimensional arrays (2 dimensions each), I want to feed them both into a function, and I would like the function to spit out an array or list containing both of them as arrays, so I can use them as they were before they went into the function.
The way I've done it at the moment is probably very inifficient and I'm having trouble accessing it properly.
What follows is my attempt to put the 2 arrays (attackerarmy and defenderarmy) into a new array called armyresults and adding an additional dimension to tell me if the record belongs to the attacker or defender:
[Code]....
Since I've already got the 2 arrays I want in the function, I've rather not inefficiently force them into a new array, only to try to re build them again when they come out of the function.
how to out 2 arrays of unknown size into an array of arrays for the return statement of my function, and then how I can access those individual arrays so I can easily copy them back out into their original form?
I am currently working on the connection between ASP.NET and Oracle. I tried to search over the net and know there are many ways. Is there any 'normal practice' on which method to be used? Or it depends?
I am currently using datasets for my data access layer. I currently store the connection string in the web.config file. I need the ability to change the connection to another database before any queries are processed. Is there an event that is triggered or a base class that can be modified that intercepts the process of retrieving the connection string from the web.config file? Is there any way to handle multiple database connections using the same code base and also take advantage of the connection pooling? I understand the best method is to get rid of datasets and use custom data objects.
I am having problem when i was trying to attach .mdf file while create a new connection of SQL server, create a new connection of SQL database file and open the database in Server Explorer. I am using VS 2010 Ultimate and SQL Express 2008 R2.
Here are the error message.
1. Attach .mdf in SQL database file and open the database in Server Explorer
However when i try to retreive it and use it i get this error: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I'm using Visual Studio 2008, and my database is SQL Server 2000.
I want to add a connection to the Server Explorer in VS. The Data source is Microsoft SQL Server (SqlClient). After entering in all my information and I click Test Connection, it is successful.
But when I click OK, I get the error:
Unable to add data connection. ExecuteScalar requires an open and available connection. The connection's current state is closed.
i am trying to create connection using OLEDB connection in my app. but i am not able to create the connection as in datasource i want to use Server.mappath, but cudn't find the right method to use it. i am trying to make connection with Access database file. following is code i have tried:
string path = Server.MapPath("~/uploadaccess/Production.mdb"); string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("~/uploadaccess/Production.mdb")&";"; and also OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path&";"); and tried this OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~/uploadaccess/Production.mdb"));
and this is the error i am getting:
Operator '&' cannot be applied to operands of type 'string' and 'string'
just for my testing purpose i know i can define both the connection's outside in a single web config file by different name's and access them in my front end according to it but what if i want to have seprate for both connection's web.config situation is like this see image so i want to access my connections from second web config file how i can do that.
I previously used a datasource and a connection string to connect to my database on sql server and all orked fine. The connection string was saved in my web.config file and is: ....
I am using VS2008 and oracle 10g ODP.Net. The oracle database that I used in the connection is working and tested it using sql plus.The connection string when used in a GriDView works perfectly. Now when I created a new listview control with the same connection it gives an error.Database schema could not be retrieved for this connection. Please make sure connection settings are correct and the database is online
Object reference not set to an instance of an object at VSDataObjectSupport (478,6)
have a very important issue,i have three Stored Procedures Sp1,Sp2 and Sp3 .the first one (Sp1) will execute the second one (Sp2) and save returned data into @tempTB1 and the Second one will execute the third one (Sp3) and save data into @tempTB2.if I execute the Sp2 it will works and it will returned me all my data from the Sp3 ,but the problem is in the Sp1, when i execute it it will display this Error:INSERT EXEC statement cannot be nested I tried to change the place of execute Sp2 and it display me another error:Cannot use the ROLLBACK statement within an INSERT-EXEC statement.