|
The SqlCommand parameter in the SqlDataAdapter constructor is for the SelectCommand , not the InsertCommand . But the other response is correct, you don't need the SqlDataAdapter . The SqlCommand.ExecuteNonQuery call is enough.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Sorry i am a newb to programming, cant get the meaning of what you trying to say.
Is there any article or book that clearly explain about database linking??
Please introduce to me Thanks
fire85.
|
|
|
|
|
You need an SqlCommand object to represent execute any SQL statement on the database. The SqlDataAdapter is only a bridge that connects the data obtained from the database by executing a SELECT command, to a DataSet or DataTable (which can hold data on which you can work, without being connected to the database.)
The SqlDataAdapter can also contain other commands (specifically, one INSERT , UPDATE and DELETE ) that will be used when you ask it (the data adapter) to save the changes you've made to the offline data (DataTable or DataSet ) to the actual database.
So, it works like this:
1. You create a SqlCommand object representing your SELECT statement (it can be a stored procedure wrapping a SELECT statement.)
2. You create a new SqlDataAdapter object and pass it the SELECT statement.
3. You create an empty DataTable (or DataSet )
4. When you call myDataAdapter.Fill(myDataTable) , the SELECT statement is executed in the database, and all the returned records are stored into the myDataTable object.
5. You start working with the data in the myDataTable object. You can add, modify or delete records offline, and your changes will not be saved in the real database. At this point you are not connected to the database.
When you want to update the database with the changes you made to myDataTable , you follow these steps:
1. Create SqlCommand objects for the INSERT , UPDATE and DELETE queries.
2. Assign those command objects to the InsertCommand , UpdateCommand , and DeleteCommand properties of SqlDataAdapter object (myDataAdapter here).
3. Call myDataAdapter.Update(myDataTable) .
4. The data adapter will look at each record in myDataTable and see what kind (if any) of modification it has had, and execute the appropiate command (INSERT if the row was added, UPDATE if it was modified, or DELETE if it was removed.)
So, to your question. First see this[^] topic in MSDN. Can you see that the constructor's argument is named selectCommand ?
When you pass your SqlCommand (which does represent an INSERT query) to the constructor, it's taken as the SELECT command. So, when you try to access it through the InsertCommand property, it naturally is null, since you have not initialized it to something. A quick fix would be to set the InsertCommand to your cmd object before trying to set its CommandText:
<code>OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(cmd);
oldeDbDataAdapter1.InsertCommand = cmd;
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO Mailing (MailingField)" +
"VALUES ('"+ desc +"')"; But as you saw from the above description of data adapters, since you are executing a query and you don't get any results back, a DataAdapter is overkill (actually you are not using it, you just use it to hold a command object which you then execute directly.) So, get rid of the data adapter:
string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
string desc = txtMailing.Text;
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\testing_K_\db1.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO Mailing (MailingField) VALUES (?)";
con.Open();
cmd.Parameters.Add("param1", desc);
cmd.ExecuteNonQuery();
con.Close(); As you can see, there's no OleDbDataAdapter (I just noticed you are using Access and not SQL Server, but all the above description equally applies.)
Note the bold text in the code above. Your code as you had it contains a serious security vulnerability (it's vulnerable to SQL injection attacks, see this article). The code I provided you contains paramters, which, for one thing prevent this[^] type of attacks, but also avoid problems if somehow there's a single quote (' ) in the desc variable.
I hope this helps!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Ya is much more clearer to me now.
Thanks alot LuisR, you have been a great help
Thanks
fire85.
|
|
|
|
|
fire85 wrote:
Ya is much more clearer to me now.
Thanks alot LuisR, you have been a great help
You're very welcome! I'm glad I could help!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I have a set of ActiveX controls, which provide access to underlying document and its structure.
The main document has an Items collection, which holds information on all objects defined within this document.
I am trying to build a convertor for the documents and therefore need to enumerate all items within this collection.
The problem is that Items collection holds different types (classes) of data, e.g. Line, Rectangle, Ellipse, String, and the function TypeName in VB6 would return these names. In C# the GetType() method returns always the name of abstract class. The function TypeName in VB.Net provides the same result as C#.
Does anybody know how to get same result as in VB6.0?
Unfortunatelly I am not able to provide you with this ActiveX control, because they're part of commercial software
|
|
|
|
|
Guys,
I have to build a web application to make a phone call from a call center and record the phone call. Then it must be possible to later recall the conversation with it's corresponding stored call entry which was inserted into a DB table.
I looked at the MS TAPI and it seems like the way to go. Can any body point me in any sort of direction on where to start?
Thanks in advance!
|
|
|
|
|
TAPI and C# aren't the easiest of bedfellows.
I prefer to write COM wrapper around C++/TAPI code and use COM/Interop
However there is a useful C# wrapper around TAPI 2, here[^]
Michael
CP Blog [^] Development Blog [^]
|
|
|
|
|
Hi,
I have an application with links to word files.
With the link, I display the lastupdatedate, but I need also the LastUpdateUser.
Is it possible to find it?
Thanks
|
|
|
|
|
have you tried using the DSOFile.dll. Its ideal for handling office documents. See this link :
http://support.microsoft.com/kb/q224351/
edit:
You can also try using the FileInfo class in .NET as well.
|
|
|
|
|
Suppose I want an application which takes a source file and outputs its data members and functions.
Supposed to develop into a kind of class view explorer.
Regarding parsing the source file ....
should i go for using regular expressions rules for this parsing or
should i go for a language representation in context free grammer[CFG] and use the CFG rules for parsing
or is there any other possible ideas for source file parsing
awaiting advice
fordge
|
|
|
|
|
|
how to locate an accurate position in the vectorgraph in C#?
for example,when i move the cursor into the vectorgraph,and do the click
on one position(point),how can i get its accurate position?
I am from Shanghai China,please forgive my poor English!
wish u good luck
shi fangting
Where there is a will,there is a way!
|
|
|
|
|
i used .NET remoting in my app, but i couldn't release ref object at server-side.
How do release MarshalByrefobj ????
thanks
bachkhoa
|
|
|
|
|
|
Can any one help me as to give me the name of the method call in .Net
that sees if a file contains a specific text that is not System.IO.Directory.GetFiles I know that such a method exist because I have used it 3 years ago but cannot remember the name of the method Ps also
not by using the envDTE find method that really suchs and is slow
|
|
|
|
|
I have a web page that was developed in c# in .net 1.0 everythins was fine. After installing the .net 1.1 one my web program it not working anymore. This web page load the crystal report viewer on loading. When I clicked to that page it ask for a USERID and PASSWORD of the server. I removed the object crystal report in Visual studio and replaced it with the new version and run it again. Now it gave me this message "CS1595: 'CrystalDecisions.Web.CrystalReportViewer' is defined in multiple places; using definition from 'c:\WINNT\assembly\GAC\CrystalDecisions.Web\9.1.5000.0__692fbea5521e1304\CrystalDecisions.Web.dll'"
|
|
|
|
|
Hi there,
I have created one web project Project1.
I have created a control library TestDll
Now i have set the reference of TestDll in web project Project1.
Now whenever I make any change in TestDll and compile it, the changes are not reflected in Project1. Say if i add new method in TestDll and comiple the dll again, when i try to access that methoed from Project1, its not available (i cant see that method from project1).
Can anybody please help me?
Thanks a lot mate! I am sure you wont let me down..
cheers
Mihir
|
|
|
|
|
Did you set a Project reference to TestDll or did you set a reference to the .DLL file directly?
When you make component libraries, make sure you go into the assembly file and change the version definition to a static number. Don't use the default of "1.0.*". Change it so that it stays constant, like "1.0.0.0".
Using the "*" in the version attribute, the version number will increment with each compile of the .DLL. When the version changes, your reference is now stil bound to the older version, not the updated version.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Can anybody tell me that, if i'm using .NET remoting server with SAO, how can i raise an event across different application domains? the application wors fine when on the same machine but creates problem when I try using server and client on different machine.
The error description and stack trace is:
I tried with microsoft solution which says that u create an abstract class and override it in client and handle events there. But it does not work for me.
Here is the error:
Exception occured at server. Message:
Server encountered an internal error. For more information, turn on customError
s in the server's .config file.
Stack trace:
Server stack trace:
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
at Coordinator.RemotableDelegate.WelComeMessage(ChatArgs ca) in E:\Jayant\Pra
ctice\MyChatArchitecture\Coordinator\IMediator.cs:line 139
at MarshalByRefClass.Mediator.ConnectMe(String userName) in E:\Jayant\Practic
e\MyChatArchitecture\MarshalByRefClass\Mediator.cs:line 49
I'm not using any App.config file. My server code is as follows.
// For Http channel
try
{
SoapClientFormatterSinkProvider scl = null;
SoapServerFormatterSinkProvider ssr = new SoapServerFormatterSinkProvider();
ssr.TypeFilterLevel = TypeFilterLevel.Full;
IWebProxy proxy = new WebProxy("proxy.sharda.mahindrabt.com", 80);
ICredentials ic = new NetworkCredential("myself", "123");
proxy.Credentials = ic;
IDictionary id = new Hashtable();
id["port"] = 9113;
id["typeFilterLevel"] = TypeFilterLevel.Full;
id["name"] = System.Guid.NewGuid().ToString();
id["proxyName"] = "proxy.sharda.mahindrabt.com";
id["proxyPort"] = 80;
id["machineName"] = "DSCP01214Jayant";
id["credentials"] = ic;
id["unsafeAuthenticatedConnectionSharing"] = true;
HttpChannel htc = new HttpChannel(id, scl, ssr);
ChannelServices.RegisterChannel(htc);
RemotingConfiguration.RegisterWellKnownServiceType(typeof (Mediator), "Chat.soap", WellKnownObjectMode.Singleton);
Console.WriteLine("Server running at Http port: 9113 and Tcp port: 5678");
Client code:
uname = "myself";
pwd = "123";
SoapClientFormatterSinkProvider scl = new SoapClientFormatterSinkProvider();
SoapServerFormatterSinkProvider ssr = new SoapServerFormatterSinkProvider();
ssr.TypeFilterLevel = TypeFilterLevel.Full;
IWebProxy proxy = new WebProxy("proxy.sharda.mahindrabt.com", 80);
if (txtProxyUserName.Text == string.Empty ||
txtProxyPassword.Text == string.Empty)
{
ic = new NetworkCredential(uname, pwd);
}
else
{
ic = new NetworkCredential(txtProxyUserName.Text,
txtProxyPassword.Text);
}
proxy.Credentials = ic;
id = new Hashtable();
id["port"] = 0;
id["typeFilterLevel"] = TypeFilterLevel.Full;
id["name"] = System.Guid.NewGuid().ToString();
id["preauthenticate"] = true;
id["proxyName"] = "proxy.sharda.mahindrabt.com";
id["proxyPort"] = 80;
id["machineName"] = "DSCP01214Jayant";
id["credentials"] = ic;
id["unsafeAuthenticatedConnectionSharing"] = true;
HttpChannel htc = new HttpChannel(id, scl, ssr);
ChannelServices.RegisterChannel(htc);
this.SetChannelProxy(htc, proxy);
im = (IMediator) Activator.GetObject(typeof (IMediator), "http://localhost:9113/Chat.soap");
result = true;
}
catch (Exception ex)
{
result = false;
stsConnected.Text = string.Empty;
stsConnected.Text = "An error occured. Message: " + ex.Message;
lblErrorMessage.Text = "An error occurred: " + ex.Message;
}
finally
{
}
return result;
}
private void SetChannelProxy( HttpChannel channel, IWebProxy proxy )
{
//get the channel data
FieldInfo clientChannelFieldInfo = typeof(HttpChannel).GetField("_clientChannel", BindingFlags.Instance | BindingFlags.NonPublic);
//cast it to httpclientchannel to get an access to IWebProxy Class.
HttpClientChannel clientChannel = (HttpClientChannel) clientChannelFieldInfo.GetValue(channel);
//get Proxy object
FieldInfo proxyObjectFieldInfo = typeof(HttpClientChannel).GetField("_proxyObject", BindingFlags.Instance | BindingFlags.NonPublic);
//set up proxy explicitly.
proxyObjectFieldInfo.SetValue(clientChannel, proxy );
}
Jayant D. Kulkarni
Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET
|
|
|
|
|
Hi All,
I am working on FileSystemWatcher to monitor user home (Environment.SpecialFolder.Personal), the watcher component works fine but generates a lot of noise (junk entries in the log file). Hence, my queries -
1. How can I monitor just the user changes? (if thats doable). By default the file system watcher generates changes made by any process in the monitored path.
2. The watcher component generates multiple entires for same change, for example; the watcher component records to entries for same change,
Path to monitor - C:\Documents and Settings\user1\monitoredpath\user
<snip from="" log="" file="">
127637200458287323 Changed C:\Documents and Settings\user1\monitoredpath\user\wrkspace1\shared\copy of new text document (2).txt
127637200458287323 Changed C:\Documents and Settings\user1\monitoredpath\user\wrkspace1\shared\copy of new text document (2).txt
Is there a way this can be filtered?
A couple of requirements though - I don't want to set the filter to a particular file extension. Plus the changes are required for the sub directories.
Any help will be appreciated.
Cheers,
Vikram
|
|
|
|
|
1) You can't filter by Process or Process owner. You can only filter by the type of change being made and by filename specifications.
2) You can't filter this. What you get depends on exactly what the application is doing to the file system.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks for the reply. But the question remains, if such noise can be filtered out. Anyways, any suggestion as to monitor the file path using some other component?
|
|
|
|
|
The only way to filter this stuff out is if you supply the filtering code, which you have to write. The FileSystemWatcher will not do it for you.
There are no other components that monitor the file system.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I am reading in a stream from a file and the text being read in is like this.
"\"ABBE0001\",\"ABBEY\",\"CHRISTOPHER\",\"\",\"\",\"MR\",\"\",\"453 772 576\",\"\",\"\",\"M\",\"24/11/1960\""
I want to remove all backslashes and quotes but when I use the replace it does not work. How can I remove the backslashes and quotes.
Thanks.
Stephen
|
|
|
|
|