开发者

my sql table column accepting "0" as value

I have two tables one is members table with columns member id , member first name, member last name. I have another table guest passes with columns guest pass id and member id and issue date .

I have a list view that will displays guest passes details (I.e) like member name and issue date and I have two text boxes those are for entering member name and issue date .

开发者_运维知识库

member name text box is auto complete text box that working fine....

but the problem is when I am entering the name that is not in member table at this time it will accept and displays a blank field in list view in member name column and member id is stored as "0" in guest pass table ......

I don't want to display the member name empty blank and I don t want to store "0" in guest pass table

and this is the insert statement

          sql2 = @"INSERT INTO guestpasses(member_Id,guestPass_IssueDate)";
          sql2 += " VALUES(";
          sql2 += "'" + tbCGuestPassesMemberId.Text + "'";
          sql2 += ",'" + tbIssueDate.Text + "'";

guestpassmemberId = memberid

is there any validation that need to be done

and this is the auto complete text box statement

           sql = @"SELECT member_Id FROM members WHERE  concat(member_Firstname,'',member_Lastname) ='" + tbMemberName.Text+"'";

 dt = GetData(sql, mf);

         if (dt != null)
         {
            if (dt.Rows.Count > 0)
            {
                tbCGuestPassesMemberId.Text  = Convert.ToInt32(dt.Rows[0]  ["member_Id"]).ToString();
            }
         }

can any one help me on this ... is there any type of validation with sql query

pls help me .....


You can validate the values before passing them to the INSERT. Additionally you can also set a constraint to validate versus a regular expression in SQL.

SQL constraints

To validate before inserting you should have something like this:

  private void validateData(Long memberId) {
       //Pseudo code Depends on how you are connecting to your database...
       SQLQuery query = getQuery("existsMemberId");
       query.setParameter("memberId");
       executeQuery(query);
       // If the query returns something then the reference exists and it is ok to proceed
  }

In the file you are storing your queries...

   #existsMemberId
   select 1
   from members mem
   where mem.id = :id <-- parameter

Additionally you should make foreign key constraint between members and guest passes with ID as the foreign key:

   ALTER TABLE GuestPasses
   ADD CONSTRAINT fk_memberId
   FOREIGN KEY (id)
   REFERENCES Members(id)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