|
sir sorry and thank you for seeing my error after a review of my code.
I don't know how to explain it to you but I will illustrate it as a comment in my code.
|
|
|
|
|
private void Ajouter (String refProd, String quantite, String design, String prixU, String prixTtc)
{
String[] row = { refProd, quantite, design, prixU, prixTtc };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
private void BtnAjouter_Click(object sender, EventArgs e)
{
double total;
decimal total_achat;
if (Lbl_Affich_Designation.Text == "" || Lbl_Affich_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl_affich_TxtQteStock.Text == "" || CmbRef_Produit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (CmbRef_Produit.Text != "")
{
double a = 0;
double b = 0;
double c;
int nbprod;
double.TryParse(TxtQteCmd.Text.Trim(), out a);
double.TryParse(Lbl_affich_TxtQteStock.Text.Trim(), out b);
double.TryParse(Lbl_Affich_PrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
nbprod = 0;
try
{
using (OleDbCommand cmd = d.sql_con.CreateCommand())
{
cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
cmd.Parameters.AddWithValue("@ref_det", CmbRef_Produit.Text);
cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
cmd.Parameters.AddWithValue("@Designation", Lbl_Affich_Designation.Text);
cmd.Parameters.AddWithValue("@Prix_unitaire_HT", Lbl_Affich_PrixUnitaire.Text);
cmd.Parameters.AddWithValue("@Prix_total_HT", total);
d.sql_con.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = d.DT;
Ajouter(CmbRef_Produit.Text, TxtQteCmd.Text, Lbl_Affich_Designation.Text, Lbl_Affich_PrixUnitaire.Text, total.ToString());
cmd.Dispose();
d.sql_con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
LoadDB();
for (int i = 0; i < listView1.Items.Count; i++)
{
total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);
nbprod++;
LblNbProd.Text = nbprod.ToString();
}
Lbl_Affich_TotalCmd.Text = total_achat.ToString();
Vider();
TxtQteCmd.Text = "1";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
d.sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!", "Problème avec le stock", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Caractères non autorisés", "Problème de saisie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
|
|
|
|
|
I've got a situation in my .Net application where I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name.
To get the input file from the address bar:
HttpPostedFileBase fileContent = Request.Files[0];
Then the save:
fileContent.SaveAs(fullPath);
Unfortunately the SaveAs does not work, as when you get the Word Document from the Response object, it blanks it out when doing the SaveAs.
So what I need to do is get the Word Document that the user selected, and save the contents of that Word Document to a Word Document with a different, standardized name.
so what I have so far is
using (var documentReader = new StreamReader(fileContent.InputStream))
{
var readDoc = documentReader.ReadToEnd();
}
I know my readDoc variable contains the selected Word document contents, and it appears to do the ReadToEnd correctly, the problem now is, I need to be able to save those contents stored in readDoc to a different Word Document with a different name.
Any ideas on how to go about this?
|
|
|
|
|
Once you have the file name, you don't have to "read" the file; you can just copy it with a new name.
File.Copy Method (System.IO) | Microsoft Docs
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
|
|
|
|
|
No, because as I understand it, the file is being uploaded to a webserver. File.Copy works on the local system only; could be a bit of an issue if you could just copy files by name from a remote client!
|
|
|
|
|
Frankly, I couldn't tell where he was planning to copy from or to, or why the document had to be opened. Congrats on your insight. And no, I wasn't "copying" from "client to server."
Quote: I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name.
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
|
|
|
|
|
True, it's certainly confusing. I'd latched on (probably incorrectly) to his reference to the "response object". But then it's posted in "General" rather than "Web" so you're probably right.
|
|
|
|
|
Once you have read all the file data you just need to write it out to the place where it is to be saved:
using (var documentReader = new StreamReader(fileContent.InputStream))
{
var readDoc = documentReader.ReadToEnd();
StreamWriter writer = new StreamWriter(saveasfilename);
writer.Write(readDoc);
writer.Close();
}
This is an over simplification, and could cause problems for Word document type files, as they are not text streams. A better solution would be to read and write byte arrays so the actual format of the file's content is copied verbatim.
|
|
|
|
|
Hi Richard -
Yes this indeed is a Word document, which is the problem.
I get the Word document from the user via a popup modal, via the statement
HttpPostedFileBase filecontent = Request.Files[0];
it's when I try to do the save
filecontent.SaveAs(fullpath);
where it fails, as it writes out a blank document.
Could you give me an idea or example of your read and write bytes proposed solution? I'm not familiar.
Thanks, appreciate it.
|
|
|
|
|
|
That's not correct. There's already a concrete class in the framework which inherits from HttpPostedFileBase . It's recommended to use the HttpPostedFileBase class in MVC so that it's easier to test.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
My bad, but I couldn't see that in the documentation anywhere.
|
|
|
|
|
fileContent.SaveAs should work fine. Are you reading the posted file multiple times?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I've been Googling this but I can't seem to find an answer. How can I get a list of foregin keys for a table ini SQL using C#?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
For SQL Server, select from the sys.foreign_keys view:
sys.foreign_keys (Transact-SQL) - SQL Server | Microsoft Docs[^]
SELECT name, OBJECT_NAME(referenced_object_id) As ForeignTable
FROM sys.foreign_keys
WHERE parent_object_id = OBJECT_ID('TableName')
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to do it using C#
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It's no different from executing a SELECT on any other database tables.
You can use a normal SqlConnection and SqlCommand objects and execute a reader, or just use a SqlDataAdapter and just fill a DataTable.
|
|
|
|
|
True. Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
So just package that as an SqlCommand object, and use it with a DataAdapter.Fill method call to fill a DataTable.
What part of that is giving you a problem?
"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!
|
|
|
|
|
True. Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Let's assume I have a simple class:
public class StringAndInt
{
public string myString;
public int myInt;
public StringAndInt(string myString, int myInt)
{
this.myString = myString;
this.myInt = myInt;
}
} If I want to create an array of this class, then the code pretty quickly becomes hard to read:
StringAndInt[] myArray = new StringAndInt[] { new StringAndInt("String", 0), new StringAndInt("String", 1), new StringAndInt("String", 2) }; I think in C-programming the corresponding code is much easier to read:
StringAndInt myArray[] = {{"String", 0}, {"String", 1}, {"String", 2}}; What's the most readable way I can accomplish this in C#? Please note that I need to be able to create arrays of different sizes, but in practice it would be ok to set an upper limit of 1000 elements. I could create lots of methods like below, but then I would like to be able to tell the compiler to optimize away unused methods (I'm not using reflection and I don't export the code as a dll to anybody so it would be safe to optimize away uncalled methods):
StringAndInt[] constructArray(string myString, int myInt)
{
return new StringAndInt[] { new StringAndInt(myString, myInt) };
}
StringAndInt[] constructArray(string myString0, int myInt0, string myString1, int myInt1)
{
return new StringAndInt[] { new StringAndInt(myString0, myInt0), new StringAndInt(myString1, myInt1) };
}
StringAndInt[] constructArray(string myString0, int myInt0, string myString1, int myInt1, string myString2, int myInt2)
{
return new StringAndInt[] { new StringAndInt(myString0, myInt0), new StringAndInt(myString1, myInt1), new StringAndInt(myString2, myInt2) };
}
.
.
.
StringAndInt[] myArray = constructArray("String", 0, "String", 1, "String", 2);
|
|
|
|
|
One way to do it would be to use the params keyword in a static method:
public class StringAndInt
{
public string myString;
public int myInt;
public StringAndInt(string myString, int myInt)
{
this.myString = myString;
this.myInt = myInt;
}
public static StringAndInt[] MakeArray(params object[] data)
{
if (data.Length % 1 == 1) throw new ArgumentException("Initialization data must be provided in pairs");
int elements = data.Length / 2;
StringAndInt[] arr = new StringAndInt[elements];
for (int index = 0; index < elements; index++)
{
object a1 = data[index * 2];
object a2 = data[index * 2 + 1];
if (!(a1 is string s && a2 is int i)) throw new ArgumentException($"Invalid argument pair ({a1},{a2}");
arr[i] = new StringAndInt(s, i);
}
return arr;
}
}
Then to use it is clear:
StringAndInt[] myArray = StringAndInt.MakeArray("String", 0, "String", 1, "String", 2);
"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!
|
|
|
|
|
|
Tricky with an array, but for a List<T> you can use an extension method:
public static class StringAndIntExtensions
{
public static void Add(this ICollection<StringAndInt> list, string myString, int myInt)
{
list.Add(new StringAndInt(myString, myInt));
}
} to let you write:
var myList = new List<StringAndInt>
{
{ "String", 0 },
{ "String", 1 },
{ "String", 2 }
}; The Magical Methods in C# · Cezary Piątek Blog[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|