-
Database connection issue
can someone tell me ???. Why the following code is not working ???. let me know some idea.
Code:
private bool OpenConnection(ref OleDbConnection conn)
{
try
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
if (conn.State!=conn.Open )
{
MessageBox.Show("Connection is Not Open");
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
-
Re: Database connection issue
vb Code:
if (conn.State!=conn.Open )
Should be:
vb Code:
if (conn.State!=ConnectionState.Open )
-
Re: Database connection issue
That should be
Code:
if (con.State != ConnectionState.Open)
{
}
-
Re: Database connection issue
Still not working .it says The name 'ConnectionState' does not exist in the current context.here is the following code.
Code:
private bool OpenConnection(ref OleDbConnection conn)
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
if (conn.State!=ConnectionState.Open )
{
MessageBox.Show("Connection is Not Open");
return false;
}
else
{
return true;
}
}
}
}
-
Re: Database connection issue
You are missing namespace System.Data
Code:
if (con.State != System.Data.ConnectionState.Open )
-
Re: Database connection issue
-
Re: Database connection issue
Quote:
Originally Posted by
firoz.raj
already using System.Data.OleDb ; is included at the very bigining of code window.
additional why the following code is not working ???
Code:
OleDbConnection conn=new OleDbConnection();
Not working means ?
-
Re: [RESOLVED] Database connection issue
here is a better and more elegant code:
Code:
private bool OpenConnection(ref OleDbConnection conn)
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
return conn.State == ConnectionState.Open;
}
shorter lines of code and easy to read.
-
Re: [RESOLVED] Database connection issue
Quote:
here is a better and more elegant code:
why the following code is not working ???.i simple want to fill my dataset.after filliling the listbox.let me know some idea.
Code:
private void BtFillListBox_Click(object sender, EventArgs e)
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from employees";
DataSet ds = new DataSet("Employees");
OleDbDataAdapter MyAdapter = new OleDbDataAdapter();
// MyAdapter.SelectCommand = OleDbCommand;
MyAdapter.Fill(ds, "Employees");
}
-
Re: [RESOLVED] Database connection issue
when you say it doesnt work, what happens?
you commented out the MyAdapter.SelectCommand. this is incorrect, the DataAdapter needs this to execute the select command to fill the dataset. this select command is stored in the object you created called cmd.
MyAdapter.SelectCommand = cmd;
-
Re: [RESOLVED] Database connection issue
Still i am getting error .Select Command.Connection Property has not been initialized.here is the following code.what i have written.let me know please .
Code:
using System;
using System.Data.OleDb ;
using System.Windows.Forms;
using System.Data;
namespace MakeConnection
{
public partial class Form1 : Form
{
private OleDbConnection conn = new OleDbConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
filllistbox();
}
public bool OpenConnection()
{
try
{
// OleDbConnection conn = new OleDbConnection();
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_table.mdb");
conn.Open();
if (conn.State!=ConnectionState.Open )
{
MessageBox.Show("Connection is Not Open");
return false;
}
else
{
return conn.State == ConnectionState.Open;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
private void CloseConnection()
{
try
{
if (conn.State == ConnectionState.Open )
{
conn.Close() ;
}
conn.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}
}
private void filllistbox()
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from employees";
OleDbDataAdapter dataloader = new OleDbDataAdapter();
dataloader.SelectCommand = cmd;
DataSet ds = new DataSet("Employees");
dataloader.Fill(ds, "Employees");
}
}
}
-
Re: Database connection issue
you should read the MSDN Documentation on how to use the DataAdapter classes.
you need to assign, as it says, the connection property of the select command.
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from employees";
cmd.Connection = this.conn;
-
Re: Database connection issue
Quote:
you should read the MSDN Documentation on how to use the DataAdapter classes.
you need to assign, as it says, the connection property of the select command.
still i am getting error .when i tried to run.additional what is the uses of this.conn.here is it this Pointer ???.when i run using f8. i got the the following error.
The SelectCommand property has not been initialized before calling 'Fill'.
Code:
private void filllistbox()
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from employees";
cmd.Connection =this.conn ;
OleDbDataAdapter dataloader = new OleDbDataAdapter();
// dataloader.SelectCommand = cmd;
DataSet ds = new DataSet("Employees");
dataloader.Fill(ds, "Employees");
}
-
Re: Database connection issue
I had stated the error previously and corrected the code :)
-
Re: Database connection issue
how should i fill listbox ???.let me know please.any help would be highly appreciated.
Code:
private void filllistbox()
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from employees";
cmd.Connection =this.conn ;
OleDbDataAdapter dataloader = new OleDbDataAdapter();
// dataloader.SelectCommand = cmd;
DataSet ds = new DataSet("Employees");
dataloader.Fill(ds, "Employees");
ListBox lbox = new ListBox();
}
-
Re: Database connection issue
not sure why you are creating a listbox control in code unless you are going to be adding it to the form dynamically?
simply set the datasource, datamember properties on the listbox in question to the dataset/datatable
-
Re: Database connection issue
Quote:
simply set the datasource, datamember properties on the listbox in question to the dataset/datatable
still i am getting error at this line For Each row As DataRow In ds.Tables(0).Rows.let me know someone please.here is the following code.what i have written.
Code:
private void filllistbox()
{
if (!OpenConnection())
{
MessageBox.Show("Connection is Not Open");
this.Dispose(true);
}
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from employees";
cmd.Connection =this.conn ;
OleDbDataAdapter dataloader = new OleDbDataAdapter();
dataloader.SelectCommand = cmd;
DataSet ds = new DataSet("Employees");
dataloader.Fill(ds, "Employees");
For Each row As DataRow In ds.Tables(0).Rows
{
ListBox1.Items.Add(row("Name").ToString);
}
}
-
Re: Database connection issue
Don't just tell us that there's an error. If there's an error then there's an error message. The IDE gives it to you to help you solve the problem. Logic dictates that, if you want us to help you solve the problem, you give it to us. You've already been prompted to do that at least twice in this thread alone.
-
Re: Database connection issue
the code you have highlighted you are using is VB.NET. but you are using C#. these are 2 different languages. you need to convert your code to C# equivelent.
foreach(DataRow currentRow in ds.Tables[0].Rows)
{
...
}
-
Re: Database connection issue
still i am getting one error .'System.Data.DataRow' is a 'type' but is used like a 'variable'.datarow is a type means let me clarify please.
-
Re: Database connection issue
try to read the error message and understand what it means. your syntax is also incorrect. Are you using Visual Studio IDE? if so, then there is no excuse or any reason why your syntax should be incorrect
-
Re: Database connection issue
listBox1.Items.Add(DataRow("Name").tostring);
should be
listBox1.Items.Add(currentRow["Name"].tostring);
-
Re: Database connection issue
incorrect
take a look at your syntax.
-
Re: Database connection issue
Quote:
Are you using Visual Studio IDE? if so, then there is no excuse or any reason why your syntax should be incorrect
how many visual IDE Is Available?.additional when i tried to run the following code .i am still getting error. 'currentRow' is a 'variable' but is used like a 'method'
-
Re: Database connection issue
First up, in post #22 you were told that you need to change DataRow("Name") to currentRow["Name"] and you've instead changed it to currentRow("Name"). If you aren't going to follow the advice that people provide then there's not much point asking for it.
You've also been told MANY times that C# is a case-sensitive language and you still choose to ignore that. There is no method named "tostring". There is, however, a method named "ToString". Finally, while VB allows you to omit the parentheses on methods if the argument list is empty, C# requires ALL method calls to include parentheses.
-
Re: Database connection issue
Quote:
Originally Posted by
Techno
incorrect
take a look at your syntax.
i was hoping he'd be able to guess the tostring bit himself lol
-
Re: Database connection issue
yes now the code is working fine.additional can you tell me what is the uses of this keyword here??? .is it this pointer like c++??.let me clarify please.
Code:
cmd.Connection =this.conn ;
-
Re: Database connection issue
-
Re: Database connection issue
-
Re: Database connection issue
.as we know executeNonQuery Function returns No of Rows Affected.in the following code i want to see.how much record has beed deleted.but i am getting error.cannot convert from 'method group ' to 'string'.let me know please.
Code:
private void Del()
{
Int64 d;
OleDbCommand cmd=new OleDbCommand();
cmd.Connection =this.conn;
cmd.CommandText ="DELETE CDs.ArtistName FROM CDs";
:)
MessageBox.Show(d.ToString );
}
private void Delete_Click(object sender, EventArgs e)
{
Del();
}
-
Re: Database connection issue
As I have already stated, C# requires you to include parentheses on every method call, whether you're passing parameters or not. ToString is a method. You can't call it without parentheses.
-
Re: Database connection issue
It doesn't matter anyways, since the command doesn't get executed... the code as it stands will simply display "0" in the messagebox.
-tg
-
Re: Database connection issue
Quote:
It doesn't matter anyways, since the command doesn't get executed... the code as it stands will simply display "0" in the messagebox.
it is working nice .what is a issue in the above code ???.Additional i have tried to insert textbox data into the listbox.but getting error at red line.let me know please.[Code]private void BtAdd_Click(object sender, EventArgs e)
-
Re: Database connection issue
CommandText requires a string.
you arent providing a string.
-
Re: Database connection issue
Still i am getting expected error ;.at the following line.Kindly let me know the idea
-
Re: Database connection issue
Quote:
Originally Posted by
firoz.raj
Still i am getting expected error ;.at the following line.Kindly let me know the idea.
Code:
cmd.CommandText ="insert into CDs(ArtistName )values ('"txtArtistName"')";
Code:
cmd.CommandText ="insert into CDs(ArtistName) Values ('"txtArtistName.Text"')";
-
Re: Database connection issue
still i am getting ; Expected Error.at the following line .
Code:
cmd.CommandText ="insert into CDs(ArtistName )values ('"txtArtistName.text"')";
-
Re: Database connection issue
because you need to add the value of txtArtistName.Text into the string.
cmd.CommandText ="insert into CDs(ArtistName )values ('" + txtArtistName.text + "')";
for best practice you should be using StringBuilder to concatinate string as well as using SPROCS (Stored Procedures) for data insertion, deletion and retrieval for security and performance
-
Re: Database connection issue
Well.Can SomeOne Tell me ?How should i refresh the list Box.after adding data
it should immediately go in the listbox.let me know please.
Code:
private void BtAdd_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = this._conn;
cmd.CommandText ="insert into CDs(ArtistName )values ('" + txtArtistName.Text + "')";
cmd.ExecuteNonQuery();
//filllistbox();
}
:wave:
-
Re: Database connection issue
you need to rebind the data. so you need a method which binds the data and call that method.
-
Re: Database connection issue
can you post the code you currently have? it would help
-
Re: Database connection issue
Can You tell me ?How should I write DataBase Path in the settings area af the properties ?let me know please.When tried to Click on settings nothing happens.so let me know How should I assign path there???.
Thx in Advance.
-
Re: Database connection issue
well typically a connectionstring looks like this:
Data Source=computerName\SQLSERVERINSTANCE;Initial Catalog=Databasename;Trusted_Connection=true;
replace data source and initial catalog values appropriately.
if you are NOT using SQL Server Express, then the data source is typically just a local pointer example:
local
(local)
.
if you are using SQL Server Express, then the instance name is SQLExpress:
.\SQLExpress
-
Re: Database connection issue
Quote:
well typically a connectionstring looks like this:
Data Source=computerName\SQLSERVERINSTANCE;Initial Catalog=Databasename;Trusted_Connection=true;
replace data source and initial catalog values appropriately.
if you are NOT using SQL Server Express, then the data source is typically just a local pointer example:
local
(local)
.
if you are using SQL Server Express, then the instance name is SQLExpress:
.\SQLExpress
but i am using DataBase Ms Access 2007.let me know please!!!:D:D:D
-
Re: Database connection issue
sure. in which case the connectionstring differs.
you need to import the System.Data and System.Data.OleDB namespace to gain access to the OleDBConnection and OleDBCommand classes which will do what you pretty much want and operate similarly to the Sql classes.
www.connectionstrings.com contains all the possible connection strings.
http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
-
Re: Database connection issue
I Simple want if specific Text exists in the fields( Identity) in the specific table(IdentityTab). Method needs to return true . otherwise false .i did flow . but I don’t know how should I Achieve the following Method ??? Using C# Code. which I did in Vsd . Using OleDbCommand object and the OleDbDataReader in c#
Code:
public bool Identification(string identifier) {
using (OleDbConnection conn=new OleDbConnection(_path)){
conn.Open();
using (OleDbCommand Cmd = new OleDbCommand(_path)){
Cmd.CommandText = "Select identityNo from identityTab";
how should i check specific text in a fields of Specific Table ???.
if found
return true ;
}
return false;
}
:(
-
Re: Database connection issue
well you are almost there. in this case, you should use oleDbDataReader which is a fast forward only reader. so executeReader on your OleDbCommand, which returns you an OleDbDataReader, then see if there is a record returned by calling it's Read() method, which returns a bool.
an example and documentation of the class:
http://msdn.microsoft.com/en-us/libr...er(VS.71).aspx
-
Re: Database connection issue
I need using vsd logic . let me know . how should I Achieve it ???.How should I check Specific text in the field of the table .:(
-
Re: Database connection issue
looks like homework to me. a link was given which explains how the class works, with an example. you need to read the documentation and understand it to implement your VSD logic.
hint: the reader has an index you can specify to read a field value.
-
Re: Database connection issue
[Quote]looks like homework to me. a link was given which explains how the class works, with an example. you need to read the documentation and understand it to implement your VSD logic.
hint: the reader has an index you can specify to read a field value.
[FONT="Comic Sans MS" How should I check specific Index Using Reader Property ??????????? .[/FONT]:(
-
Re: Database connection issue
by specifying the indexing characters. or you can use GetString method to get a string value of the specified column index.
example:
reader[ColumnIndexHere]
this will read the value of the specified column index
however your code is still wrong. OleDbDataReader is not an instantiable class.
-
Re: Database connection issue
-
Re: Database connection issue
Calm down. You're talking like freaking Yoda, and panicking at the same time and it's not helping the situation.
Think for a moment about the code you posted and why it doesn't work (by the way, that's two trout slaps for not mentioning the error message.)
You pass in a parameter "identifier" ... but then never use it any where. Then you use a variable called "identityNo" which is never defined anywhere. AND you use it as the indexor of the reader array. Then you're comparing what I'm assuming to be a numerical field to the boolean value of true.
-tg
-
Re: Database connection issue
Quote:
you use a variable called "identityNo" which is never defined anywhere. AND you use it as the indexor of the reader array. Then you're comparing what I'm assuming to be a numerical field to the boolean value of true.
I Have tried but still I am getting the same error !!!.
Code:
public bool Identification(string identifier){
using (OleDbConnection conn = new OleDbConnection(_path)){
conn.Open();
using (OleDbCommand Cmd = new OleDbCommand(_path)){
Cmd.CommandText = "Select identityNo from identityTab where identityTab.IdentityNo="+identifier ;
return true;
}
return false;
}
}
:(
-
Re: Database connection issue
1) Stop yelling... that's not going to accomplish anything other than offending those who are doing their best to help.
2) what is _path? I'm guessing that it is your connection string, right? Then you pass it to your connection (which you are - good) but you do NOT pass it to your command object.
3) The only things you should be passing to your command object is your connection and your sql statement. at a minimum, you should be passing the connection (but not the _path).
4) You've created the command object set the command text, but never execute it.
5) You may want to consider reading through our Database FAQs and Tutorials there's some good stuff in there, including the basics of how to talk to your database. And how to use parameters too.
-tg
-
Re: Database connection issue
At first Identifier is a text .when I write single quote .i am getting error Newline is Constant !!!.
Cmd.CommandText = "Select identityNo from identityTab where identityTab.IdentityNo="' +identifier+ ' " ;:(
-
Re: Database connection issue
Quote:
Originally Posted by
firoz.raj
At first Identifier is a text .when I write single quote .i am getting error Newline is Constant !!!.
Cmd.CommandText = "Select identityNo from identityTab where identityTab.IdentityNo="' +identifier+ ' " ;:(
I see one issue that is very obvious.
Code:
Cmd.CommandText = String.Concat( "Select identityNo from identityTab where identityTab.IdentityNo='", identifier, "'");
This should solve the very obvious issue. Always double check when combining strings if you require an ' remember to have it inside the ". As you can see in your example, neither are within the realm of the quotations and therefore you should be receiving an error.
-
Re: Database connection issue
To be perfectly honest, even the ' isn't necessary. If it's a number, treat it like a number, not a string.
Code:
Cmd.CommandText = String.Concat( "Select identityNo from identityTab where identityTab.IdentityNo=", identifier);
-tg