I've installed Oracle DB on another pc(A) and trying to establish the connection from my pc(B). Now when I'm trying to debug the application I get this error- "[DBNETLIB][ConnectionOpen (ParseConnectParams()).]Invalid connection."
public void InsertionTest()
{
string cmdText = #"Insert into O_TEST_TABLE (ID,Name,MOBILE) Values (4, 'rD','798984');";
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["HR"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(ConnectionString))// "Provider=SQLOLEDB; DATA SOURCE=172.16.1.220:1521/orcl;PASSWORD=hr;PERSIST SECURITY INFO=True;USER ID=hr"))
using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
{
con.Open();
int rowsUpdated = cmd.ExecuteNonQuery();
if (rowsUpdated == 0)
{
Console.WriteLine("Failed!!!");
}
else
{
Console.WriteLine("Success.");
}
}
}
In my web config file, the connection string is-
<connectionStrings>
<add name="HR" connectionString="Provider=SQLOLEDB;Initial Catalog=HR;DATA SOURCE=172.16.1.13:1521/orcl;PASSWORD=hr;PERSIST SECURITY INFO=True;USER ID=hr; OLEDB.NET=True " />
</connectionStrings>
I feel provider should be Provider=OraOLEDB.Oracle
Related
public static DataSet ParseDatabaseData(string sheetName)
{
string connectionString = "Provider=System.Data.SqlClient;Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;";
SqlConnection conn = new SqlConnection(connectionString);
string strSQL = "SELECT * FROM [" + sheetName + "$]";
SqlCommand cmd = new SqlCommand(strSQL, conn);
conn.Open();
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
conn.Close();
return dataset;
}
The error show that 'provider' keyword is wrong.
Please help me to correct how to connect with Database through connection string?
You do not need to specify the Provider in the Connection String.
Try it like this:
public static DataSet ParseDatabaseData(string sheetName)
{
string connectionString = "Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;";
Instead of mentioning the connection string in the individual file itself, you can place the connection string in the web.config or app.config and use the config where ever required.
Sample for web.config place the connection string under the <configuration>, there you can provide the provider name:
<configuration>
<connectionStrings>
<add name="ConnString"
connectionString="Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
and inside the file
public static DataSet ParseDatabaseData(string sheetName)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
Note: add using System.Configuration; for the ConfigurationManager.
I want to save checkboxlist data in a database. I created a table in SQL named 'tblSubject' and made connectionstring in web.config. However I still get the erorr :
NullReferenceException was unhandled by user code
object reference not set to an instance of an object.
This is the code in c#:
private void PopulateSubjects()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from subjects";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["Subject"].ToString();
item.Value = sdr["Id"].ToString();
item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
chbox.Items.Add(item);
}
}
conn.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update subjects set IsSelected = #IsSelected" +
" where Id=#Id";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chbox.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#IsSelected", item.Selected);
cmd.Parameters.AddWithValue("#Id", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
And in Web.config:
<connectionStrings>
<add name=" constr" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;
Initial Catalog = dbtblSubject; Integrated Security = true" providerName="System.Data.SqlClient" />
</connectionStrings>
Any help would be much appreciated.
Remove the white space in Web.config:
<add name=" constr" ...
To
<add name="constr" ...
First of all, you should look at your stack trace for where the nullreference occurs.
It looks like you're on the right track, thinking that the connection string is the cause of this exception. If you look at your Web.config, the name of the connection string is " constr", with an extra space in the start. This does not match your code:
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
Remove the first space in the connection string name in Web.Config, and your code will probably work.
I want to backup my DB but I got a error:
ConnectionStrings cannot be used like a method
How can I resolve this?
string strCon = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
string sSQL = "BACKUP DATABASE Database TO DISK = 'D:\\Database.bak';";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.Connectionstrings(strCon).ConnectionString))
{
SqlCommand command = new SqlCommand(sSQL, connection);
connection.Open();
command.ExecuteNonQuery();
}
You should directly use that variable as SqlConnection requires a string object containing a connection string and you are storing it in a string object itself.
So it would be simply like this:
using (SqlConnection connection = new SqlConnection(strCon))
{
SqlCommand command = new SqlCommand(sSQL, connection);
connection.Open();
command.ExecuteNonQuery();
}
Recommended: (to store it in Web.config)
<connectionStrings>
<add name="job" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Then access it like this: (using System.Configuration;)
ConfigurationManager.ConnectionStrings["job"].ConnectionString
ConnectionStrings is a collection. It must be used like:
ConfigurationManager.Connectionstrings[0].ConnectionString
please excuse me for asking a primary question. I need to connect to this link http://sunspares.millenniumit.com/phpmyadmin/ using c#. It is of MySQL and i need to access a table called 'spares' in the 'inventory' database. Can anyone help me to initialize this connection using C#. I found the below coding but i'm confused how to provide the exact server that i need to be connected as well as to the table
con = mysql_connect("localhost","username","password");
Install the mysql connector/net.
Create a new project.
Add reference to: MySql.Data.
Add using MySql.Data.MySqlClient;.
Add the following code to your application:
private void button1_Click(object sender, System.EventArgs e)
{
string MyConString = #"SERVER=localhost;
DATABASE=mydatabase;
UID=testuser;
PASSWORD=testpassword;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from mycustomers";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string thisrow = "";
for (int i= 0;i<Reader.FieldCount;i++)
thisrow+=Reader.GetValue(i).ToString() + ",";
listBox1.Items.Add(thisrow);
}
connection.Close();
}
<connectionStrings>
<add name="MySQLConnectionString" connectionString="server=localhost;User Id=root;pwd=;database=data1;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
-----------------------
using MySql.Data.MySqlClient;
...
MySqlConnection Conn = new MySqlConnection();
Conn = new MySqlConnection(ConnStr);
I'm pretty new in .net, I hope this question will not sound stupid. How can I do the following sql script to the database in a webmethod in .net c#?
Under Web.config I have code the following code for connecting to the database:
<connectionStrings>
<add name="TestConnectionString" connectionString="Data Source=TXT-TEST-SQL-02;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
How can I do under test.asmx.cs - webmethod to run the following script from the database to retrieved the data from the table?
[WebMethod]
public string testSearch(int id)
{
return result;
}
SELECT name
FROM Customer
WHERE customer_id = id
Try
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
and
[WebMethod]
public string testSearch(int id)
{
string connString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
String sql = "SELECT name FROM Customer WHERE customer_id = #id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("#id", SqlDbType.Int);
cmd.Parameters["#id"].Value = id;
String result = "";
try
{
conn.Open();
result = (string)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
}
}