ConfigurationManager doesnt recognize my connection string - asp.net

I'm trying to do a DropdownList from Database using ASP.Net Web Form.
Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["OraDbContext"].ConnectionString;
OracleCommand cmd = new OracleCommand("SELECT NOMPRE FROM HOPEMPL", conn); //HOPEMPL#HQ
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
DDnompre.DataSource = ds;
DDnompre.DataTextField = "NOMPRE";
DDnompre.DataValueField = "NOMPRE";
DDnompre.DataBind();
}
Connection String:
<connectionStrings>
<add name="OraDbContext" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=localhost:1521/xe;DBA PRIVILEGE=;PASSWORD=1234;USER ID=LOUG"" providerName="System.Data.EntityClient" />
</connectionStrings>
Error:
"System.ArgumentException : ''metadata' is not a valid connection
string attribute' "
I'm also working with a database first (EF)

try with :
<add name="{ConnectionName}"
connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;"
providerName="Oracle.DataAccess.Client"/>

Related

SqlException after moving connection string to web.config

I have to change my data source to the name of my server [server name]\SQLExpress after moving my connection string from Page_Load to web.config. Otherwise I get an SqlException at con.Open(). The app works fine both ways am just wondering why I need to change the datasource name after moving the connection string.
<connectionStrings>
<add name="CS"
connectionString="data source=[server name]\SQLEXPRESS; Initial Catalog = SampleDb; integrated security = true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
(string cs = "data source=.\\SQLExpress; initial catalog = SampleDb; integrated security= true";)-moved to web.config
string CS = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
//pass the connection string to the SqlConnection() constructor
SqlConnection con = new SqlConnection(CS);
//tell sqlcommand what query to execute and which connection
SqlCommand cmd = new SqlCommand("select * from Electronics", con);
//open connection
con.Open();
//execute the command
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
//close the connection
con.Close();

Database is not connected from webconfig file

The below code is during page load event in aspx.cs file. When I load the page in chrome, it throws some error in cs file.
Stack trace error:Network related error while connecting to sql server:
protected void Page_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM employee"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
listviewmain.DataBind();
}
}
}
}
Connection string in web.config file:
<add name="constr"
connectionString = "data source=.;initial catalog=[Analyst_RealEstate_WebCP];persist security info=True;user id=sa;password=pass#word1;multipleactiveresultsets=True;"
providerName="System.Data.SqlClient"/>
Sql Connection :
When I am trying to connect in sql using these credentials, I am able to login but not through web.config file

'Unicode' is an invalid connection string attribute Asp.net

My code have error ---- 'Unicode' is an invalid connection string attribute
Web Config :
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=XE;Persist Security Info=True;User ID=****;Password=****;Unicode=True"
providerName="System.Data.OracleClient" /> </connectionStrings>
Asp.net:
public DataTable Bankomat(DataTable dt)
{
using (con = new OracleConnection())
{
using (cmd = new OracleCommand())
{
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString();
cmd.Connection = con;
cmd.CommandText = "select * from bankomat";
con.Open();
dt.Load(cmd.ExecuteReader());
con.Close();
}
}
return dt;
}
In the connection string
connectionString="Data Source=XE;Persist Security Info=True;User ID=****;Password=****;Unicode=True"
the Unicode=True is belong to System.Data.OracleClient and not to Oracle.DataAccess.Client
So just remove it.

Object reference not set to an instance of an object. on my string

hi I have this error" Object reference not set to an instance of an object." In my string constr.
what does it mean?
thanks for answering =)
private void BindGrid()
{
string constr = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString; //Error here
sql = "select * from LogDetails";
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataTable t = new DataTable();
sda.Fill(t);
GridView1.DataSource = t;
GridView1.DataBind();
}
You need to add connection string in your web.config file.
<connectionStrings>
<add name="conString" connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;UserID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

How to use created connection string of Web.Config in my C# asp.net 4.0 project?

Actually I am new in this topic so required some help.
I have added connection string in Web.Config
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
and know that, to use it I have to put this statement in my C# code behind
string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
That's all I know.
My Question is
What should I do if I want to execute some query for my aspnetdb.mdf dataabase (Built in db of ASP.NET built in login contols in Visual Studio 2010)
Earlier, I was doing this to accomplish my task
1) No connection string in Web.Config. and
2) Hard code in codebehind
SqlConnection con = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true");
SqlCommand cmd = new SqlCommand();
protected void btnnameedit_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.CommandText = "update tamhankarnikhil set fname = '" + fname.Text + "'";
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
fname.Text = "";
}
catch (Exception a)
{
Response.Write(a.Message);
}
}
Here's what you could do:
protected void btnnameedit_Click(object sender, EventArgs e)
{
try
{
string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
using (var conn = new SqlConnection(connStr))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "UPDATE tamhankarnikhil SET fname = #fname";
cmd.Parameters.AddWithValue("#fname", fname.Text);
cmd.ExecuteNonQuery();
fname.Text = "";
}
}
catch (Exception a)
{
Response.Write(a.Message);
}
}
You will notice the usage of parametrized queries to avoid SQL injection to which your code was vulnerable to due to the string concatenations you were using when constructing the SQL query.
You will also notice that the SqlConnection and SqlCommand are wrapped in using statements to ensure their proper disposal even in the event of an exception.

Resources