如何验证数据库中没有的文本框值 [英] How to validate text box value not there in database

查看:93
本文介绍了如何验证数据库中没有的文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnSearch_Click(object sender, EventArgs e)
        {
String ConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
            SqlConnection con = new SqlConnection(ConnString);
           
            if (txtslipno.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + textbox1.Text + "'", con);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();
            }

            if (txtzone.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Zone  = '" + textbox2.Text + "'", con1);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();


            }
            if (txtsetion.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Section  = '" + textbox3.Text + "'", con2);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();
            }
        }




上面三个文本框中的
就在那里。



滑动没有textbox1区域textbox2部分textbox3





i想从上面验证,如果textbox1滑不了输入值不在数据库中显示要使用的消息未找到记录





同样如果textbox2区域输入的值不在数据库中显示消息未找到用户记录



类似如果textbox3部分输入值不在数据库中显示消息未找到用户记录





我在上面的代码中怎么做。



我尝试了什么:



请参阅描述部分。



from the above three text box is there.

Slip no textbox1 Zone textbox2 Section textbox3


i want to validate from the above, if textbox1 slip no entered value is not in database shows the message to use Records not found


similarly if textbox2 zone entered value is not in database shows message to user records not found

similarly if textbox3 section entered value is not in database shows message to user records not found


for that how to do in my above code.

What I have tried:

Refer to describe section.

推荐答案

SqlCommand cmd = new SqlCommand("select * from Sample where Slipno = '" + textbox1.Text + "'", con);



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


正如ppolymorphe所提到的,您的代码存在SQL注入问题,您需要更新该代码的代码。 />


考虑到你的问题,你可以尝试以下方法 - 如果你有任何匹配的记录,你会收到数据库电话,否则你什么都不返回,所以检查记录的大小你回来了。





As mentioned by ppolymorphe your code has an issue with SQL injection and you need to update code for that one.

Considering your question you can try something below - if you have any matching record you will receive it back with database call otherwise you will simply return nothing, so check the size of records you get back as below.


if (txtslipno.Text.Trim() != "")
{
    SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + textbox1.Text + "'", con);
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.SelectCommand = cmd;
    da.Fill(ds);

    if(ds.Tables != null && ds.Tables[0].Rows.Count > 0)
    {
       //you got DS populated with your slip number  
        grdRpt.DataSource = ds;
        grdRpt.DataBind();
    }
    else
    { 
        //no record found for your slip number 
    }
    con.Close();
}


这篇关于如何验证数据库中没有的文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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