简体   繁体   English

如何检查数据库表中的列是否为空,并在此基础上如何重定向到其他页面?

[英]How to check whether column in table in database is null or not and on that basis how to redirect to other page?

I am trying to check whether the column pw in table tbla is null or not on input to textbox txtjournalname on click to linkbutton lblviewentry. 我试图检查表tbla中的pw列是否为空,或者在单击链接按钮lblviewentry时输入到文本框txtjournalname时是否为空。 I have two columns in table tbla firstone named as jn and second one named as pw and both are of nvarchar(50) datatype.I have stored some data on both the column. 我在表tbla中有两列,第一个列名为jn,第二个列名为pw,并且都属于nvarchar(50)数据类型。我在这两个列上都存储了一些数据。 Now when i input one of the value stored in column jn and along with that if there is also value in column pw in same row as Jn there should be redirection to Default15.aspx page and as i input the value which is stored in jn column and if there is also value in pw column of same row there is redirection to page Default15.aspx as required.Now what i require is ,if i input any sting to textbox txtjournalname not stored in jn column and if there is no pw column of same row as jn there should be redirection to View Entry.aspx here instead of redirecting to View Entry.aspx it shows error There is no row at position 0. 现在,当我输入存储在jn列中的值之一,并且如果它与Jn在同一行中的pw列中也有值时,应该重定向到Default15.aspx页,并且当我输入存储在jn列中的值时如果同一行的pw列中也有值,则可以根据需要重定向到页面Default15.aspx。现在我需要的是,如果我向jn列中未存储的文本框txtjournalname输入了任何字符串,并且没有pw列的与jn相同的行,这里应该重定向到View Entry.aspx,而不是重定向到View Entry.aspx,这表明错误在位置0没有行。

Below is behind code on button and method i used. 以下是我使用的按钮和方法上的代码。

on button 在按钮上

  protected void lblviewentry_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(txtjournalname.Text))
    {
        DataTable dt = j.getjpwd(txtjournalname.Text);
        if (dt.Rows[0][1].ToString() != "")
        {
            Response.Redirect("Default15.aspx");

        }
        else
        {
            Response.Redirect("View Entry.aspx");
        }
    }
}

Method Used 使用方法

    public DataTable getjpwd(string journalname)
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
    string sql = "select*from tbla where jn=@jn";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@jn", journalname);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

with reference of row count in data table you can redirect user to another page 参照数据表中的行数,您可以将用户重定向到另一页

 if (dt.Rows.Count>0)//here  you  will get weather your data-table have any row or not
 {
            Response.Redirect("Default15.aspx");    
 }

if u want to check by field 如果你想按领域检查

if(!String.IsNullOrEmpty(dt.Rows[0]["TITLE"].ToString()))
{

}

如果索引0上没有任何内容,则数据表为null或行计数为0尝试使用if(dt!= null && dt.Rows.Count> 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM