主题:各位高手帮我看看这添加代码有什么问题
procedure Tfzj.FormShow(Sender: TObject);
begin
fdm.at_stu.Open;
fdm.at_stu.Append;
dbedit1.SetFocus;
end;
procedure Tfzj.bt_addClick(Sender: TObject);
begin
if trim(dbedit1.Text)='' then //如果学号为空,则提示输入学号
begin
application.MessageBox('学号不能为空,请输入学号','提示',mb_ok);
dbedit1.SetFocus;
exit;
end;
//如果班级号为空,则提示输入班级号
if trim(dbedit2.Text)='' then
begin
application.MessageBox('班级号不能为空,请输入班级号','提示',mb_ok);
dbedit2.SetFocus;
exit;
end;
try
fdm.at_stu.Post; //保存记录
fdm.at_stu.Refresh;
showmessage('添加成功');
except //学号重复异常处理,学号为主键
messagedlg('检查记录是否重复了,不能保存',mtinformation,[mbok],0);
fdm.at_stu.Cancel;
dbedit1.SetFocus;
exit;
end;
fdm.at_stu.Append; //增加一条新记录
dbedit1.SetFocus; //设置光标默认在dbedit1
end;
刚运行时出错了,错误如下:
Project Project1.exe raised exception class EVariantTypeCastError with message'Could not convert variant of type(Null)into type(String)'.Process stoped.Use Step or Run to continue.
再次运行,当在增加窗体里添加相同的学号时出错了:
Project Project1.exe raised exception class EOleException with message
'[Microsoft][ODBC SQL Server Driver][SQL Server]违反了PRIMARY KEY约束'PK_Student'.不能在对象't_student'中插入重复键。'.Process stopped.Use Step or Run to continue.
注:我已经用异常处理了学号重复问题了,为什么还出这种错呢?
begin
fdm.at_stu.Open;
fdm.at_stu.Append;
dbedit1.SetFocus;
end;
procedure Tfzj.bt_addClick(Sender: TObject);
begin
if trim(dbedit1.Text)='' then //如果学号为空,则提示输入学号
begin
application.MessageBox('学号不能为空,请输入学号','提示',mb_ok);
dbedit1.SetFocus;
exit;
end;
//如果班级号为空,则提示输入班级号
if trim(dbedit2.Text)='' then
begin
application.MessageBox('班级号不能为空,请输入班级号','提示',mb_ok);
dbedit2.SetFocus;
exit;
end;
try
fdm.at_stu.Post; //保存记录
fdm.at_stu.Refresh;
showmessage('添加成功');
except //学号重复异常处理,学号为主键
messagedlg('检查记录是否重复了,不能保存',mtinformation,[mbok],0);
fdm.at_stu.Cancel;
dbedit1.SetFocus;
exit;
end;
fdm.at_stu.Append; //增加一条新记录
dbedit1.SetFocus; //设置光标默认在dbedit1
end;
刚运行时出错了,错误如下:
Project Project1.exe raised exception class EVariantTypeCastError with message'Could not convert variant of type(Null)into type(String)'.Process stoped.Use Step or Run to continue.
再次运行,当在增加窗体里添加相同的学号时出错了:
Project Project1.exe raised exception class EOleException with message
'[Microsoft][ODBC SQL Server Driver][SQL Server]违反了PRIMARY KEY约束'PK_Student'.不能在对象't_student'中插入重复键。'.Process stopped.Use Step or Run to continue.
注:我已经用异常处理了学号重复问题了,为什么还出这种错呢?