|
Sorry, I meant tax "evasion".
Oh, wait ...
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
How to insert this XML file in SQL Server? I have done one but cannot output. The error said "String was not recognized as a valid DateTime"
Here the code that I done:
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
int appointment_ID = 0;
string nric = null;
string client_Name = null;
DateTime? appointment_Date = null;
DateTime? start_Time = null;
SqlConnection conn = new SqlConnection(@"Data Source=USER1-PC\SQLEXPRESS;Integrated Security=true;Database=ProductDB");
XmlReader xmlFile = XmlReader.Create("G://appointment.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);
int i = 0;
conn.Open();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
appointment_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
appointment_Date = DateTime.Parse(ds.Tables[0].Rows[i].ItemArray[1].ToString());
start_Time = DateTime.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString());
nric = ds.Tables[0].Rows[i].ItemArray[3].ToString();
client_Name = ds.Tables[0].Rows[i].ItemArray[4].ToString();
SqlCommand command = new SqlCommand("insert into Appointment values(" + appointment_ID + ",'" + appointment_Date + "'," + start_Time + "'," + nric + "'," + client_Name + ")", conn);
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
}
conn.Close();
MessageBox.Show("Done .. ");
Here is appointment.xml:
<interface>
<appointment>
<appointment_id> 000000087508
<appointment_date>2018-01-25
<appointment_start_time>13:15:00
<client>
<nric>456987123
<client_name>JAMES
<appointment>
<appointment_id>000000087501
<appointment_date>2018-01-25
<appointment_start_time>14:30:00
<client>
<nric>963258741
<client_name>Bill Tan
<client>
<nric>123456789
<client_name>David Ang
<checksum>2
|
|
|
|
|
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood' The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
Chances are, when you fix that throughout your application, the problem you have noticed will vanish at the same time.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Just noting that SQL server can process XML directly.
Jieha Lee wrote: The error said "String was not recognized as a valid DateTime"
Print out your SQL - don't guess, actually print it.
Then you can take that SQL and try it directly on the database.
As a guess as to the error, SQL expects a very specific form for a timestamp (not a 'date') and you have not created that form.
|
|
|
|
|
sir can we take the field as nvarchar to store xml?
|
|
|
|
|
Not sure what that means.
Can you store an entire xml document as text in a single column in a database? Then yes.
However the poster is attempting to process and parse the xml and store the parsed result, not xml, in the database.
|
|
|
|
|
Can someone please help a newbie with what I suspect is a simple error?
Program.cs:
using System.IO;
namespace CreateDT
{
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void MyMethod (name1 string, name2 string)
{
}
}
}
Form1.cs:
using System;
namespace CreateDT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DoItButton_Click(object sender, EventArgs e)
{
MyMethod(currentfile, TargetFile);
}
Gives error "CS0103 The name 'MyMethod' does not exist in the current context." I've looked at lots of examples which all seem to be done this way. Obviously not, what am I missing?
|
|
|
|
|
The method 'MyMethod' is inside (= a member of) the (static) class 'Program', so you have to use
Program.MyMethod(currentfile, TargetFile);
|
|
|
|
|
Methods (and fields, properties and events) are part of the class definition: So when you add a method to ClassA, it doesn't exist as part of ClassB. So to access a ClassA method from ClassB, you have to specify which class you are referring to; which class the system should look at - either by specifying a class instance:
Button b = new Button();
b.Text = "Click me!"; Or (in the case of static methods like MyMethod) by specifying the class name:
Program.MyMethod(currentfile, TargetFile);
But you shouldn't really be modifying the Program.cs file - instead add a Utilities.cs file, and create a static class in that. To DO that, right click your project name in the Solution Explorer pane, and select "Add"..."Class...". Change the name from "Class1" to "Utilities" and click the "Add" button.
Change the class to static and add your method to that. Then when you write your code, it's a bit more obvious that these are designed as "generic helper" methods:
Utilities.MyMethod(currentfile, TargetFile);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.
modified 21-Jan-18 15:05pm.
|
|
|
|
|
They are both private methods of class OrdersContyrolAccessibleObject .
|
|
|
|
|
I try private too I have the same problem.
DB
|
|
|
|
|
It is a simple spelling mistake on your class name.
|
|
|
|
|
What you think it's wrong spelling.
BUY == Buy;
SELL == Sell;
or
rBBUY == rbBuy;
I changed in Windows Form Properties
I have the same Error Buy and Sell
David
|
|
|
|
|
David, your class name is spelled incorrectly. You named your class
OrdersContyrolAccessibleObject . Note the extra y in Control.
This space for rent
|
|
|
|
|
Yes, Was extra y there , but I have the same problem.
I change it from private to public, do not work.
I don't know what can I do next to work.
David
|
|
|
|
|
Show us exactly what you have as your code now.
This space for rent
|
|
|
|
|
First, apologies if this is a common question, I've done a few searches to no avail but honestly I'm not 100% sure how to search for what I want.
I would like to create a project (Project X), probably just a DLL, that can be used as a reference for multiple other projects. Project X is installable, and Project A and Project B can reference it. I want to be able to make updates to Project X, without having to reinstall A and B.
Example: A and B referece v1.0 of X. Project C comes along, and needs a few added classes, etc. that would make sense to have in a global project like X. So X becomes 1.1, and Project C uses that new code. Project A and Project B continue to work without requiring an update.
A real world example: A vendor that my employer uses has five or six applications for various tasks, they all need to communicate either with each other or with our mainframe. The vendor has a "Security" app that has all of the code for these connections. Occassionally, if we get a new application or update an existing application, we have to update the Security app to a new version, but this doesn't require us to update every one of that vendor's applications that rely on said security app. I want to mimick that and I'm drawing a blank.
|
|
|
|
|
So do. What is stopping you?
I do it all the time: A solution of projects which produce DLL Assemblies (either with a single test EXE project or individual test EXE projects as necessary).
Other projects producing EXEs reference either the Release build Assembly, or the project that generated it (depending on whether I need to step into the DLL assembly for debugging purposes or not).
Change the DLL, rebuild it, overwrite / reregister it, and the existing EXE projects use the new version without needing any recompilation.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sounds like you need a DLL, or a COM library. In both cases you can manage future enhancements with new versions without affecting existing clients. Google, or the CodeProject Articles section[^] should have some whitepapers and samples.
|
|
|
|
|
I have a base class that sets property values:
<pre>public void SetProperty<T>(string propertyName, ref T propertyField, T value)
{
if (!Object.Equals(value, propertyField))
{
if (OnPropertyChanging(propertyName, propertyField, value))
{
IsDirty = true;
T oldValue = propertyField;
propertyField = value;
RaisePropertyChanged(propertyName);
OnPropertyChanged(propertyName, oldValue, propertyField);
}
}
}
I then call it like this:
<pre>private string _CompanyName;
public string CompanyName
{
get { return _CompanyName; }
set
{
if (_CompanyName != value)
{
SetProperty<string>("CompanyName", ref _CompanyName, value);
}
}
}
Yet for a nullable property I get a compilation error.
private decimal? _GLRate;
public decimal? GLRate
{
get { return _GLRate; }
set
{
SetProperty<decimal>("GLRate", ref _GLRate, value);
}
}
Cannot convert from ref decimal? to ref decimal
I'm guessing it doesn't work with generics??
How can I make this work?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
"decimal" is not the same type as "decimal?" even though they have a lot of characters in common ("?" is what the call syntactic sugar, Visual Studio converts decimal? into a type that looks nothing like "decimal")
So update the type in the angle brackets to reflect the actual type
SetProperty<decimal?>("GLRate", ref _GLRate, value);
However you can actually do without the brackets altogether, .net can work the type out from the params you supply
SetProperty("GLRate", ref _GLRate, value);
|
|
|
|
|
Removed the <types>.. Works great! Thank you!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|