|
this is my code...why it doesn't update changes to my datasource???
void MenuItem3Click(object sender, System.EventArgs e)
{
MyConn = new OdbcConnection(myConnectionString);
string SQL = "SELECT * FROM novice ORDER BY id_novice DESC";
OdbcDataAdapter myCommand = new OdbcDataAdapter(SQL,MyConn);
myCommand.SelectCommand = new OdbcCommand(SQL, MyConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "novice");
OdbcCommandBuilder custCB = new OdbcCommandBuilder(myCommand);
BindingManagerBase cm = BindingContext[dataGrid1.DataSource] ;
DataRowView drv = (DataRowView )cm.Current ;
((DataRow )drv.Row).Delete () ;
myCommand.Update(ds, "novice");
ds.AcceptChanges();
}
________________________
live and die by the code
|
|
|
|
|
IMO, to call an OdbcDataAdapter instance myCommand is confusing. You should - following suggested naming guidelines in the .NET Framework sDK - use myAdapter at the very least. Something even more descriptive than that, using "adapter" somewhere, would even be better.
Also, while not the problem, calling ds.AcceptChanges() is not necessary. The Update method does this for you upon successful completion of updating the data store (i.e., no exceptions are thrown).
You should try debugging the code to make sure that the OdbcCommandBuilder constructed suitable SQL statements for each of OdbcDataAdapter 's properties besides the SelectCommand which you've already defined (and is required for the command builder).
Are you sure that the DataSet is full and that all the data is in the "novice" DataTable ? At times you have to set up OdbcDataAdapter.TableMappings if the table from which you're selecting doesn't result in the table name you expect. This is a problem with SQL Server, which names it's tables (result sets) "Table", "Table1", "Table2", and so on.
I also assume that the BindingContext code is elsewhere, perhaps as part of a Button.Click handler? Make sure that however you bind your data, you use the same exact approach to get the BindingContext . To eliminate any problems, it's recommended that you use the following:
CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember</code>]; This ensures that if you set only DataSource to a DataTable , or set the DataSource to the DataSet and the DataMember to the table name (which is recommended for data navigation or to support multiple table views if binding multiple tables throughout the lifetime of that particular DataGrid ), that either works. The binding context must be exactly the same as how you bound your data, or it will not work.
Finally, debug your application or add some debugging code to make sure that the DataSet actually includes changes (which could be a result of not binding correctly, as I stated above). You can do this easy by using DataSet changes = ds.GetChanges();</pre>That will filter out all the unchanged data so you don't have to sift through so much, since examining <code>DataSet s in the variables windows is difficult (Whidbey, i.e. Visual Studio 2005, will improve this with debugging viewers for various types of objects, which is also extensible for your own objects).
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]
|
|
|
|
|
Hello!
I have a problem, and nobody seems to know the remedy...
There are a lot of programmes capable of editing (doing various things) custom file formats, for example game editors.
For instance, there is a fifaFS console application which gives the oppurtunity of editing .big graphic archives of FIFA 2005 from EA Sports. You can pack, unpack files, rebuild etc.
So how did the creator manage to write such software? I mean, there is nothing in Delphi capable of editing .big files (it's written in Deplhi, but I am a C# programmer so I want to know how to do it in C#)... So how?!
I heard that you need to know the structure of the file... Ok, so for example I have the file specification of BMP file format.
But how do I do stuff with it? Please explain! And please give some piece of code... I really need your help!
Please don't tell me that .big is a commercial file format owned by EA, so there's no chance of editing it. I know at least 20 programmes capable of editing .big file.
Thanks!
Regards,
Lars
|
|
|
|
|
I dont htink there is anything magical with this file format. I think its just something where several files are packed into a single file. At the beginning of those files one can clearly the the contents.
You could just dig through one sample file and try to findout yourself how exactly it works. You could also contact someone from one of those 20 programs and ask him if he has any infos conecrning the exact format.
So it doesnt have to do anything with the programming language uyou use, but with the knwoledge you have of the format. If you know it a simple stream is enough to unpack the contents.
|
|
|
|
|
Yeah, well, thanks, but I am kind of a begginer...
So could anyone please give an example code for the available BMP file format, just to demonstrate the technique? Please!
The BMP specification is here: http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/BMP.txt
Or just type 'File formats' in Google, then check out the first site. Choose whichever file you like, and please help me!
Thanks!
|
|
|
|
|
It won't help much if someone shows you how to read file format A (for example, BMP) if you want to modify file format B.
When you get down to it, each file is just a bunch of bytes you can read or write (check BinaryReader or BinaryWriter in the FCL, for example).
But unless you get the specification _what_ the bytes at a certain offset mean, you're out of luck.
You might be able to scan for certain byte patterns, but usually when you try to modify a file without knowing the exact structure you'll end up with a corrupt file.
Once you have the structure you can write classes that read or write the correct files, but not any sooner.
mav
|
|
|
|
|
Ok, thanks! But you didn't get it: I don't want to edit those .big files, I just stated them as an example. I just want to understand how does one edit some file format. And I wanted some simple code about working with those bytes...
|
|
|
|
|
Editing the file format meaning understanding exactly what each byte in the format means. There is no one method for editing every file format. You read the file stream in, parse it up based on the rules for the file format, edit it based on the rules of the file format, and then write it back out.
Are you asking how to read a file? How to parse up the data? or what?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I would manage all the inputText of Internet Explorer and InputBox of other running software by my Program, so iif the user make right click on the input it shows the normal windows contextmenu cut/copy/delete but with a new vox relative to my program, infact if cliccked it call my program and it can return a text to put autmatically inside...
Yes it is a bit complicated but can you help me to understand how to do?
Thanks.
|
|
|
|
|
With a new "vox"? (Voice activation?) Could you elaborate?
You can interact with both the WebBrowser control (which is actually Internet Explorer) or a running instance (or create a new instance if none already exist) of the InternetExplorer class by creating an RCW (Runtime Callable Wrapper) from shdocvw.dll, which you can do using either tlbimp.exe from the .NET Framework SDK, or by importing a COM reference to "Microsoft Internet Controls" using the COM tab when you click "Add Reference..." for your project.
You should search this site for "WebBrowser" or "InternetExplorer" (no space, since it's a class name). There are lots of articles about hosting the WebBrowser control in Windows Forms, as well as advanced hosting interfaces. Either way allows you to handle when documents are loaded, then walk the document object model (DOM) using the WebBrowser.Document property and casting it to the mshtml.IHTMLDocument2 , which is defined in the Microsoft.mshtml.dll assembly that should've installed with VS.NET (and a number of other applications, or you can create your own from mshtml.dll as well). Walking the DOM is just like scripting a DHTML document, only with a little bit of extra work (and believe me, a lot less than with native C++ code) to cast objects to interfaces that implement the property or method you seek, which script does automatically).
You can also do this using the InternetExplorer class to automate a running instance of IE.
You could also implement a toolbar using managed code (though this isn't recommended as it will significantly increase the working set - amount of memory allocated to the process - for whatever process is hosting the WebBrowser control, be it iexplore.exe, explorer.exe (Windows Shell), or some other process). There are articles about that here on Code Project as well, if you'd only search. Try searching for "IE toolbar".
To search this site, use the text box at the top of every page underneath the logo.
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]
|
|
|
|
|
Hello,
I'm new to the C# language but I'm trying to build a little app that will let me monitor to see if my computer is making HTTP/FTP/UNC requests to the outside world. I was just wondering how I can start on this? Is there a System namespace or class that I can use??
Thank you very much,
Troy
|
|
|
|
|
does anyone know how to start this one?
thanks
|
|
|
|
|
I'm wondering what the best threadsafe way to create and use general purpose routines like special date functions, special user calculations, etc. Up until now, what I have been doing is to create a file call myutils.cs that looks something like the code below. I can call it without instantiating it because it is static which is very convienient. I am calling these routine from multiple threads and my app is crashing so I'm concerned I may have made a fatal mistake.
Could someone enlighten me please if what I am doing is major bad? Or, possibly suggest a better way to do the common things.
Thanks
Peter
public class PolarUtils
{
public static int[] HillsForUser(string currentUserId)
{
SqlConnection sqlConnection = PolarUtils.GetNewSqlConnection();
sqlConnection.Open();
string sqlSelectString = "SELECT id_hill FROM HillFile ";
sqlSelectString += " WHERE UserId = '" + currentUserId + "'";
SqlCommand sqlCom
...
public static SqlConnection GetNewSqlConnection()
{
SqlConnection sqlConnection1 = new SqlConnection();
string str = null;
|
|
|
|
|
Within the same AppDomain, access to static members is serialized so they are automatically thread-safe.
What is the exception that is thrown. Saying "my application crashes" is hardly specific enough to provide you with any decent amount of help.
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]
|
|
|
|
|
This may be obvious, but does that mean that static members act like critical sections in that access from another thread will be automatically blocked until the current thread exits that static method? If so, that's great to know. If possible, could you point me to a link where I can read more about it?
Thanks again for both answers,
-Peter
|
|
|
|
|
Actually, upon looking for your documentation I found this to not be true. I was told this once a long time ago in the managed newsgroups. Guess I shouldn't take things for granted, huh?
In that case, use the C# lock keyword (which uses a Monitor when you compile) or a Mutex to synchronize access.
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]
|
|
|
|
|
Oh, and using string concatenation to create SQL expressions is a terribly BAD idea. All I have to do is pass 0 OR 1=1; DROP TABLE HillFile -- and bye bye HillFile data. I could do a lot worse, too, depending on the content which I could easily discover since most RDBMS expose schema information that can be queried, like SQL Server and Access.
Use parameterized queries. See the SqlCommand.Parameters property documentation[^], as well as the SqlParameter class documentation[^] for details and examples. This also could be the reason you're getting an exception, if you're passing in string data that includes quotes anywhere. Using parameterized commands aleviates this problem, as well as avoiding SQL injection attacks. If you're getting a SqlException , which is most like the problem.
And, BTW, if your applications "crashes" because of an exception thrown, you really need to handle exceptions better. There is no reason - short of a catastrphic failure and in order to protect data - that an application should crash. Always validate your data and handle problem areas with try-catch blocks.
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]
|
|
|
|
|
thanks for the tips. In my new stuff I always use the .Parameter rather than sql because of the issues you mention. Also, I do need to better handle my errors so I know what the conditon was on the thread crashes.
|
|
|
|
|
Can someone suggest the different ways to persist/share temporary data across classes in a multi-tier WinForm Application?
For example, assume that I have a Data Structure/Class that holds [connection related data] and another one that holds [user preferences data]. Where can I store –temporarly- this data so I can readily access it from any object in the application with ease.
The following are the methods that I am aware of:
1. Share data through static methods... Bad idea because it is Public.
2. Share using AppDommain.CurrentDomain’s SetData and GetData methods.
Thanks in advance!
|
|
|
|
|
DaMagicMaster wrote:
The following are the methods that I am aware of:
1. Share data through static methods... Bad idea because it is Public.
2. Share using AppDommain.CurrentDomain’s SetData and GetData methods.
Both ways are public. Statics are only the same within the same AppDomain . Any other application or code in another AppDomain won't see the same thing.
Using approach #1, however, they don't have to be data. If your application is in one assembly, you can declare the members as internal , but this defeats the purpose since for a single assembly internal is the same as public . In .NET 2.0 you can also use the InternalsVisibleToAttribute in your assembly to name which other assemblies can see internals. Currently you can use code access security to allow only certain assemblies to access those members, for instance by their public key token (since all assemblies should be signed, and you typically sign all your organization's assemblies - or at least assemblies for a particular product - with the same key pair). See the StrongNameIdentityPermissionAttribute in the .NET Framework SDK for details.
In a N-tier application, that data must also be serializable, so don't forget that. An assembly on one machine couldn't access the data on another without that data being marshaled, which requires the data to be serializable in .NET. If you're using the .NET Framework features - such as .NET Remoting[^], this means attributing your classes and structures with the SerializableAttribute and optionally implementing ISerializable to control the serialization of an object. If you create your own mechanism - which isn't recommended between two managed applications, but may be necessary if supporting legacy code either on the client or server - then you must make sure you marshal your data in other ways. Windows drag-n-drop and the clipboard have a few different ways of marshaling data, from global memory to streams and more.
Between managed code, however, I highly recommend .NET Remoting[^]. It's like Web Services on steroids, and allows you to serialize to binary formats and use different wire formats (like TCP instead of HTTP, which is - by nature - unidirectional). You can even define your own formatters and channels, as well as other sinks which act on the data in a non-specific way (a great example of Aspect-Oriented Programming, or AOP).
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]
|
|
|
|
|
Cool; I did not know that static is limited to same application domain!
Are there any other ways to share data between objects in the same winforms assembly/application?
For instance, in web forms, we can use session or application objects to share and persist data across webpages without having to pass these data from one page to the other.... how about WinForms, are there better mechanisim to persist/share data beside storing the data in appDomain or static classes?
Thanks
|
|
|
|
|
You can store small amounts of data in TLS (Thread Local Storage) using obvious members of the Thread class, but - again - this is only recommended for small amounts of data. When you alloc TLS for a thread, all threads are alloc'd that amount (at least from what I understand of the native APIs that these methods encapsulate).
Passing references to the data or storing in statics is your best option, though. There are many, many more ways like using ContextBoundObject objects to provide the same data to all AppDomains within a process (and even passing them to remote AppDomains) to locking them in global memory (not recommended except for P/Invoke or COM interop) using the Marshal class. I recommend reading through the C# Language specification as well as the .NET Framework SDK both available online at http://msdn.microsoft.com/library[^] for more ideas and information.
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]
|
|
|
|
|
Thanks... I guess that answer nailed it
Strange that Microsoft does not have a more readily available way to share data, but I guess static method would do for now!
|
|
|
|
|
There's plenty of ways to share data, as I pointed out. How you share data depends on how both the setter and getting of that data intend to set and get that data. There's nothing generic about it.
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]
|
|
|
|
|
ok so I've done some thinking about this and here's what I've come up with. The XML file followed by Psuedo code followed by the code.
XML FIle:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<categories>
<category>Operating System</category>
<category>Productivity</category>
<category>Entertainment</category>
<category>Games</category>
<category>Misc.</category>
</categories>
<disk_numbers>
<disk>001</disk>
<disk>002</disk>
<disk>003</disk>
</disk_numbers>
</configuration>
Psuedo Code:
start in parent node "categories"
for each child node it has named "category"
check if innerText is equal to the selected item in the combo box
if it is
remove the child node
if it isn't
go to the next child node
during one pass through the child nodes this can never fail since the combo box item wouldn't be
listed if it wasn't in the file
Code:
private void mainToolbarRemoveCategoryButton_ItemClick(object sender,
DevExpress.XtraBars.ItemClickEventArgs e)
{
XmlNode parentNode = configDoc.SelectSingleNode("/configuration/categories");
if(MessageBox.Show("Are you sure?", "Katalog", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) == DialogResult.No)
{
return;
}
else
{
foreach(XmlNode n in parentNode)
{
if(n.InnerText.ToString() == programCategoryComboBoxEdit.SelectedItem.ToString())
{
try
{
parentNode = n.ParentNode;
parentNode.RemoveChild(n);
configDoc.Save("katalog.config");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
So hopefully it's obvious what I'm trying to do, I guess the foreach loop is where the issue is, is this how I would traverse each child node?
thanks!
|
|
|
|
|