red boy
Zainteresovan član
- Poruka
- 353
Radi mi samo select i delete...Radio sam svoj primer za projekat koji moram da nosim profesoru...
Ne izbacuje gresku,ali ne vrsi upis u bazu.Zatim sam probao sa primerom iz knjige.Isti slucaj.
Dacu bolje primer iz knjige
A gresku koju javlja
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.
// Ukazuje na red
row["id"] = Convert.ToInt32(txtID);
Pre nego sto sam dodao ToInt32 ,javljao je istu gresku sa dodatkom da ocekuje convert ToInt32,a kada sam uradio skoro ista prica...
Svoj primer bolje da ne stavljam.Baza mi se nalazi u Debug pored exe verzije....
Jel ima neko ideju.Mislim da je ovo osnova povezivanja sa bazom,sta bi bilo da radim nesto komplikovanije...
Ne izbacuje gresku,ali ne vrsi upis u bazu.Zatim sam probao sa primerom iz knjige.Isti slucaj.
Dacu bolje primer iz knjige
Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb; //for MS Access
namespace ebook2
{
public partial class Form1 : Form
{
private DataTable dataTable;
private OleDbConnection conn;
private OleDbDataAdapter dataAdapter;
private DataSet ds;
private int currRec = 0;
private int totalRec=0;
private bool insertSelected;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void FillControls()
{
txtID.Text=dataTable.Rows[currRec]["id"].ToString();
txtName.Text=dataTable.Rows[currRec]["name"].ToString();
txtLast.Text=dataTable.Rows[currRec]["lastname"].ToString();
txtJob.Text=dataTable.Rows[currRec]["job"].ToString();
}
private void btnLoadTable_Click(object sender, EventArgs e)
{
//MS Access
this.Cursor = Cursors.WaitCursor;
string targetConnection="provider=Microsoft.Jet.OLEDB.4.0;data source=baza_krofni.mdb";
conn=new OleDbConnection(targetConnection);
string commandString="SELECT * FROM person";
dataAdapter=new OleDbDataAdapter(commandString,conn);
ds=new DataSet();
dataAdapter.Fill(ds,"prog");
dataTable=ds.Tables["prog"];
currRec=0;
totalRec=dataTable.Rows.Count;
FillControls();
InitalizeCommands();
ToggleControls(true);
this.Cursor=Cursors.Default;
//btnNext.Enabled=true;
//btnPrevious.Enabled=true;
}
private void InitalizeCommands()
{
// preparing insert OleDB command
dataAdapter.InsertCommand = conn.CreateCommand();
dataAdapter.InsertCommand.CommandText = "INSERT INTO person (id,name,lastname,job)" +
"VALUES (@id,@name,@lastname,@job)";
AddParams(dataAdapter.InsertCommand, "id", "name", "lastname", "job");
// preparing Update OleDB command
dataAdapter.UpdateCommand = conn.CreateCommand();
dataAdapter.UpdateCommand.CommandText = "UPDATE person SET name=@name,lastname=@lastname,job=@job WHERE id=@id";
AddParams(dataAdapter.UpdateCommand, "id", "name", "lastname", "job");
// preparing delete OleDB command
dataAdapter.DeleteCommand = conn.CreateCommand();
dataAdapter.DeleteCommand.CommandText = "DELETE FROM person WHERE id=@id";
AddParams(dataAdapter.DeleteCommand, "id");
}
private void AddParams(OleDbCommand cmd, params string[] cols)
{
foreach (string col in cols)
{
cmd.Parameters.Add("@" + col, OleDbType.Char, 0, col);
}
}
private void ToggleControls(bool val)
{
txtID.ReadOnly = val;
txtName.ReadOnly = val;
txtLast.ReadOnly = val;
txtJob.ReadOnly = val;
btnLoadTable.Enabled = val;
btnNext.Enabled = val;
btnPrevious.Enabled = val;
btnDeleteRecord.Enabled = val;
btnEditRecord.Enabled = val;
btnInsertRecord.Enabled = val;
btnCancel.Enabled = !val;
btnSave.Enabled = !val;
}
private void btnNext_Click(object sender, EventArgs e)
{
currRec++;
if (currRec >= totalRec)
currRec = 0;
FillControls();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
currRec--;
if (currRec < 0)
currRec = totalRec - 1;
FillControls();
}
private void btnEditRecord_Click(object sender, EventArgs e)
{
ToggleControls(false);
}
private void btnSave_Click(object sender, EventArgs e)
{
lblLabel.Text = "Save changes...";
this.Cursor = Cursors.WaitCursor;
DataRow row = dataTable.Rows[currRec];
row.BeginEdit();
row["id"] = Convert.ToInt32(txtID);
row["name"] = txtName;
row["lastname"] = txtLast;
row["job"] = txtJob;
row.EndEdit();
dataAdapter.Update(ds, "prog");
ds.AcceptChanges();
ToggleControls(true);
insertSelected=false;
this.Cursor = Cursors.Default;
lblLabel.Text = "Save changed";
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (insertSelected)
{
btnDeleteRecord_Click(null, null);
insertSelected = false;
}
FillControls();
ToggleControls(true);
}
private void btnInsertRecord_Click(object sender, EventArgs e)
{
insertSelected = true;
DataRow row = dataTable.NewRow();
dataTable.Rows.Add(row);
totalRec = dataTable.Rows.Count;
currRec=totalRec-1;
row["id"]=totalRec;
txtID.Text = totalRec.ToString();
txtName.Text = "";
txtLast.Text = "";
txtJob.Text = "";
ToggleControls(false);
}
private void btnDeleteRecord_Click(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("Da li sigurno brisete \n", "Potvrdi\n", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
DataRow row=dataTable.Rows[currRec];
row.Delete();
dataAdapter.Update(ds, "prog");
ds.AcceptChanges();
lblLabel.Text = "Record Deleted";
totalRec--;
currRec = totalRec - 1;
FillControls();
}
}
}
}
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.
// Ukazuje na red
row["id"] = Convert.ToInt32(txtID);
Pre nego sto sam dodao ToInt32 ,javljao je istu gresku sa dodatkom da ocekuje convert ToInt32,a kada sam uradio skoro ista prica...
Svoj primer bolje da ne stavljam.Baza mi se nalazi u Debug pored exe verzije....
Jel ima neko ideju.Mislim da je ovo osnova povezivanja sa bazom,sta bi bilo da radim nesto komplikovanije...
Poslednja izmena: