Hi,

see my comment below.

In article <94483@palm-dev-forum>, "Sean Rogers" <[EMAIL PROTECTED]> 
wrote:

> I'm having problems with my database usage.  At the moment, I'm just trying 
> to save a name entered by the user in a text field and display that saved 
> name in a different form.  I know databases aren't necessary for trivial 
> things like this, but I'm starting out easy to get my head around databases. 
> Can anyone help me out?
> 
> First, I've got a struct like this:
> 
> typedef struct {
>     char TrackName[20];
> } DBRecord;
> 
> 
> Here's my saving code:
> 
> FieldPtr      fld;
> FormPtr               frm;
> Int           itemIndex;
> CharPtr               newTrackName;
> VoidHand      h;
> UInt16 index = 0;
> DBRecordType  r;
> 
> frm = FrmGetActiveForm();
> itemIndex = FrmGetObjectIndex(frm, TrackRecordNameField);
> fld = FrmGetObjectPtr(frm, itemIndex);
> newTrackName = FldGetTextPtr(fld);
> h = DmNewRecord(DB, &index, sizeof(r));
> StrCopy(r.TrackName, newTrackName);

You should use StrNCopy instead StrCopy. The user should enter a string 
longer than 20 chars.

> if (h) {
>     VoidPtr p = MemHandleLock(h);
>     Err err = DmWrite(p, 0, &r, sizeof(r));
>     MemPtrUnlock(p);
>     DmReleaseRecord(VictoryDB, index, true);
> }
> 
> 
> And here's my loading code:
> 
> FieldPtr      fld;
> FormPtr               frm;
> Int           itemIndex;
> CharPtr               newTrackName = 0;
> VoidHand      h;
> UInt          index = 0;
> 
> frm = FrmGetActiveForm();
> itemIndex = FrmGetObjectIndex(frm, ShowTrackNameField);
> fld = FrmGetObjectPtr(frm, itemIndex);
> // get the handle to the record
> h = DmQueryRecord(VictoryDB, index);
> if (h) {
>     DBRecordType * p = (DBRecordType *) MemHandleLock(h);
>     StrCopy(newTrackName, p->TrackName);

You have to allocate the pointer before copying something in it.

>     MemPtrUnlock(p);
> }
> FldSetTextPtr(fld, newTrackName);
> FldDrawField(fld);
> 

If you use the FldSetTextPtr function, don't forgot to set the EditField 
as non-editable.
Don't forgot to release the newTrackName pointer after to avoid memory 
leaks.

Ludovic

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to