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.
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.
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.
... 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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I'll try that!
ASKER
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(Configuratio nSettings. AppSetting s.GetValue s(0).GetVa lue(0).ToS tring());
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:
Had to use the following connection string:
public SqlConnection sCon = new SqlConnection(Configuratio
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");
Hopefully you will find this useful
http://msdn2.microsoft.com/en-us/library/ms998292.aspx
Open in new window