Cześć wszystkim.
Mam problem z kodem, który mi się nie kompiluje z niewiadomego dla mnie powodu...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace WebApplication5
{
public partial class _Default : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DeptInfo();
}
}
public void DeptInfo()
{
// calling up your connection string that was configured in you Web Config File
using (OleDbConnection conn = new OleDbConnection(GetConnectionString()))
{
DataTable dt = new DataTable();
//conn.Open();
String sql = "SELECT * FROM projects";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string Cat;
Cat = dt.Rows[i]["nazwa_projektu"].ToString();
DropDownList1.Items.Add(Cat);
}
}
cmd.Connection.Close();
}
}
protected void Remove_Click(object sender, EventArgs e)
{
ExecuteDelete(DropDownList1.SelectedItem.Text);
}
private void ExecuteDelete(string nazwa_projektu)
{
OleDbConnection conn = new OleDbConnection(GetConnectionString());
string sql = "DELETE FROM projects WHERE nazwa_projektu=@nazwa_projektu)";
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbParameter[] param = new OleDbParameter[1];
param[0] = new OleDbParameter("@nazwa_projektu", OleDbType.VarChar, 50);
param[0].Value = nazwa_projektu;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Data Cannot Be Deleted : ";
Label1.Text = msg + ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
Label1.ForeColor = System.Drawing.Color.LimeGreen;
Label1.Text = "Record Was Successfully Deleted!";
}
DeptInfo();
}
}
}
Po wybraniu pozycji z dropdownlist i kliknieciu na Remove_click - kompilator wskazuje na cmd.ExecuteNonQuery(); w private void ExecuteDelete(string nazwa_projektu) pisząc:
System.Data.OleDb.OleDbException: „Zbędny ) w wyrażeniu kwerendy 'nazwa_projektu=@nazwa_projektu)'.”
Po usunięciu linii cmd.ExecuteNonQuery();...pozycje w dropdownlist się klonują (plik bd zostaje niezmieniony) mimo, że w założeniu wybranie pozycji z listy i zatwierdzenie przyciskiem powinno usunąć odpowiednie rekordy z bazy access