display an alert depending value returned by @@rowcount from stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanboy
    New Member
    • Jul 2010
    • 20

    #1

    display an alert depending value returned by @@rowcount from stored procedure

    I'm using ASP.net2.0,SQLs erver2005.i have a stored procedure which returns the number of rows affected by a search based on search key.i want that if no rows are returned,an alert box with custom message should appear.

    When i run the code below, i get an error


    Error 2 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)



    Code:
    protected void btnSearch_Click(object sender, EventArgs e)
        {
            string constring = ConfigurationManager.AppSettings.Get("con").ToString();
            SqlConnection conn = new SqlConnection(constring);
            conn.Open();
    
            SqlCommand check = new SqlCommand("spSearchCombo", conn);
            check.CommandType = CommandType.StoredProcedure;
    
    
            check.Parameters.Add(new SqlParameter("@searchBy", SqlDbType.NVarChar, 50));
            check.Parameters["@searchBy"].Value = ddlSearchBy.SelectedItem.Value.ToString();
    
            
            check.Parameters.Add(new SqlParameter("@searchKey", SqlDbType.NVarChar, 50));
            check.Parameters["@searchKey"].Value = txtSearch.Text;
    
    //@no_rows=@@rowcount in stored procedure
    
            check.Parameters.Add(new SqlParameter("@no_rows",SqlDbType.Int));
            check.Parameters["@no_rows"].Direction = ParameterDirection.Output;
    
            int result = check.Parameters["@no_rows"].Value;
    
            if (result >0)
           {
                SqlDataAdapter da = new SqlDataAdapter(check);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.dgv1.DataSource = ds.Tables[0].DefaultView;
    
                dgv1.DataBind();
    
                conn.Close();
           }
    
            else
            {
                Response.Redirect("<script language='javascript'>alert('No record  matches your criteria');</script>");
            }
            
        }
    when i change the code to

    check.Parameter s["@no_rows"].Direction = ParameterDirect ion.Output;

    string result = check.Parameter s["@no_rows"].Value.ToString ();

    if (result !="0")
    followed by data adapter code i get
    Object reference not set to an instance of an object. at run time

    Plz help me out.plz guide me as to how them 'check' command can be used inside 'if' block to implement my need.

    Thanking u in advance
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    Couple of things I observe
    1.You are missing execute statement before
    int result = check.Parameter s["@no_rows"].Value;
    also u can check
    if(check.Parame ters["@no_rows"].Value!=DBNull. Value)

    Comment

    Working...