|
I am not sure that the sequence of commands is correct. Try this:
using (ConnectionDB d = new ConnectionDB())
{
d.CONNECTER();
using (OleDbCommand cmbox = d.sql_con.CreateCommand())
{
cmbox.CommandText = "SELECT Code_article FROM Catalogue";
}
d.DECONNECTER();
}
|
|
|
|
|
Thanks i will try it and you will get back
|
|
|
|
|
I try and the error comes from me thank you very much mmister it works.
|
|
|
|
|
First off, don't hard code connection strings - they need to be in a config file or similar, if only so you don't release the password in clear text with your app...
Second, never store databases in your app directory: although it works in development, it will fail in production as the application folder rarely has write permissions anymore for anti-virus reasons. See here: Where should I store my data?[^] for some better ideas.
Thirdly, you are much, much better off creating your Connection object in your code when you need it inside a using block, as they are scarce resources and should be Disposed when you are finished with them:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
{
cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int age = (int) reader["Age"];
string desc = (string) reader["Description"];
Console.WriteLine($"{age}\n{desc}");
}
}
}
} That way, whatever happens to your code, the objects will be closed and disposed for you automatically.
Fourthly, although you are trying to separate the DB from the form (which is a good idea) don't use MessageBox to report problems with DB related stuff - it makes too many assumptions about how your code will be used. Error handling and reporting is the province of the Presentation Layer, not the Data Layer: there is no guarantee that the user will be able to see a MessageBox! Let the code that calls the DB stuff handle errors - it can report or not, but it may log instead, or just terminate what it is doing. Your way, the user gets the MessageBox, then your app crashes because the connection isn't open anyway.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thank you sir message well received. I haven't tried the configuration file side for the connection to the database yet, but I will study it and see if I can manage it.
|
|
|
|
|
public class ConnectionDB
{
public OleDbConnection sql_con;
public OleDbCommand sql_cmd;
public OleDbDataAdapter DB;
public DataSet DS = new DataSet();
public DataTable DT = new DataTable();
public OleDbDataReader DR;
public string connetionString = null;
public void CONNECTER()
{
connetionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\DB_CaisseEnregistreuse.mdb;Persist Security Info=True;Jet OLEDB:Database Password=B@sta08091987";
sql_con = new OleDbConnection(connetionString);
}
public void DECONNECTER()
{
if (sql_con.State == ConnectionState.Open)
{
sql_con.Close();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ConnectionDB d = new ConnectionDB();
private void LoadDB()
{
using (OleDbConnection sql_con = new OleDbConnection(d.connetionString))
{
sql_con.Open();
using (OleDbCommand sql_cmd = d.sql_con.CreateCommand())
{
string CommandText = "SELECT * FROM Detail_temp";
d.DB = new OleDbDataAdapter(CommandText, d.sql_con);
d.DS.Reset();
d.DB.Fill(d.DS);
d.DT = d.DS.Tables[0];
dataGridView1.DataSource = d.DT;
d.DECONNECTER();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
LoadDB();
d.sql_con.Open();
TxtQteCmd.Text = "1";
Lbl_Affich_TotalCmd.Text = "0";
LblAfficDate.Text = DateTime.Now.ToString();
using (OleDbCommand cmbox = d.sql_con.CreateCommand())
{
cmbox.CommandText = "SELECT Code_article FROM Catalogue";
using (d.DR = cmbox.ExecuteReader())
{
try
{
while (d.DR.Read())
{
CmbRef_Produit.Items.Add(d.DR["Code_article"]).ToString();
}
if (string.IsNullOrWhiteSpace(CmbRef_Produit.Text))
{
Lbl_affich_TxtQteStock.Text = "";
Lbl_Affich_Designation.Text = "";
Lbl_Affich_PrixUnitaire.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
d.DECONNECTER();
}
d.DECONNECTER();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Sir I tried to do as you suggested and I would like you to take a look at my code, just see if it's good. And I would also like to know if the DISCONNECT method is worth it
|
|
|
|
|
hello
I am seraching some infos to create ma own script language to execute and enhance dos command in relation with my application variable with for and if conditions.
if you have some exemple or informations ?
thx
didier
|
|
|
|
|
Your question is a word salad and really makes no sense.
Are you asking how to add scripting support to some other application you've written? Are you trying to add command line argument support to your app?
What do you mean by "enhance dos command"?
Perhaps if you gave an example of what you're trying to do...
|
|
|
|
|
Windows command shell (aka dos) language already contains FOR and IF commands.
|
|
|
|
|
In case you're not aware of ".bat" files. I'd say more than one command in a file constitutes a "script".
DOS Batch Files
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
|
I want a scritpt can interact with ma database and my own function .
thescript must interact with log4net for his operation
exemple
if movefile( <varfromdb("key1")>\too.txt , <varfromdb("key2")>\foo.txt) > 0 then
print ("Error")
endif
|
|
|
|
|
OK. You can forget the "DOS", as you called it, batch file.
Use Powershell if you want Log4Net support.
You can use Powershell to do anything you want from a command line, albeit a PowerShell prompt.
|
|
|
|
|
Yes but the power shell syntax is so harddy for a little user...
and it must work on server from 2k3 to 2k16 and the same in linux.
|
|
|
|
|
And?
Get to learning Powershell.
It works on all current versions of Windows and there's a version for Linux too.
|
|
|
|
|
hello thanks but powershell is not for my enduser the syntax is too difficult .
i'm looging vor a language with
var
for next
if then else
while wend
ant the possibility to add my own functions
|
|
|
|
|
Well Powershell has all those features.
|
|
|
|
|
You don't have much choice.
Rolling your own language/interpreter/debugger is going to take you quite a long time to develop. On top of that, YOU will be the only source of support for the customers on the language.
|
|
|
|
|
it for this reason , i'm looking for a pattern to do it?
|
|
|
|
|
Pattern? To do what?
This is purely research and learning right now.
|
|
|
|
|
You could write the Powershell scripts for your clients much faster than you could create this application.
|
|
|
|
|
Basically I have a program what opens .spl's for images for a game and it's made in c#, some of the .spls open but most are causing this error below. I can upload the source if needed and willing to pay some cash if it can be fixed.
https:
Source client - also has 4 images from the games i'm trying to use, 2 work (man000000.spl) & (00011100.spl) and 2 that cause the error (00011000.spl) & 00011101.spl
Source program -
https://easyupload.io/i8lo8c
Cheers,
P
|
|
|
|
|
sorry, it sound's like you need RentACoder or such - we don't do that here - if people show is problem(s) with code, what they expect, how they tried, then we'll try to nudge them in the right direction - we don't do homework or code for hire
|
|
|
|
|
Look at the first two message which show: "ArgumentOutOfRangeException" on a List index. So you are passing a list item index which is either less than zero, or greater than the number of items minus one, in your list. Go to the point in your code that makes the call and you should be able to see why.
|
|
|
|
|
I have a problem, I have two applications, a windows form and a webservice, the webservice code is on a server with IIS. I downloaded remote debbuger 2019 and after installing it I tried to connect the webservice project through a new instance and in the remote debugger panel it tells me that I am connected.
Then I go to my windows form application and start debugging but when I enter the webservice method it doesn't move the debug to the other application as I would have expected, and it times out.
What am I doing wrong?
|
|
|
|