inserir,atualizar,listar e excluir usando nwind em C#
19/12/2007
0
Erro de Servidor no Aplicativo ´/WebSite2´.
--------------------------------------------------------------------------------
Erro de sintaxe na instrução INSERT INTO.
Descrição: Ocorreu uma exceção não tratada durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código.
Detalhes da Exceção: System.Data.OleDb.OleDbException: Erro de sintaxe na instrução INSERT INTO.
Erro de Origem:
Linha 25:
Linha 26: OleDbCommand cmd = new OleDbCommand(cmd1, connection);
Linha 27: cmd.ExecuteNonQuery();
Linha 28:
Linha 29: connection.Close();
Arquivo de Origem: c:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Default.aspx.cs Linha: 27
Rastreamento de Pilha:
[OleDbException (0x80040e14): Erro de sintaxe na instrução INSERT INTO.]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +267
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +192
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +48
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +108
_Default.Button1_Click(Object sender, EventArgs e) in c:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Default.aspx.cs:27
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
--------------------------------------------------------------------------------
Informações sobre a Versão: Microsoft .NET Framework Versão:2.0.50727.312; Versão do ASP.NET:2.0.50727.833
CÓDIGO
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(@´Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Nwind.mdb´);
connection.Open();
String cmd1 = ´INSERT INTO Products ( ProdructID ,ProductName, UnitPrice) VALUES = (´ + Id + ´ ´´ + nome.Text + ´´,´ + preco.Text + ´)´;
OleDbCommand cmd = new OleDbCommand(cmd1, connection);
cmd.ExecuteNonQuery();
connection.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(@´Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Nwind.mdb´);
connection.Open();
String cmd1 = ´UPDATE Products SET ProductName= ´´ + nome.Text + ´´, UnitPrice=´ + preco.Text + ´ where ProductID = ´ + Id.Text;
OleDbCommand cmd = new OleDbCommand(cmd1, connection);
cmd.ExecuteNonQuery();
connection.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(@´Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Nwind.mdb´);
connection.Open();
String cmd1 = ´select * from Products´;
OleDbCommand cmd = new OleDbCommand(cmd1, connection);
cmd.ExecuteNonQuery();
connection.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(@´Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Nwind.mdb´);
connection.Open();
String cmd1 = ´DELETE Products SET ProductName= ´´ + nome.Text + ´´, UnitPrice=´ + preco.Text + ´ where ProductID = ´ + Id.Text;
OleDbCommand cmd = new OleDbCommand(cmd1, connection);
cmd.ExecuteNonQuery();
}
}
Por favor...se for possível..me ajudem o mais rápido possível...esse projeto é p entregar até hj a noite...tentei até hj a tarde fazer,mas n consegui
Valeu msm
Mariana
Marianaaleao
Posts
26/12/2007
Thiagokoelho
a classe com a função _GetConnection
protected void Button1_Click(object sender, EventArgs e)
{
classe _classe = new classe();
_classe._GetConnection(´INSERT INTO Products ( ProdructID ,ProductName, UnitPrice) VALUES = (´ + Id + ´ ´´ + nome.Text + ´´,´ + preco.Text + ´)´;
}
public void _GetConnection(string strQuery)
{
OleDbConnection _conn = new OleDbConnection();
_conn.ConnectionString = ´Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mariana\Documents\Visual Studio 2005\WebSites\WebSite2\Nwind.mdb´;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = _conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
_conn.Open();
cmd.ExecuteNonQuery();
_conn.Close();
_conn.Dispose();
}
02/01/2008
Rjun
02/01/2008
Rjun
string codSQL; codSQL = "INSERT INTO Products (ProdructID ,ProductName, UnitPrice) VALUES (, ´´, )"; codSQL = string.Format(codSQL, Id, nome.Text, preco.Text);
Clique aqui para fazer login e interagir na Comunidade :)