|
-
Jul 21st, 2010, 04:11 AM
#1
Thread Starter
Frenzied Member
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;
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 21st, 2010, 04:57 AM
#2
Re: Database connection issue
vb Code:
if (conn.State!=conn.Open )
Should be:
vb Code:
if (conn.State!=ConnectionState.Open )
-
Jul 21st, 2010, 04:57 AM
#3
Re: Database connection issue
That should be
Code:
if (con.State != ConnectionState.Open)
{
}
Please mark you thread resolved using the Thread Tools as shown
-
Jul 21st, 2010, 05:12 AM
#4
Thread Starter
Frenzied Member
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;
}
}
}
}
Last edited by firoz.raj; Jul 21st, 2010 at 06:31 AM.
-
Jul 21st, 2010, 05:14 AM
#5
Re: Database connection issue
You are missing namespace System.Data
Code:
if (con.State != System.Data.ConnectionState.Open )
Please mark you thread resolved using the Thread Tools as shown
-
Jul 21st, 2010, 05:27 AM
#6
Thread Starter
Frenzied Member
Re: Database connection issue
Last edited by firoz.raj; Jul 21st, 2010 at 06:31 AM.
-
Jul 21st, 2010, 06:00 AM
#7
Re: Database connection issue
 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 ?
Please mark you thread resolved using the Thread Tools as shown
-
Jul 26th, 2010, 03:57 AM
#8
PowerPoster
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.
-
Jul 26th, 2010, 04:04 AM
#9
Thread Starter
Frenzied Member
Re: [RESOLVED] Database connection issue
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");
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 26th, 2010, 04:06 AM
#10
PowerPoster
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;
-
Jul 26th, 2010, 05:14 AM
#11
Thread Starter
Frenzied Member
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");
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 26th, 2010, 08:10 AM
#12
PowerPoster
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;
-
Jul 28th, 2010, 12:49 AM
#13
Thread Starter
Frenzied Member
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.
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");
}
-
Jul 28th, 2010, 12:51 AM
#14
PowerPoster
Re: Database connection issue
I had stated the error previously and corrected the code
-
Jul 31st, 2010, 08:26 AM
#15
Thread Starter
Frenzied Member
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();
}
-
Jul 31st, 2010, 10:42 AM
#16
PowerPoster
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
-
Aug 2nd, 2010, 12:30 AM
#17
Thread Starter
Frenzied Member
Re: Database connection issue
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);
}
}
-
Aug 2nd, 2010, 01:02 AM
#18
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.
-
Aug 2nd, 2010, 02:09 AM
#19
PowerPoster
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)
{
...
}
-
Aug 2nd, 2010, 03:30 AM
#20
Thread Starter
Frenzied Member
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.
Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.
-
Aug 2nd, 2010, 04:48 AM
#21
PowerPoster
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
-
Aug 2nd, 2010, 10:38 AM
#22
Lively Member
Re: Database connection issue
listBox1.Items.Add(DataRow("Name").tostring);
should be
listBox1.Items.Add(currentRow["Name"].tostring);
-
Aug 2nd, 2010, 12:44 PM
#23
PowerPoster
Re: Database connection issue
incorrect
take a look at your syntax.
-
Aug 2nd, 2010, 11:22 PM
#24
Thread Starter
Frenzied Member
Re: Database connection issue
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'
Last edited by firoz.raj; Aug 22nd, 2010 at 08:31 AM.
-
Aug 2nd, 2010, 11:30 PM
#25
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.
-
Aug 3rd, 2010, 03:44 AM
#26
Lively Member
Re: Database connection issue
 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
-
Aug 3rd, 2010, 05:13 AM
#27
Thread Starter
Frenzied Member
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 ;
-
Aug 3rd, 2010, 05:52 AM
#28
PowerPoster
Re: Database connection issue
-
Aug 3rd, 2010, 11:42 AM
#29
Fanatic Member
Re: Database connection issue
VB -> Me == C# ->this
-
Aug 4th, 2010, 07:45 AM
#30
Thread Starter
Frenzied Member
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();
}
-
Aug 4th, 2010, 07:59 AM
#31
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.
-
Aug 4th, 2010, 08:23 AM
#32
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
-
Aug 9th, 2010, 04:14 AM
#33
Thread Starter
Frenzied Member
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.
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)
Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.
-
Aug 9th, 2010, 04:19 AM
#34
PowerPoster
Re: Database connection issue
CommandText requires a string.
you arent providing a string.
-
Aug 9th, 2010, 05:21 AM
#35
Thread Starter
Frenzied Member
Re: Database connection issue
Still i am getting expected error ;.at the following line.Kindly let me know the idea
Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.
-
Aug 9th, 2010, 05:26 AM
#36
Fanatic Member
Re: Database connection issue
 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"')";
-
Aug 9th, 2010, 05:48 AM
#37
Thread Starter
Frenzied Member
Re: Database connection issue
still i am getting ; Expected Error.at the following line .
Code:
cmd.CommandText ="insert into CDs(ArtistName )values ('"txtArtistName.text"')";
-
Aug 9th, 2010, 05:53 AM
#38
PowerPoster
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
-
Aug 10th, 2010, 12:37 AM
#39
Thread Starter
Frenzied Member
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();
}
Last edited by firoz.raj; Aug 10th, 2010 at 12:43 AM.
-
Aug 10th, 2010, 01:28 AM
#40
PowerPoster
Re: Database connection issue
you need to rebind the data. so you need a method which binds the data and call that method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|