blah blah blah is here! blah blah » Close

up0down
link

I can not update database using datagridview Can any one suggest me what to do .

last answered 2 years ago

1 answers

up0down
link

Umm,
I had a similar problem too:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
/*
* Just Loading in the PMIETRXTEST.mdb data base getting the UID's from from the data grid
* and search through rtbData to see if txtBarCode is in there. 27/03/09 11:20
* remember to trim the split up Database values 27/03/09 12:28
*
*/
namespace PlainDB
{
public partial class Form1 : Form
{
// SerialPort BarCodePort = new SerialPort();
char[] charBuffer = new char[8];
public Form1()
{
InitializeComponent();
}
private void btnOpenDataBase_Click(object sender, EventArgs e)
{
int iColumnIndex = 0;
string sColumns = null;
dataGridView1.Visible = true;

foreach(DataGridViewRow oRow in dataGridView1.Rows)
{
sColumns += oRow.Cells[iColumnIndex].Value + " \n";
}
rtbData.Text = sColumns;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'pMIETRXTESTDataSet.ETRX_INTL_TEST' table. You can move, or remove it, as needed.
this.eTRX_INTL_TESTTableAdapter.Fill(this.pMIETRXTESTDataSet.ETRX_INTL_TEST);
}
private void btnSearch_Click(object sender, EventArgs e)
{
char[] delimiterChars = { '\n', '=' };
int a;
string BarCodeGot, text;
bool FoundIt = false;
text = rtbData.Text;
BarCodeGot = txtBarCode.Text;
MessageBox.Show(BarCodeGot);
string[] words = text.Split(delimiterChars);
MessageBox.Show(" words in text:" + words.Length);
a = 0;
foreach (string s in words)
{
a++;
}
for (a = 0; a < words.Length; a++)
{
// MessageBox.Show(BarCodeGot + "\n" + words[a]);
if (words[a].Trim() == BarCodeGot)
{

MessageBox.Show("Got IT!");
FoundIt = true;
}
}
if (FoundIt != true)
{
MessageBox.Show("No!");
}
}
private void btnOpenBarCode_Click(object sender, EventArgs e)
{
if (btnOpenBarCode.Text == "Bar Code Scanner Open")
{
//BarCodePort.Open();
btnOpenBarCode.Text = "Bar Code Scanner Closed";
}else if (btnOpenBarCode.Text == "Bar Code Scanner Closed")
{
// BarCodePort.Close();
btnOpenBarCode.Text = "Bar Code Scanner Open";
}
}
//private void tmrPollForRecievedData_Tick(object sender, EventArgs e)
//{
// string charToRead;
// while (BarCodePort.BytesToRead > 0)
// {
// charToRead = (BarCodePort.ReadExisting());
// rtbIncoming.Text = rtbIncoming.Text + (charToRead);//+ System.Convert.ToString(charToRead);
// }
// tmrPollForRecievedData.Enabled = false;
//}
//private void tmrAutoRead_Tick(object sender, EventArgs e)
//{
// rtbBarCodeLog.Text = rtbBarCodeLog.Text + textBox1.Text;
// textBox1.Text = "";
// Read_In_Data();
//}
}
}

