|
There's no reliable way to tell. If the last record you created gets deleted, how are you going to know that??
Normally, you either create a blank, or partially filled, or even completely filled, record in the database, except for the autogenerated number, get the ID after the data is inserted and use that for the remaining operations, if any.
|
|
|
|
|
well whats the correct syntax in c# to do something like this ?
int itemvalue = this.bookdbDataSet.Tables[0].Rows(bookdbDataSet.Tables[0].Rows.Count - 1)("ID");
that should bet the id value of the last item in the dataset which is what i'm seeking.
thanks for your help by the way...
|
|
|
|
|
there seems to be no safe way in MS Access, Google around and you will find things like this one[^].
|
|
|
|
|
I figured it out thanks for the help guys.
Here for anyone curious about how to do something like this...
this.locationTextBox.Text = this.bookdbDataSet.Tables[0].Rows[bookdbDataSet.Tables[0].Rows.Count - 1]["ID"].ToString();
|
|
|
|
|
Yeah, that gets you the last "currently used" row. It will NOT give you the ID of the next record that's going to get created. Because Access doesn't support true stored procedures, there's no way to reliably get the ID of the records that was just created.
|
|
|
|
|
Okay, thanks again to you and everyone else. Let me explain what I'm trying to accomplish and whatever the best approach to this is where I'll direct my attention. I seen someone mention the GUID's which sounds great however I don't think it will work with how I intend to use the number.
See my mother was using pure access to enter her books manually for school. She has well over 800+ books for Kindergarten. Anyway, in order to find her books she takes and assigns them a number starting with 1, then 2 and 3 and so on. Lets say that book 3 gets lost. It is no big deal per say because if she had 800 books and decided to replace book 3 it would get a new number of 801. Basically what I'm saying is it doesn't have to be the TRUE number or records it just has to be a unique number but in sequence...
With the above being said it is still advised to use GUID's or is there a way to take and look at a field in the database. say LOCATIONNUMBER and somehow get the greatest value in there. Once i have the greatest value simply add 1 to it and that would be my next suggested book number?
Sorry to be a pest and hopefully I'm not seen as one just trying to figure this out so we can start filing the books she lost in the flood again.
thanks for your help,
tony
|
|
|
|
|
Easy. Retire the lost book with a flagged field or something, maybe even a description as to why it was retired. Enter the information into the database, let the database assign the book number, you can then retrieve the ID number of the last record inserted using this[^]. Then you can show her what the number assigned was.
|
|
|
|
|
Thanks that is exactly what I wish to do.
I tried adding this on the additem button click but i keep getting can not interrupt token @ at position 8
int idnumber =0;
idnumber = Convert.ToInt32(bookdbDataSet.BookList.Select("Select @@IDENTITY"));
idnumber += 1;
|
|
|
|
|
What does your connection string look like? This little trick only works on Jet 4.0 databases.
|
|
|
|
|
I actually got it how I want it now. Thanks again for putting up with me.
What I ended up doing was taking your advice of the whole retire the book show her the last number generated idea. That is essentially what she wanted so I went with that instead of making it more complex than it needed to be.
What I ended up doing was adding a checkbox for the retire and also adding that column into the database. Then based on your advice I took and generated a message box telling her what the last item number was when she clicks save. This works great cause if she removes the book (I removed this feature because I feel it shouldn't be removed but simply either updated or retired), but if she took and removed it it would still show her the correct next number. In order to do that I simply called on the previous code i posted about getting the row count -1's ID value and then adding 1 to it.
thanks again.
|
|
|
|
|
Auto numbers s*ck; don't use them. Use GUIDs instead; you can generate them in the client code and then pass them to the database.
|
|
|
|
|
Hi ive just started using emguCV the .net implement of openCV
Im very new to c# and coding in general so i decided to do some Video analytics instead of the usual "Hello World" just to break me in.
I have a picture box on my form which shows the camera output
im just after a bit of advice on this bit of code in my project. I cant understand why the code below works and refresh the image fine, yet the code below the code below doesnt, even though in my head it should work fine
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
<br />
Capture capture = new Capture();
Application.Idle += new EventHandler(delegate(object sender1, EventArgs ef)<br />
{
pictureBox1.Image = capture.QueryFrame().ToBitmap();
});<br />
<br />
<br />
}<br />
and my revised version of it doesnt work see below
private void Form1_Load(object sender, EventArgs e)
{
Capture capture = new Capture();
while (true)
{
pictureBox1.Image = capture.QueryFrame().ToBitmap();
}
}
the first bit of code is pretty much straight off a tut and it works fine but I dont understand what its doing when it does this
Application.Idle += new EventHandler(delegate(object sender1, EventArgs ef)
but if i dont put
pictureBox1.Image = capture.QueryFrame().ToBitmap() in a loop it works but obviosly its just a static image. so i thought if I loop it it would just keep refreshing as fast as it can get a frame from the capture.
If anyonce can help me or even understand what im on about it would be much apreciated.
|
|
|
|
|
How many frames do you want per second?
And how many can your webcam deliver?
you should not put loop-forever kind of things in any of the event handlers, they are supposed to be very quick and return control to Windows so the GUI remains alive, which means a form can be moved, resized, minimized/maximized, etc and keeps repainting itself when necessary.
slow operations, or loop-forever kind of things belong in a separate thread (maybe from ThreadPool, or a BackgroundWorker) or in a timer handler. For up to 30 frames per second, I would recommend a System.Windows.Forms.Timer
If all of the above is a mystery, please consider buying and studying an introductory book on C#.
You might also find some of my little articles interesting:
animation1[^]
crossthreads1[^]
|
|
|
|
|
Hey sorry while i was typing you got in first, thanks for getting back, I would like around 10-25 fps Ive used threads in python before and Ill look into them, I have a few C# ebooks and have been reading up but I thought id give some stuff ago.
Ill look into timers.
Thanks for your help Luc
|
|
|
|
|
Sorry i forgot to memtion when i run the second bit of code it compiles fine but the form never loads and watching task manager for the app shows memory getting eaten quickly and keeps on rising I havnt tested to see if it keeps on going for ever because im pretty sure itll consume all the memory in my pc and then im guessing crash it out dramataically. the working bit of code only chews memory up till about 200mb then will go back to around 60mb then reapeats until you close the program.
|
|
|
|
|
that is because you never dispose of your images. Instead of:
pictureBox1.Image = capture.QueryFrame().ToBitmap();
do this:
Image oldImage=pictureBox1.Image;
pictureBox1.Image = capture.QueryFrame().ToBitmap();
if (oldImage!=null) oldImage.Dispose();
|
|
|
|
|
Thats definitly sorted the memory problem out now its just taking 13mb - 14mb while running,
im gonna play around with this somemore tommorrow now.
thanks Luc
|
|
|
|
|
you're welcome.
|
|
|
|
|
hi, im newbie in c# encapsulation,recently i just study an article on the encapsulation for hiding its field in class so that it protect the data by outside world...but im confuse...i did try some example.. .Below is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EncapsulationExample
{
class Program
{
static void Main(string[] args)
{
School d = new School();
d.SetSchool("Alan");
Console.WriteLine("The School is :" + d.GetSchool());
d.SetSchool("Sing");//changing parameter will also change private //variable name as well
Console.WriteLine("The School is :" + d.GetSchool());
d.testing = "month";
Console.WriteLine("The testing is :" + d.testing);
}
}
public class School
{
private string name;
public string testing;
// Accessor.
public string GetSchool()
{
return name;
}
// Mutator.
public void SetSchool(string a)
{
name=a;
}
}
}
...here is my confusion..even the private variable name has been declared as private, but the variable can also be changed by assigning different parameter in SetSchool method...which is same as public string testing where we can change the variable of testing as well.What is the difference with using method and without method(access variable directly from public variable) ??....What does "protect the data by outside world" actually means? any explanation with coding would be much appreciated..Thanks in advance
|
|
|
|
|
First, C# has Properties that replace explicit getters and setters:
public string SchoolName
{
get { return name; }
set { name = value; }
}
and then instead of calling
d.SetSchool("Alan");
Console.WriteLine("The School is :" + d.GetSchool());
you just do
d.SchoolName = "Alan";
Console.WriteLine("The School is :" + d.SchoolName);
Second, the reason to do this is to separate the interface of the class from the implementation of the class. The interface is what is presented to other classes when they use your class, in other words the interface is what the class can do. The implementation of the class is the internal stuff that other classes don't need to know about, it's how a class does what it does. To maximize code re-usability classes shouldn't know or care about the how, all they need to know is the what.
This goes along with:
binn019 wrote: protect the data by outside world
This means that you don't allow other classes to access any stuff that they don't need to. Your School class stores the name of the school. Do other objects need to know how it stores that? No, they only need to know that it does store it. All other classes need to know is that they set the value d.SchoolName to something to change the name and use that same value to retrieve the name later. Internally it's up to School to store it however it makes sense. Right now it's a string but what if later on you want to change it? If you use proper encapsulation then you can change how School stores the name without affecting any other class, but what if you want to change the variable testing ? Then you have to go through all of the files in your code that use that variable and update them as well. This is the essence of encapsulation.
Example: The way things are right now you can change the variable name to an int without breaking anything:
public class School
{
private int name;
public string testing;
public string GetSchool()
{
return name.ToString();
}
public void SetSchool(string a)
{
if (!int.TryParse(a, out name))
{
name=0;
}
}
}
Why? who care. That's not relevant; what is relevant is that you can change name to anything you want without having to change any of the code in main because name is properly encapsulated. You cannot do the same thing with variable . If you change it to an int then you also have to change the code in main to reflect that change.
|
|
|
|
|
thank you Jimmanuel, you have clear some of my doubts on encapsulation. As we can do all our changes on School class without making any changes on other class. we can set any conditions in setters without changing the code from the main function.
|
|
|
|
|
Hello,
i am creating a software, which loads image(fingerprint) in picture box after i want to draw circles on the picturebox on the place i have clicked mouse. I created events on pictureBox1 MouseDown(for clicking) and Paint(for drawing). When i load my form and click on pictureBox1 nothing happens, no circles are drawn, BUT after i load image in pictureBox1 all the circles that should have been drawn before appear on picturebox with loaded image. If i click on picturebox after loading image nothing gets drawn and only if re-load image new circles appear. What am i doing wrong?
Thanks for answer
here is my code and screenshot:
Screenshot[^]
namespace fingerprint_test_v04
{
public partial class Form1 : Form
{
private ArrayList myPts = new ArrayList();
private Bitmap img = null;
public Form1()
{
InitializeComponent();
this.Text = "FingerprintzzzZZzzZZzzz";
}
public void MyPaintHandler(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}
private void openFPR2ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image = img;
img = new Bitmap(open.FileName);
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = img;
img = new Bitmap(open.FileName);
angle.Enabled = true;
angle_ValueChanged(null, EventArgs.Empty);
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
private void angle_ValueChanged(object sender, System.EventArgs e)
{
if (angle.Value > 359.9m)
{
angle.Value = 0;
return;
}
if (angle.Value < 0.0m)
{
angle.Value = 359;
return;
}
Image oldImage = pictureBox1.Image;
pictureBox1.Image = Program.RotateImage(img, (float)angle.Value);
if (oldImage != null)
{
oldImage.Dispose();
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
this.Controls.Add(pictureBox1);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Graphics g = Graphics.FromHwnd(this.Handle);
myPts.Add(new Point(e.X, e.Y));
Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
foreach (Point p in myPts)
g.DrawEllipse(new Pen(Color.Red, 3), p.X, p.Y, 10, 10);
}
}
}
|
|
|
|
|
Hi,
while not spot on, I suggest you read this little article[^] which will give you the basics of painting.
|
|
|
|
|
You are drawing on the pictureBox image. if no image is loaded nothing is drawn, but the point(s) where you clicked are loaded.
when you load an image the paint event gets called and draws a circle for each point in
myPts.
|
|
|
|
|
Hi,
I'm gonna create a BackgroundWorker with an anonymous method.
I've written the following code :
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(
() =>
{
int i = 0;
foreach (var item in query2)
{
....
....
}
}
);
But Delegate 'System.ComponentModel.DoWorkEventHandler' does not take '0' arguments and I have to pass two objects to the anonymous method : object sender, DoWorkEventArgs e
Could you please guide me, how I can do it ?
Thanks.
|
|
|
|