|
SqlCommand cmd = null;<br />
<br />
cmd = connDB.CreateCommand();<br />
cmd.CommandText = "INSERT INTO MandantMC (" +<br />
"Mand, " +<br />
"MID, " +<br />
"VALUES " +<br />
"(?,?)";<br />
<br />
cmd.Parameters.Add("Mand", txtBoxMandant.Text.ToString());<br />
cmd.Parameters.Add("MID", txtBoxKürzel.Text.ToString());<br />
<br />
<br />
cmd.ExecuteNonQuery();
The connDB works. But this code does not. I try to get data from textboxes to SQL DB. If I make hardcoded code like :
... VALUES ('W','Y') then it works. But if I want data from controls, there is an error while passing cmd.ExcecuteNonQuery(). Also if I insert the ' ( within an own string ) it doesn't work. Only ' works in CommandText.
Any sols ?
|
|
|
|
|
The SqlClient uses named parameters, so you need to use @Mand and @MID instead of the ?'s in your sql string. In addition, you may need to specify @ when adding the parameters.
my blog
|
|
|
|
|
Yes, you do need to specify "@" before the parameter names when adding them to the SqlCommand.Parameters collection property.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
SqlCommand cmd = null;
cmd = connDB.CreateCommand();
cmd.CommandText = "INSERT INTO MandantMC (" +
"Mand, " +
"MID, " +
"VALUES " +
"(?,?)";
cmd.Parameters.Add(@"Mand", txtBoxMandant.Text.ToString());
cmd.Parameters.Add(@"MID", txtBoxKürzel.Text.ToString());
???
I tested it, still doesn't work.
Also placed it in CommandText and instead the ? with Parameter Names
|
|
|
|
|
You really should read the SqlParameter documentation. The solution is:
SqlCommand cmd = cmd.connDB.CreateCommand();
cmd.CommandText = @"INSERT INTO MandantMC (
Mand,
MID
VALUES (@Mand, @MID)";
cmd.Parameters.Add("@Mand", SqlDbType.NVarChar, 40).Value = txtBoxMandant.Text;
cmd.Parameters.Add("@MID", SqlDbType.NVarChar, 40).Value = txtBoxKürzel.Text; Notice the placement of "@". These are used for the parameter names. Replace "NVarChar" with whatever string type you defined in your SQL Server table and "40" with whatever length you defined for each column.
Also, why are you using Text.ToString() ? The Control.Text (inheritted by every derivative of Control like TextBox ) is already a string. You're wasting CPU time on unnecessary instructions.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you !
Remember my first code ?
Here the new CommandText
cmd.CommandText = @"INSERT INTO MandantMC (Mand,MID) VALUES (@Mand,@MID)";
Only 1 Line used. My last code doesn't work in multiple lines seperated by "+". Don't understand it...maybe compiler has problems with translating.
I never use toString on Textboxes. It was only for testing purpose while SQL Error occured. Also waste of typing
|
|
|
|
|
You shouldn't use string concatentation and "@" together. The literal character, "@", is so that you don't have to escape special characters (like backslashes) or so that your text can span multiple lines. Both your original code and my example of using "@" (but honestly, such a short SQL command is easier to read as a single line) would compile. Your only problem is that in the first code you posted you had a comma after MID which may cause a SqlException to be thrown; it's an extraneous comma.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I am writing a c# windows application and want it to fill out a simple HTML form, submit the form, and return the content of the returned webpage. Is this possible?
I know how to use WebRequest and WebResponse to retrieve the content of a specific webpage, but I can not figure out how to retrieve the content of a webpage accessed by submitting a form.
Here's what the form looks like for some more info:
<html>
<form method=post action="../cgi-bin/homestead/mail.pl?mama172">
Name:
<input type="text" size="35" name="name">
Address?
input type="text" size="40" name="address">
City?
<input type="text" size="25" name="city">
<input type ="submit" value="Send">
</form>
</html>
|
|
|
|
|
Take a look at the HttpWebRequest.GetRequestStream[^] for HttpWebRequest.GetRequestStream which shows a good example of sending a POST request to a CGI sitting off on some web server.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
|
|
I have some code to open up a word document and add paragraph to the end of the document and then save and close the document. Now creating a word application object is fine, the opening a word document is fine. The problem occurs when I use this code
1. Word.Paragraph wordPara;
2. wordPara = wordDoc.Content.Paragraphs.Add(ref wordRng);
3. wordPara.Range.Text = "Original electronically authenticated by " + physName + " on " + strDateTime;
4. wordPara.Range.Font.Bold = 1;
5. wordPara.Range.InsertParagraphAfter();
For some reason after line 3 runs I recieve this error: "The server threw an excpetion". That is only error I recieve. In my project I am referencing the Microsoft Word 10.0 Object Library can anyone help?
Nicholas Ingratta
ningratt@hotmail.com
|
|
|
|
|
What else does the stack trace say for the exception? You should also try duplicating this code in a VBA project (the ol' school VB) which - if the .NET exception doesn't tell you enough, and I'm surprised that it doesn't - will give you more details about the problem itself through the Err object in VB (its implementation of IErrorInfo defined for OLE/COM).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
the only thing in the stack trace that is of any importance is:
"at Word.Range.set_Text(String prop)"
not really sure what that means
I only have Visual studio .net so I cannot create and old VBA project that I know of
|
|
|
|
|
Obviously you have Office (otherwise you couldn't even instantiate the WordApplication class), so you have VBA. VBA = "Visual Basic for Applications" and is a VB binding to the application's automation layer. Just click Tools->Macro->Visual Basic Editor.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
|
And it won't. VB != VB.NET. It's merely the same syntax. VB (6 and older) is an automation client that uses objects defined in type libraries and implemented by COM servers. VB.NET uses the .NET BCL (base class library) and compiles to Intermediate Language (IL).
So, you copied VB.NET code into a VB IDE (Integrated Development Environment). You need to write VB which is documented if, in the IDE, you click on the Help menu (and like Contents or Index or whatever).
The basic objects will be the same, but the types not defined in the typelib (like Missing.Value) will not work - they're defined in the .NET BCL and, unless exposed to COM through a CCW, cannot be called by VB6 (which is what the VB IDE in Office and other applications uses).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi!
I've a Web Service and 3 different SoapExtension. In my client app., I configure the web.config to execute the 3 diferent SoapExtension.
I'm trying to throw a SoapException in the first SoapExtension and i wan´t to stop the message flow. I don´t want that the flow of the Soap Message continue.
Is that possible?
|
|
|
|
|
Hi!
I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient.
It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)?
I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens?
Does anyone no of a construct that provides a non-blocking read from a stream?
|
|
|
|
|
"Any thoughts on why this happens?":
MSDN states for the return value of the Peek method: "The next character to be read, or -1 if no more characters are available or the stream does not support seeking." So maybe your stream doesn't support seeking.?
"Does anyone no of a construct that provides a non-blocking read from a stream?":
Try getting the underlying stream through BaseStream property and using its asynchronous BeginRead and EndRead methods.
www.troschuetz.de
|
|
|
|
|
You'll need to use asynchronous interfaces. Use the BeginRead/EndRead, BeginWrite/EndWrite methods. Not THAT easy to do, but once you learn the asynchronous patterns in .NET, everything will be easier.
My advice: don't use TcpClient/TcpServer for anything but the simplest things, as they're too simple, and have several drawbacks, as you're starting to notice. Use the Socket class, instead, that is a bit more complex to use, but will not give you so many headaches.
Yes, even I am blogging now!
|
|
|
|
|
|
Thank you all for your advice! I will switch to using sockets instead. It will take some effort, but I'm sure it's worth it! Does anyone know of a good tutorial on working with sockets?
However, I'm the sort of person who cannot let things go so easily. That Peek() does not work as I think it would still borthers me somewhat, and I would like an explanation if there is one? I will let this go eventually, but until then I will remain confused. Anyone?
Thanks!!
|
|
|
|
|
Search for Sockets here on CodeProject and/or read the example given by the MSDN topics for the Socket class.
www.troschuetz.de
|
|
|
|
|
how can i clear the date time picker control from any date in it?
|
|
|
|