Delete an Item (Row) from Access Database C# Visual Studio - c#

I'm trying to program a delete button that will delete a row when it's selected.
Yet, everything I find on the internet is a bunch of _ _ _ _
I am creating my program on Visual Studio 2010 Professional. Using a MS Access Database
The form contains multiple list-boxes, each with their own piece of the row data.
I can add rows to the table through my programs GUI, but cannot seem to delete them!
I want to delete the selected item, and also to delete all rows on-click of a different button.
Thanks
NOTE: I AM NOT USING SQL BEFORE YOU DECIDE TO GIVE ME ANSWERS IN SQL..
It should look "something" like this:
dbnameDataSet.tableName deltableNameRow;
deltableNameRow = dbnameDataSet.FindByItemID( SelectedItem.lstID );
deltableNameRow.Delete();
this.tableNameTableAdapter.Update(this.dbnameDataSet.tableName);
However, My skills in C# are somewhat limited, so I can only write pseudo for most obstacles.
I have the mind to figure out the problems, but not the library of termanology and understood language to write it myself at the moment.
Please can you figure this out for me.
This is how I currently add items (Working):
dboKanadaDataSet.ProductsRow newProductsRow;
newProductsRow = dboKanadaDataSet.Products.NewProductsRow();
newProductsRow.ProductName = txtItem.Text;
newProductsRow.ListPrice = txtPrice.Text;
this.dboKanadaDataSet.Products.Rows.Add(newProductsRow);
this.productsTableAdapter.Update(this.dboKanadaDataSet.Products);
Any contributions are very welcome.
Muchas Gracias, Merci, Danke, Xie4 Xie4, Thanks!
Josh.

