如何通过此事件验证我的表单我有6个字段要输入我不希望任何字段为空 [英] How can I validate my form with this event I have 6 field to be entered I dont want any feild to be empty

查看:59
本文介绍了如何通过此事件验证我的表单我有6个字段要输入我不希望任何字段为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into course(crs_id,crs_name,crs_detail,credit_hrs) values(@id,@name,@detail,@hours)";

            cmd.Parameters.AddWithValue("@id",courseidtxt.Text);
            cmd.Parameters.AddWithValue("@name",coursenametxt.Text);
            cmd.Parameters.AddWithValue("@detail",coursedetailstxt.Text);
            cmd.Parameters.AddWithValue("@hours", credithorstxt.Text);


            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();



            cmd1 = new SqlCommand();
            cmd1.Connection = con;
            cmd1.CommandType = CommandType.Text;
            cmd1.CommandText = "insert into offer_course(crs_id,dept_id) values(@id,@did)";

            cmd1.Parameters.AddWithValue("@id", int.Parse(courseidtxt.Text));
            cmd1.Parameters.AddWithValue("@did",int.Parse(deptidtxt.Text));
           

            con.Open();
            cmd1.ExecuteNonQuery();
            con.Close();





我尝试过:



i已经搜索了很长时间但没有得到很好的解决方案请帮助我



What I have tried:

i have been searching for long time but didnt get good solution please help me

推荐答案

在方法的顶部 - 长在您到达数据库附近之前 - 验证您的所有输入:

At the top of the method - long before you get anywhere near the database - validate all your inputs:
if (string.IsNullOrWhitespace(coursenametxt.Text))
   {
   ... Report problem to user
   return;
   }
if (string.IsNullOrWhiteSpace(coursedetailstxt.Text))
   {
   ... Report problem to user
   return;
   }
...
int courseID;
if (!int.TryParse(courseidtxt.Text, out courseID))
   {
   ... Report problem to user
   return;
   }
int deptID;
if (!int.TryParse(deptidtxt.Text, out deptID))
   {
   ... Report problem to user
   return;
   }



然后,您可以通过参数传递已转换为SQL的字符串和整数值。



请按照命名惯例!

它应该是 courseName (或 tbCourseName )不是 coursenametxt 等等。


如果您使用的是ASP.NET,那么只需去找



验证 - RequiredFieldValidator


来自工具箱的
。设置所需的属性,如控制到验证和错误消息,以便在它引发警报时显示。无需在html端(javascript)或后面的代码进行任何编码。
If you are using ASP.NET then simply go for

Validation - RequiredFieldValidator

from toolbox. Set required properties like 'control to validate' and 'error message' to display in case of it raises alert. No need to do any kind of coding either at html side (javascript) or code behind.


这篇关于如何通过此事件验证我的表单我有6个字段要输入我不希望任何字段为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