简体   繁体   中英

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. 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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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