2010-08-02 5 views
0

Je tente de lier une instruction a sql à un DataGrid.Syntaxe incorrecte près de '*'. Application ASP.NET se connectant à SQL à l'aide d'ADO.net

ayant quelques problèmes:

<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 

    private const string _strConStr = "Data Source=Sqlserver01; Initial Catalog=Igors_Test; Integrated Security=SSPI"; 
    private const string _strSql = "Selct * from stotickets where [Status] = 'Active'"; 
    private SqlDataAdapter da; 
    private DataSet ds; 

    void Page_Load(object sender, System.EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      LoadData(); 
      dgTicketInfo.DataSource = ds; 
      dgTicketInfo.DataBind(); 
     } 

    } 

    void LoadData() 
    { 
     da = new SqlDataAdapter(_strSql, _strConStr); 
     ds = new DataSet(); 
     da.Fill(ds); 
    } 

</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:DataGrid runat="server" ID="dgTicketInfo" /> 
    </div> 
    </form> 
</body> 
</html> 

erreur:

Server Error in '/' Application. 
-------------------------------------------------------------------------------- 

Incorrect syntax near '*'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '*'. 

Source Error: 


Line 27:   da = new SqlDataAdapter(_strSql, _strConStr); 
Line 28:   ds = new DataSet(); 
Line 29:   da.Fill(ds); 
Line 30:  } 
Line 31: 

Source File: d:\http\netdev\Grid_Stoenv.aspx Line: 29 

Stack Trace: 


[SqlException (0x80131904): Incorrect syntax near '*'.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +212 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +245 
    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2811 
    System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +58 
    System.Data.SqlClient.SqlDataReader.get_MetaData() +112 
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6281668 
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +6282737 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +424 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +28 
    System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +211 
    System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +19 
    System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +19 
    System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +221 
    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +573 
    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +166 
    ASP.grid_stoenv_aspx.LoadData() in d:\http\netdev\Grid_Stoenv.aspx:29 
    ASP.grid_stoenv_aspx.Page_Load(Object sender, EventArgs e) in d:\http\netdev\Grid_Stoenv.aspx:18 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42 
    System.Web.UI.Control.OnLoad(EventArgs e) +132 
    System.Web.UI.Control.LoadRecursive() +66 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428 




-------------------------------------------------------------------------------- 
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927 
+0

Il devrait être 'select *', SELCT est incorrect. – rickp

+1

lol, je ne peux pas croire que les gens utilisent encore classique ado.net (pour certaines déclarations de toute façon) – RPM1984

+0

@ RPM1984 - Pourquoi? Si c'est ce que les gens savent utiliser, cela fait parfaitement l'affaire. – Justin

Répondre

1

vous avez orthographié "Select" incorrectement.

Selct * from ...

devrait être

Select * from ...

+0

Sur une note de côté, vous devriez vraiment envisager de faire le saut à Linq à SQL ou Entity Framework. Inline SQL est très passé. Vous trouverez votre vitesse de développement multipliée par dix avec un ORM. –

2

changez cette ligne

private const string _strSql = "Selct * from stotickets where [Status] = 'Active'"; 

à

private const string _strSql = "Select * from stotickets where [Status] = 'Active'"; 
Questions connexes