2011-02-23 2 views
-3
using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.SqlClient; 

public partial class Bookcheck : System.Web.UI.Page 
    { 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlDataAdapter da=new SqlDataAdapter(); 
     SqlConnection cnn=new SqlConnection(); 
     DataSet ds = new DataSet(); 
     string constr = null; 
     SqlCommand cmd = new SqlCommand(); 
     if (IsValid != null) 
     { 
      constr = @"Data Source=DEVI\SQLEXPRESS; Initial Catalog =librarymanagement; 
Integrated Security=SSPI"; 
      cnn.ConnectionString = constr; 
      try 
      { 
       if (cnn.State != ConnectionState.Open) 
        cnn.Open(); 
      } 
      catch (Exception ex) 
      { 
       string str1 = null; 
       str1 = ex.ToString(); 
      } 
      cmd.Connection = cnn ; 

      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "spbookcheck"; 
      cmd.Parameters.Clear(); 
      cmd.Parameters.AddWithValue("bookid", txtid.Text); 
      cmd.Parameters.AddWithValue("Nameofthebook", txtnb.Text); 
      da.SelectCommand=cmd; 
      try 
      { 
       da.Fill(ds); 
      } 
      catch (Exception ex) 
      { 

       string strErrMsg = ex.Message; 
       // throw new ApplicationException 
      } 
      finally 
      { 
       da.Dispose(); 
       cmd.Dispose(); 
       cnn.Close(); 
       cnn.Dispose(); 
      } 
      if (ds.Tables[0].Rows.Count > 0) 
      { 
       Msg.Text = "bookcheck successfully"; 
       Response.Redirect("Issueofbook.aspx"); 
      } 
      else 
      { 
       Msg.Text="bookcheck failed"; 
      } 
     } 



    } 

} 

Mon erreurAsp.net comment corriger l'erreur

Index hors exception gamme du unhandle par l'utilisateur ne peut pas trouver tableau 0

+3

Peut-être pouvez-vous nous dire quelle erreur vous obtenez. –

+0

@joel Etherton L'enregistrement ne sont pas insérer, puis l'erreur est hors de portée exception jeter dans l'utilisateur – kannan

+0

@joel Etherton la page sont exécutés, puis n'importe quel insert insert REAS m'aider comment puis-je changer .... – kannan

Répondre

2

Cette ligne:

if (ds.Tables[0].Rows.Count > 0) 

suppose qu'il existe des tables dans votre ensemble de données. Vous devez vérifier que ds.Tables.Count est> 0 avant d'essayer d'accéder à une table.

+0

Bonne réponse Dave! –