The code above was for bar code scanner demo, "BEEP" read the bar code compared it to an entery in the data base. My memory of this was the updates were fed to the data set that was created by the "using System.Data;" & "using System.Data.Sql;" The code below might be of some use also a Q&A between Vulpes & I ca
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Data.OleDb;
using System.Xml.Serialization;
namespace SQL_Demo
{
/*
*Demo App opens the PerformanceDataBase as a connection
*12:25 19/01/09
*recalling serial numbers & result
* change "data source=E:\SQL Demo\SQL_Demo\SQL_Demo\PerformanceDataBase.mdb"); to wherever data base is
* 17:08 20/01/09 recalling data from DB
* How do you update an Access Database ?????????
*
Dim cn as new Oledb.OledbConnection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Northwind.accdb";
Dim cmd as new Oledb.OledbCommand(cn)
cmd.CommandText = "INSERT INTO Customers (FirstName, LastName) VALUES (@FirstName, @LastName)"
*/
public partial class Form1 : Form
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=F:\PerformanceDataBase.mdb");
private DataSet ds;
public string getTable;
DateTime current = DateTime.Now;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
conn.Close();
MessageBox.Show("Goodbye !!, Data Base Closed!");
Application.Exit();
}
catch (OleDbException ex)
{
MessageBox.Show("Problem!!!!!!!!! \n"+ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
conn.Open();
MessageBox.Show("Hello World!!, Data Base Open");
}
catch (OleDbException ex)
{
MessageBox.Show("Problem!!! \n"+ex.ToString());
}
}
private void button3_Click(object sender, EventArgs e)
{
OleDbDataAdapter adaptor = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT * FROM PerformanceDataBase", conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adaptor);
ds = new DataSet();

adaptor.SelectCommand = command;
adaptor.Fill(ds, "PerformanceDataBase");
DataTable dt = ds.Tables[0];

adaptor.SelectCommand = command;
richTextBox1.Text = "Serial Number: Result: Date:\n";
foreach (DataRow dr in dt.Rows)
{
richTextBox1.Text = richTextBox1.Text + dr["Serial Number"].ToString() + " " + dr["Test Result"].ToString() +" "+ dr["Date"].ToString();// +dr["Time"].ToString();
richTextBox1.Text = richTextBox1.Text +"\n";

}
MessageBox.Show("it Worked");
}
private void button4_Click(object sender, EventArgs e)
{
// SqlDataAdapter da;
//data base opened
OleDbDataAdapter adaptor = new OleDbDataAdapter();
//OleDbCommand command = new OleDbCommand("INSERT * INTO PerformanceDataBase", conn);
OleDbCommand command = new OleDbCommand("INSERT INTO PerformanceDataBase([Serial Number][Date][Time][Test Result][Real Time Clock][Three Meg Clock][N value][K value])VALUES(9781931448065,20/01/09,16:51,256.0069,3250243,35,228381),conn");
OleDbCommandBuilder cb = new OleDbCommandBuilder(adaptor);
// ds = new DataSet();
adaptor.SelectCommand = command;

/* Oledb.OledbCommand(cn);
cmd.CommandText = "INSERT INTO Customers (FirstName, LastName) VALUES (@FirstName, @LastName)"
*/
MessageBox.Show("it Worked!!!!");
}
private void button5_Click(object sender, EventArgs e)
{

OleDbConnection cn = new OleDbConnection();
//cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.4.0;Data Source=F:\\PerformanceDataBase";
cn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=F:\\PerformanceDataBase.mdb";
// OleDbCommand cmd =
OleDbCommand cmd = new OleDbCommand();
cn.Open();
cmd.Connection = cn;
// cmd.CommandText = "INSERT INTO Customers (FirstName, LastName) VALUES (@FirstName, @LastName)";
cmd.CommandText = "INSERT INTO PerformanceDataBase ([Serial Number],[Date],[Time],[Test Result],[Real Time Clock],[Three Meg Clock],[N value],[K value])VALUES(@param1,@param2,@param3,@param4,@param5,@param6,@param7,@param8)";
cmd.Parameters.Add("@param1", OleDbType.VarChar, 20).Value = "9";//textBox1.Text;
cmd.Parameters.Add("@param2", OleDbType.VarChar, 20).Value = "9";//textBox2.Text;
cmd.Parameters.Add("@param3", OleDbType.VarChar, 20).Value = "9";//textBox3.Text;
cmd.Parameters.Add("@param4", OleDbType.VarChar, 20).Value = "9";//textBox4.Text; ;
cmd.Parameters.Add("@param5", OleDbType.VarChar, 20).Value = "9";//textBox5.Text; ;
cmd.Parameters.Add("@param6", OleDbType.VarChar, 20).Value = "9";//textBox6.Text; ;
cmd.Parameters.Add("@param7", OleDbType.VarChar, 20).Value = "9";//textBox7.Text; ;
cmd.Parameters.Add("@param8", OleDbType.VarChar, 20).Value = "9";//textBox8.Text; ;
cmd.ExecuteNonQuery();
cn.Close();

/*
SqlConnection cn = new SqlConnection();
OleDbDataAdapter adaptor = new OleDbDataAdapter();
SqlDataAdapter da;
txtTestResult.Text = "Pass";
txtTime.Text = current.ToString().Substring(11,8);
txtDate.Text = current.ToString().Substring(0, 11);
MessageBox.Show("adaptor = " + adaptor);
cn.ConnectionString = "Server=server;Database=PerfomanceDataBase;";

da = new SqlDataAdapter("SELECT * FROM PerformanceDataBase", cn);
cn.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO [PerformanceDataBase]([Serial Number],[Date],[Time],[Test Result],[Real Time Clock],[Three Meg Clock],[N value],[K value])VALUES(@p1,@p2,@p3,@p4,@p5,@p6,@p7)");
OleDbParameter p1 = new OleDbParameter("@p1", "9");
OleDbParameter p2 = new OleDbParameter("@p2", "9");
OleDbParameter p3 = new OleDbParameter("@p3", "9");
OleDbParameter p4 = new OleDbParameter("@p4", "9");
OleDbParameter p5 = new OleDbParameter("@p5", "9");
OleDbParameter p6 = new OleDbParameter("@p6", "9");
OleDbParameter p7 = new OleDbParameter("@p7", "9");
OleDbParameter p8 = new OleDbParameter("@p8", "9");

command.Parameters.Add(p1);
command.Parameters.Add(p2);
command.Parameters.Add(p3);
command.Parameters.Add(p4);
command.Parameters.Add(p5);
command.Parameters.Add(p6);
command.Parameters.Add(p7);
command.Parameters.Add(p8);

// command.Connection.Open();
command.ExecuteNonQuery();
// command.Connection.Close();

cn.Close();
*/
}

private void button6_Click(object sender, EventArgs e)
{
//dataGridView1.;
}


}
}
The orignial post was:
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=68942

Also I should say (type whatever!) I endup not using the Datagrid for altering just viewing......
Sorry Glenn

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback