|
This is my code:
I reach an error: Syntax error in INSERT INTO statement.
I know this error is because of line : dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString());
But I donno how to solve it. I have a field named "Date", with datatype "DateTime", and with format "ShortDate".
OleDbDataAdapter adp = new OleDbDataAdapter();
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source=f:\Wire1\WireDb.mdb");
conn.Close();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM tbl1 ORDER BY Code", conn);
adp.SelectCommand = cmd;
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp);
DataSet ds = new DataSet("tbl1");
DataTable dt = new DataTable();
conn.Open();
adp.Fill(ds, "tbl1");
dt = ds.Tables[0];
DataRow dr = ds.Tables[0].NewRow();
int x = dt.Rows.Count;
dr["Code"] = ++x;
dr["Street"] = "Molavi";
dr["Alley"] = "Molavi 34";
dr["Status"] = true;
dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString());
ds.Tables[0].Rows.Add(dr);
adp.Update(dt);
ds.Clear();
adp.Fill(ds.Tables[0]);
conn.Close();
Sourie
|
|
|
|
|
The INSERT statement wasn't included in your post (only SELECT), but I believe that there are two possible reasons
- Access or OleDbConnection doesn't like if the field name is Date (may be a reserved word). Tru using angle brackets (for example INSERT INTO TableName (Field1, Field2, ..., [Date]) VALUES (...)) or chcange the name of the field in the db.
- dr["Date"] has a bad value. Use debugger to view it's value and to see if it looks like what you would suspect
Hope this helps,
Mika
The need to optimize rises from a bad design
|
|
|
|
|
Oh my God, It is working now.
Thanks alot Mika, You are right "Date is a keyword for MS Access, I changed it to DateVal, and my code finally worked.
Tnx alot
Sourie
|
|
|
|
|
You're welcome Happy coding!
The need to optimize rises from a bad design
|
|
|
|
|
I got another problem.
I created similar table in my database and I named it as tbl2.
I tryed the code for this table, but it is not working.
Sourie
|
|
|
|
|
If it's working for the first table, double check the code for second one.
If still having problems I think you should open a new thread and provide info about the problem (error message, relevant code etc..) especially if the problem is somehow different. If I'm still awake, I'll try to check it also.
Also I think you would get more answers if you would post on .Net Framework forum (not sure if that's the correct one though). The problem in this question was related to a class defined in framework but also to Access database so perhaps SQL forum would do just as fine.
Mika
The need to optimize rises from a bad design
|
|
|
|
|
I am sorry it was like the previous problem. Because in the second table of my database, I defined a field named: "No".
It was also a keyword for MS ACCESS.
Thank you so much for helping me.
Kiss your child
Sourie
|
|
|
|
|
You're welcome
There are plenty of reserved words so you'll encounter this problem once in a while.
If you want, you can test using angle brackets. At least in SQL Server they make it possible to use reserved words and even spaces. When defining or using a field you can surround it with brackets, for example:
- [No]
- [Yes]
- [Field name with space]
and then for example "SELECT [No], [Field name with space] FROM [TableName] WHERE [Yes] = ..."
Mika
The need to optimize rises from a bad design
|
|
|
|
|
Thanks for your suggestion
Sourie
|
|
|
|
|
Dears,
I'm having a serious problem making me MAD!
I'm searching for days over google but no use
I'm trying to get data from access where date column in access = datetime selected from textbox in C#
CODE:
it is one line code which is not working
DateTime d = DateTime.Parse("1/1/1");//while debugging : d=1/1/2010 12:00:00 AM
sql="select * from MYTABLE where COLUMNDATE = d";
COLUMNDATE is defined as general date in MYTABLE IN access.
and then when I execute the sql (i.e: datareader.executeReader) command with C#, it is giving mismatch data type.
PLZ PLZ PLZ PLZ try to help me!!!
THANKS IN ADVANCE!!
|
|
|
|
|
Hey you need to write
DateTime d = DateTime.Parse("1/1/1");
sql="select * from MYTABLE where COLUMNDATE = '"+d.ToShortDateString()+"'";
because of d.ToShortDateString() value of d will be only 1/1/2001 which is same as general date as MyTable in Access.
|
|
|
|
|
Have a good day all ...
I wonder if there is an easy way to design a printable page by C#
with out writing over 100+ line of code
in the Event Document_PrintPage(object sender, PrintPageEventArgs e)
It's really a very hard way ........ to tidy up a small table , with a few words ..... on the printing page
and if any one , Know a good .Net Component or Library .... I would be happy to hear about it ....
( Yes I try Crystal report ....) but I need more , easy , and flexible , Designer for my page
thank you in advance ....
Kind regards ......
I know nothing , I know nothing ...
|
|
|
|
|
Perhaps this question would be better in .NET Framework [^]-forum.
However, have a look at Reporting Services included in .Net Framework.
The need to optimize rises from a bad design
|
|
|
|
|
My code creates textboxes based on data from a file. I need to be able to retreive the text from each dynamically-created textbox. By the way, each textbox is contained in a splitcontainer control (spc_Questions).
Here is the code that creates each textbox:
for (cntr = 0; cntr <; NumQ; cntr++)<br />
{<br />
input = sr.ReadLine(); <br />
newTextBox = new System.Windows.Forms.TextBox();<br />
<br />
newTextBox.Size = new Size(25, newTextBox.Size.Height);<br />
newTextBox.MaxLength = 1;<br />
newTextBox.CharacterCasing = CharacterCasing.Upper;<br />
newTextBox.TabStop = true;<br />
newTextBox.TabIndex = cntr;<br />
<br />
newTextBox.Location = new Point(spc_Questions.Panel1.Right - newTextBox.Size.Width - 10,<br />
vHeight);<br />
<br />
newTextBox.Tag = cntr.ToString();<br />
<br />
newTextBox.Show();<br />
<br />
spc_Questions.Panel1.Controls.Add(newTextBox);<br />
<br />
<br />
<br />
}
Any help would be appreciated.
|
|
|
|
|
danielhasdibs wrote: Any help would be appreciated.
Im sure it would be, but you forgot to ask a question!
|
|
|
|
|
|
you need to set the TabIndex Property
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
It's set after I create the text box.
newTextBox.TabIndex = cntr;
I've been trying to use the SelectNextControl() method to get to each textbox control, but it keeps selecting the splitcontainer instead of the textboxes.
|
|
|
|
|
what are you using passing in the first parameter in SelectNextControl()?
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
Here's a sample of the SelectNextControl() method I used.
<br />
string[] textFromControl = new string[10];<br />
Control nextControl = new Control();<br />
<br />
spc_Questions.Focus(); <br />
<br />
<br />
for (cntr = 0; cntr <; NumControls; cntr++)<br />
{<br />
<br />
this.SelectNextControl(nextControl, true, true, true, true);<br />
nextControl = this.ActiveControl;<br />
nextControl.Focus();<br />
textFromControl[cntr] = nextControl.Text;<br />
}
|
|
|
|
|
Please Try this Code , it's about buttons Control , but it think you will understand my idea ......
public partial class Form1 : Form<br />
{<br />
<br />
public Button sb;<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
<br />
for (int x = 0; x < 10; x++)<br />
{<br />
sb = new Button();<br />
sb.Size = new Size(17, 17);<br />
sb.Location = new Point(x * 17, 10);<br />
sb.Tag = (x).ToString();<br />
sb.Visible = true;<br />
sb.Click += new EventHandler(sb_Click);<br />
Controls.Add(sb);<br />
<br />
}<br />
<br />
this.Size = new Size(176, (12 * 17) + 7);<br />
}<br />
<br />
<br />
private void sb_Click(object sender, System.EventArgs e)<br />
{<br />
Button sb = sender as Button;<br />
this.Text = sb.Tag.ToString();<br />
<br />
}
I know nothing , I know nothing ...
|
|
|
|
|
Thank you! Below is how I implemented it for anyone who needs it (this is in a splitcontainer -- spc_Questions):
for (cntr = 0; cntr <; NumControls; cntr++)<br />
{<br />
newTextBox = new System.Windows.Forms.TextBox();<br />
<br />
newTextBox.Size = new Size(25, newTextBox.Size.Height);<br />
<br />
newTextBox.Location = new Point(spc_Questions.Panel1.Right - newTextBox.Size.Width - 10,<br />
cntr * setHeight);<br />
<br />
newTextBox.Tag = cntr.ToString();<br />
<br />
newTextBox.TextChanged += new EventHandler(newTextBox_TextChanged); <br />
<br />
newTextBox.Show();<br />
<br />
spc_Questions.Panel1.Controls.Add(newTextBox); <br />
<br />
} <br />
<br />
void newTextBox_TextChanged(object sender, EventArgs e)<br />
{<br />
TextBox Text = sender as TextBox;<br />
ControlTexts[int.Parse(Text.Tag.ToString())] = Text.Text;<br />
}
Thank you, again!
|
|
|
|
|
Hi
I am writing a Windows application in C# that uses a Word Form template as the basis for a report. Creating the report is not a problem but I would like to temporarily change the directory that the user saves the report to.
Many Thanks
|
|
|
|
|
I don't think I could understand what you are looking for, but is this helpful?
string outputPath = @"C:\Windows\Temp";
System.IO.Directory.SetCurrentDirectory(outputPath);
Always keep the Murphy Rules in mind!
|
|
|
|
|
Hi selcuks.
Thanks for that. I've just tried it but it doesn't work. What the application does is fire up Word, creates a new document based on the template and then populates the form fields with data. The user is then presented with the document for proof reading. At this stage, the user can save the document but I would like to point them to a specific directory, as it is buried deep within our network, but leave their default directory as it is (it is a multi user app). Hope this clarifies wht I am trying to achieve.
|
|
|
|