I have found a solution (It's not quite what I wanted, but its working for the task.)
Using the 'Binding Navigator' tool found in the toolbox.
It give the user the following bar:
Then, double-click the 'Save' button and add this code to your form:
private void saveToolStripButton_Click(object sender, EventArgs e)
{
saveRow();
}
public void saveRow()
{
// Created this as a separate function that can be called later
// ... with a simple 'saveRow();' call. (removes code repetition)
// Note: The '55555' and '66666' are to be replaced
// ... by your dataset names (55555) and table name (66666)
this.Validate();
this.55555BindingSource.EndEdit();
this.55555TableAdapter.Update(55555DataSet.66666);
}
To remove items, simply double-click the 'delete' icon and add the following line of code:
{
saveRow();
}

Related

Do action when clicking in a TreePanel node

Current Status:
I have a TreePanel and a GridPanel, where the former loads categories and the second articles, both from a database.
Using Visual Studio 2010, Ext.Net 2.5 and C#
The problem:
I'm not being able to make it so when you click in a category (TreePanel node), it does a certain function (in my case I want to fill the articles based on what category was clicked, as the nodes have ID)
I tried to use TreeNodeMouseClickEventArgs but it didn't work (in fact it created a class that has nothing), and since I just started to use this software (and language), I'm stuck.
Thanks a lot in advance.
Edit
The last code I tried was from MS website.
I found a way, and will share it:
On client side
After adding the treepanel, add a DirectEvents
<DirectEvents>
//Calls a function when doing click on a node
<ItemClick OnEvent="yourFunction">
<ExtraParams>
//Loads the nodeID
<ext:Parameter Name="NodeID" Value="record.data.id" Mode="Raw"/>
</ExtraParams>
</ItemClick>
</DirectEvents>
On server side
public void yourFunction(object sender, DirectEventArgs e)
{
//Ask the nodeID
string id = e.ExtraParams["NodeID"];
//Your function goes below
}

c# customizing controls on a save dialog -- how to disable parent folder button?

I am working from the sample project here: http://www.codeproject.com/Articles/8086/Extending-the-save-file-dialog-class-in-NET
I have hidden the address/location bar at the top and made other modifications but I can't for the life of me manage to disable the button that lets you go up to the parent folder. Ist is in the ToolbarWindow32 class which is the problem. This is what I have at the moment but it is not working:
int parentFolderWindow = GetDlgItem(parent, 0x440);
//Doesn't work
//ShowWindow((IntPtr)parentFolderWindow, SW_HIDE);
//40961 gathered from Spy++ watching messages when clicking on the control
// doesn't work
//SendMessage(parentFolderWindow, TB_ENABLEBUTTON, 40961, 0);
// doesn't work
//SendMessage(parentFolderWindow, TB_SETSTATE, 40961, 0);
//Comes back as '{static}', am I working with the wrong control maybe?
GetClassName((IntPtr)parentFolderWindow, lpClassName, (int)nLength);
Alternatively, if they do use the parent folder button and go where I don't want them to, I'm able to look at the new directory they land in, is there a way I can force the navigation to go back?
Edit: Added screenshot
//Comes back as '{static}', am I working with the wrong control maybe?
You know you are using the wrong control, you expected to see "ToolbarWindow32" back. A very significant problem, a common one for Codeproject.com code, is that this code cannot work anymore as posted. Windows has changed too much since 2004. Vista was the first version since then that added a completely new set of shell dialogs, they are based on IFileDialog. Much improved over its predecessor, in particular customizing the dialog is a lot cleaner through the IFileDialogCustomize interface. Not actually what you want to do, and customizations do not include tinkering with the navigation bar.
The IFileDialogEvents interface delivers events, the one you are looking for is the OnFolderChanging event. Designed to stop the user from navigating away from the current folder, the thing you really want to do.
While this looks good on paper, I should caution you about actually trying to use these interfaces. A common problem with anything related to the Windows shell is that they only made it easy to use from C++. The COM interfaces are the "unfriendly" kind, interfaces based on IUnknown without a type library you can use the easily add a reference to your C# or VB.NET project. Microsoft published the "Vista bridge" to make these interfaces usable from C# as well, it looks like this. Yes, yuck. Double yuck when you discover you have to do this twice, this only works on later Windows versions and there's a strong hint that you are trying to do this on XP (judging from the control ID you found).
This is simply not something you want to have to support. Since the alternative is so simple, use the supported .NET FileOk event instead. A Winforms example:
private void SaveButton_Click(object sender, EventArgs e) {
string requiredDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (var dlg = new SaveFileDialog()) {
dlg.InitialDirectory = requiredDir;
dlg.FileOk += (s, cea) => {
string selectedDir = System.IO.Path.GetDirectoryName(dlg.FileName);
if (string.Compare(requiredDir, selectedDir, StringComparison.OrdinalIgnoreCase) != 0) {
string msg = string.Format("Sorry, you cannot save to this directory.\r\nPlease select '{0}' instead", requiredDir);
MessageBox.Show(msg, "Invalid folder selection");
cea.Cancel = true;
}
};
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
I don't this is going to work. Even if you disable the button they can type ..\ and click save and it will take them up one level. You can't exactly disable the file name text box and maintain the functionality of the dialog.
You'd be better off either using the FolderBrowserDialog and setting it's RootFolder property and asking the user to type the filename in or auto generating it.
If the folder you are wanting to restrict the users to isn't an Environment.SpecialFolder Then you'll need to do some work to make the call to SHBrowseForFolder Manually using ILCreateFromPath to get a PIDLIST_ABSOLUTE for your path to pass to the BROWSEINFO.pidlRoot
You can reflect FolderBrowserDialog.RunDialog to see how to make that call.
Since you want such custom behaviors instead of developing low level code (that is likely yo break in the next versions of windows) you can try to develop your file picker form.
Basically it is a simple treeview + list view. Microsoft has a walk-through .
It will take you half a day but once you have your custom form you can define all behaviors you need without tricks and limits.

DataGridView to MS Access Database

I'm trying to update a database after the changes in a DataGridView.
After changing the code several times, I've come to this one-liner:
primeTableAdapter.Update((PrimeDataSet.PrimeDataTable)(primeDataSet.Tables["Prime"]));
The method returns the correct number of rows added / edited and entering the form again without closing the entire application everything seems fine. The new rows are kept in the DataGridView. However, the Access file is not changed one bit, so when I relaunch the application they disappear.
I've bound the DataGridView to the table using the "Add New Data Source" wizard and I'm using Visual Studio 2010.
I've also found the code at: Inserting data from a DataGridView to a database and it seems similar to what I have to do, but I wasn't able to translate the vb.net code to something that would compile. However, I'm fairly sure it is really, really close to what I have to do.
Later Edit: I had added the database to the project. By setting the Copy to Output Directory property to Copy if Newer the changes were persistent between sessions.
if you want to pay,you can use a third party (Spire dataExport ) it make things more easier : to save datagrid into msAccess :
private void btnExportToAccess_Click(object sender, EventArgs e)
{
Spire.DataExport.Access.AccessExport accessExport = new
Spire.DataExport.Access.AccessExport();
accessExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
accessExport.DataTable = this.dataGridView1.DataSource as DataTable;
accessExport.DatabaseName = #"..\..\ToMdb.mdb";
accessExport.TableName = "ExportFromDatatable";
accessExport.SaveToFile();
}
and Here a link where you can find more clarification

Automating Website/Web forms using C# in Firefox, using Visual Studio 2010?

If anyone can help me I'd appreciate it.
I'm working on a C# file in Visual Studio 2010 that I need to be able to test a website with multiple form pages, for the purpose of an example we'll refer to them all as 1.aspx, 2.aspx etc.
I've my code that fills out the first page (1.aspx) fine, and click the "continue" button to load the next page, but when it gets to 2.aspx it won't continue to fill out the form.
We'll say an element on the 2.aspx page is called "DOB". On trying to run from the start (I've all the pages form data in the one .cs file) I get an error like "DOB does not exist in the current context".
Anyone's insight into this would be really appreciated!
In all honesty, it sounds like you might be better off using the WatiN Framework. I have been writing automation with it for years and the way that it is implemented and its ease-of-use make it worth the slight learning curve.
Just to add a bit more to the answer; and yes, this is pseudo-code:
[Test]
public void Should_attach_to_browser()
{
ExecuteTest(browser =>
{
browser.GoTo(NewWindowUri);
browser.Link(Find.First()).Click();
var findBy = Find.ByTitle("New window");
var newWindow = Browswer.AttachTo(browser.GetType(), findBy);
newWindow.Close();
});
}
In the code above, note the Browser.AttachTo(browser.GetType(), findBy); method. Based on what I have understood of your question, the .AttachTo() method would work well since you would be able to take the focus off the current form and assign it to the next in your work/execution flow.

Drag & drop of a dynamically created shortcut

I have a C# application that creates shortcuts to launch other programs with specific arguments and initial directories. I would like the user to be able to drag a shortcut from the Windows form and drop it anywhere relevant like the desktop, the start menu, and so on but I don't really know how to handle that, could anyone point me in the right direction?
I have seen a few samples using PInvoke and IShellLink like this one, or read answers on SO like here, which already help create shortcuts and save them in a .lnk file. I assume I have to hand over data in a DoDragDrop() call when the user initiates a drag operation, for example by handling a MouseDown signal. That's as far as I got, I suppose I need to know exactly which type the target is expecting to accept the drop, and how to serialize the shortcut, but couldn't find any information on that part.
Perhaps another option would be to get the location of the drop, and manage that from my application, but there again I'm a bit clueless as how to do that.
The framework version is currently 3.5, and I'm only considering Windows platforms.
Thanks in advance for your help!
Update/Solution:
Using the ShellLink code mentioned above to create a temporary shortcut file, I simply used DataObject for the drag and drop, like in the following example:
private void picShortcut_MouseDown(object sender, MouseEventArgs e)
{
ShellLink link = new ShellLink();
// Creates the shortcut:
link.Target = txtTarget.Text;
link.Arguments = txtArguments.Text;
link.Description = txtDescription.Text;
link.IconPath = txtIconFile.Text;
link.IconIndex = (txtIconIndex.Text.Length > 0 ?
System.Int32.Parse(txtIconIndex.Text) : 0);
link.Save("tmp.lnk");
// Starts the drag-and-drop operation:
DataObject shortcut = new DataObject();
StringCollection files = new StringCollection();
files.Add(Path.GetFullPath("tmp.lnk"));
shortcut.SetFileDropList(files);
picShortcut.DoDragDrop(shortcut, DragDropEffects.Copy);
}
Quite complicated if you consider the PInvoke code (not shown here), and I still need to create this temporary file with the target name. If anyone knows a... erm, shortcut, it's welcome! Perhaps by porting the code for which John Knoeller gave a link (thanks!).
Raymond Chen did a whole article on this very topic on his blog check out dragging a virtual file
I answered a question sort of similar to this on a previous thread. This might be a starting point for you.
Drag and Drop link

Categories

Resources