Link to home
Start Free TrialLog in
Avatar of victornegri
victornegri

asked on

Connect to MSSQL db using C#

Using Visual Web Developer 2005 (C#) to create an ASP 2.0 web form.

I've created a data grid on the page that displays the rows of a table and as a result it has created a connection string to my MSSQL2000 db in web.config.

I want to be able to create my own functions in the .aspx.cs file with their own SQL statements (for UPDATE/INSERT, etc) but I don't know how to reference the connection string in web.config to do that. Can someone help?

If you need me to be more specific, I'll try my best. I'm pretty new to .NET.
Avatar of RedKelvin
RedKelvin

Hi

Hopefully you will find this useful
http://msdn2.microsoft.com/en-us/library/ms998292.aspx


<connectionStrings>
  <add name="MyDbConn1" 
       connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>
  <add name="MyDbConn2" 
      connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"/>
</connectionStrings> 

Open in new window

Avatar of victornegri

ASKER

Unfortunately I can't use Windows Authentication to connect to the SQL server.

... and I already have the connection strings in my web.config file. I'm wondering how I can reference them in my .aspx.cs file so I can create functions that can do Update/Insert/Select queries on specific tables.
You can reference it as:
string connStr = ConfigurationManager.ConnectionStrings("[connectionstringname]").ConnectionString;
 
sqconn = New SqlConnection(connStr);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Munawar Hussain
Munawar Hussain
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I'll try that!
OK. Got it working... somehow. My main problem was that I didn't have the using System.Data.SqlClient; statement.

Had to use the following connection string:
public SqlConnection sCon = new SqlConnection(ConfigurationSettings.AppSettings.GetValues(0).GetValue(0).ToString());
Does anyone see anything wrong with this statement? I put it outside of the function so that I could use it in different functions.

When I used the connection strings supplied above, they didnt' work. I got different errors. (i.e. "The ConnectionString property has not been initialized." or something about using a property as a method).

The rest of my code is as follows. Please comment on my approach and let me know if I'm doing something stupid:

SqlCommand cmd_Fill_pnlList = new SqlCommand();
 
            cmd_Fill_pnlList.Connection = sCon;
            cmd_Fill_pnlList.CommandText = String.Format("SELECT * FROM tblParts WHERE PartNo = '{0}'",lblPartNoHidden.Text);
 
            SqlDataAdapter adp_Fill_pnlList = new SqlDataAdapter();
            adp_Fill_pnlList.SelectCommand = cmd_Fill_pnlList;
 
            DataSet ds_Fill_pnlList = new DataSet();
            adp_Fill_pnlList.Fill(ds_Fill_pnlList, "tblFill_pnlList");

Open in new window