'Unicode' is an invalid connection string attribute Asp.net - 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.

Related

ConfigurationManager doesnt recognize my connection string

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"/>

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();

Keyword not supported: 'server(local); database'

my web.config is
<connectionStrings>
<add name="taraznegarConnectionString" connectionString="Data Source=.;Initial Catalog=taraznegar;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
and this is my code:
namespace taraz
{
public class DAL
{
string connection = "Data Source=.;Initial Catalog=taraznegar;Integrated Security=True";
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
public DAL()
{
con = new SqlConnection();
con.ConnectionString = connection;
cmd = new SqlCommand();
cmd.Connection = con;
da = new SqlDataAdapter();
da.SelectCommand=cmd;
dt = new DataTable();
}
public DataTable ExecuteReader(string SQL)
{
connect();
da.SelectCommand.Connection = con;
da.SelectCommand.CommandText = SQL;
try
{
da.SelectCommand.ExecuteReader();
}
catch
{
da = null;
}
disconnect();
da.Fill(dt);
return dt;
}
public string ExcuteNonQuery(string SQL)
{
string result=null ;
cmd.CommandText = SQL;
connect();
try
{
cmd.ExecuteNonQuery();
}
catch { result = "خطا"; }
disconnect();
return result;
}
public string ExecuteScalare(string sql)
{
string result = null;
cmd.CommandText = sql;
connect();
try {
result = cmd.ExecuteScalar().ToString();
}
catch { result = "خطا"; }
disconnect();
return result;
}
void connect()
{
if (con.State == ConnectionState.Closed)
con.Open();
}
void disconnect()
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
}
and when I'm using this class in my project the error is:
Server Error in '/' Application.
Keyword not supported: 'server(local); database'.
what's the probleam?
When you have a ConnectionString in your webconfig file then in your application you don't need to create another connection. You should use that connection. In your class DAL you are creating a new connection string. Instead of that you should use something like this-
string connectionString = ConfigurationManager.ConnectionStrings["taraznegarConnectionString"].ConnectionString;
For Entity Framework (database-first or model-first; when you have a physical EDMX model file) you need to use a special type of connection string which is quite different from the straight ADO.NET connection strings everyone else has mentioned so far...
The connection string must look something like:
<connectionStrings>
<add name="testEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;Initial Catalog=taraznegar;Integrated Security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Inside this connection string, you'll find the provider connection string= attribute which is basically your ADO.NET connection string:

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 access connection string Class.cs file?

I am totally new to Windows programming so i don't know how to access connection string in Class.cs file as we can in web application as follow:
public DataTable Client_Login_Check(string Email, string Password)
{
DataTable dt = new DataTable();
try
{
object[] objParam = new object[2];
objParam[0] = Email;
objParam[1] = Password;
return dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["GL"], "GreenLight_Users_LoginCheck", objParam).Tables[0];
}
catch (Exception ex)
{
OutputMessage = ex.Message;
}
return dt;
}
I have added "System.Configuration" namespace...But still i am not getting ConfigurationManager in help. Please help me..
Apart from adding a reference to System.Configuration.dll in the project, you will also need to add a using declaration for the namespace in the file:
using System.Configuration;
Note:
You should be using ConfigurationManager.ConnectionStrings["GL"].ConnectionString and have the connection string in the connectionStrings configuration section, not in the appSettings section.
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager
if you want to use App.config
add these lines in tag
<configuration>
<connectionStrings>
<add name="mystring" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dbName;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="Server" value=".\SQLEXPRESS"/>
<add key="Database" value="dbAIAS"/>
<add key="ClientSettingsProvider.ServiceUri" value=""/>
</appSettings>
<configuration>
in Name.cs file you can access by
string strconn = #"Data Source= " + ConfigurationSettings.AppSettings["Server"].ToString() + "; Initial Catalog= " + ConfigurationSettings.AppSettings["Database"].ToString() + ";Integrated Security=True";

Resources