Existing posts keep on re-add upon deletion of selected row in jTable - sql

I try to refresh the data of jTable upon deletion of selected row. Here are my codes to set up table :
private JTable getJTableManageReplies() {
jTableManageReplies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTableManageReplies.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int viewRow = jTableManageReplies.getSelectedRow();
// Get the first column data of the selectedrow
int replyID = Integer.parseInt(jTableManageReplies.getValueAt(
viewRow, 0).toString());
eForumRepliesAdmin reply = new eForumRepliesAdmin(replyID);
replyID = JOptionPane.showConfirmDialog(null, "Are you sure that you want to delete the selected reply? " , "Delete replies", JOptionPane.YES_NO_OPTION);
if(replyID == JOptionPane.YES_OPTION){
reply.deleteReply();
JOptionPane.showMessageDialog(null, "Reply has been deleted successfully.");
SetUpJTableManageReplies();
}
}
}
});
return jTableManageReplies;
}
public void SetUpJTableManageReplies() {
DefaultTableModel tableModel = (DefaultTableModel) jTableManageReplies
.getModel();
String[] data = new String[5];
db.setUp("IT Innovation Project");
String sql = "Select forumReplies.reply_ID,forumReplies.reply_topic,forumTopics.topic_title,forumReplies.reply_content,forumReplies.reply_by from forumReplies,forumTopics WHERE forumReplies.reply_topic = forumTopics.topic_id ";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
jTableManageReplies.repaint();
tableModel.getDataVector().removeAllElements();
try {
while (resultSet.next()) {
data[0] = resultSet.getString("reply_ID");
data[1] = resultSet.getString("reply_topic");
data[2] = resultSet.getString("topic_title");
data[3] = resultSet.getString("reply_content");
data[4] = resultSet.getString("reply_by");
tableModel.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
And this is my sql statement :
public boolean deleteReply() {
boolean success = false;
DBController db = new DBController();
db.setUp("IT Innovation Project");
String sql = "DELETE FROM forumReplies where reply_ID = " + replyID
+ "";
if (db.updateRequest(sql) == 1)
success = true;
db.terminate();
return success;
}
I called the repaint() to update the table data with the newest data in database and it works. I mean the data after deletion of certain row. However, the existing posts will keep on re-add. Then I add the removeAllElement method to remove all the existing posts because my sql statement is select * from table. Then, there is an error message which is ArrayIndexOutOfBoundsException. Any guides to fix this? Thanks in advance.

I called the repaint() to update the table data with the newest data
in database and it works.
There is no need to call repaint method when data is changed. Data change is handled by the Table Model (DefaultTableModel in this case.) And fireXXXMethods are required to be called whenever data is changed but you are using DefaultTableModel even those are not required. (Since by default it call these methods when ever there is a change.)
I think the problem is in the valuesChanged(..) method. You are getting the value at row 0 but not checking whether table has rows or not. So keep a constraint.
int viewRow = jTableManageReplies.getSelectedRow();
// Get the first column data of the selectedrow
if(jTableManageReplies.getRowCount() > 0)
int replyID = Integer.parseInt(jTableManageReplies.getValueAt(viewRow, 0).toString());

Related

Google BigQuery returns only partial table data with C# application using .net Client Library

I am trying to execute the query (Basic select statement with 10 fields). My table contains more than 500k rows. C# application returns the response with only 4260 rows. However Web UI returns all the records.
Why my code returns only partial data, What is the best way to select all the records and load into C# Data Table? If there is any code snippet it would be more helpful to me.
using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using System.Data;
using Google.Apis.Services;
using System;
using System.Security.Cryptography.X509Certificates;
namespace GoogleBigQuery
{
public class Class1
{
private static void Main()
{
try
{
Console.WriteLine("Start Time: {0}", DateTime.Now.ToString());
String serviceAccountEmail = "SERVICE ACCOUNT EMAIL";
var certificate = new X509Certificate2(#"KeyFile.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.Bigquery, BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.CloudPlatform, BigqueryService.Scope.DevstorageFullControl }
}.FromCertificate(certificate));
BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "PROJECT NAME"
});
string query = "SELECT * FROM [publicdata:samples.shakespeare]";
JobsResource j = Service.Jobs;
QueryRequest qr = new QueryRequest();
string ProjectID = "PROJECT ID";
qr.Query = query;
qr.MaxResults = Int32.MaxValue;
qr.TimeoutMs = Int32.MaxValue;
DataTable DT = new DataTable();
int i = 0;
QueryResponse response = j.Query(qr, ProjectID).Execute();
string pageToken = null;
if (response.JobComplete == true)
{
if (response != null)
{
int colCount = response.Schema.Fields.Count;
if (DT == null)
DT = new DataTable();
if (DT.Columns.Count == 0)
{
foreach (var Column in response.Schema.Fields)
{
DT.Columns.Add(Column.Name);
}
}
pageToken = response.PageToken;
if (response.Rows != null)
{
foreach (TableRow row in response.Rows)
{
DataRow dr = DT.NewRow();
for (i = 0; i < colCount; i++)
{
dr[i] = row.F[i].V;
}
DT.Rows.Add(dr);
}
}
Console.WriteLine("No of Records are Readed: {0} # {1}", DT.Rows.Count.ToString(), DateTime.Now.ToString());
while (true)
{
int StartIndexForQuery = DT.Rows.Count;
Google.Apis.Bigquery.v2.JobsResource.GetQueryResultsRequest SubQR = Service.Jobs.GetQueryResults(response.JobReference.ProjectId, response.JobReference.JobId);
SubQR.StartIndex = (ulong)StartIndexForQuery;
//SubQR.MaxResults = Int32.MaxValue;
GetQueryResultsResponse QueryResultResponse = SubQR.Execute();
if (QueryResultResponse != null)
{
if (QueryResultResponse.Rows != null)
{
foreach (TableRow row in QueryResultResponse.Rows)
{
DataRow dr = DT.NewRow();
for (i = 0; i < colCount; i++)
{
dr[i] = row.F[i].V;
}
DT.Rows.Add(dr);
}
}
Console.WriteLine("No of Records are Readed: {0} # {1}", DT.Rows.Count.ToString(), DateTime.Now.ToString());
if (null == QueryResultResponse.PageToken)
{
break;
}
}
else
{
break;
}
}
}
else
{
Console.WriteLine("Response is null");
}
}
int TotalCount = 0;
if (DT != null && DT.Rows.Count > 0)
{
TotalCount = DT.Rows.Count;
}
else
{
TotalCount = 0;
}
Console.WriteLine("End Time: {0}", DateTime.Now.ToString());
Console.WriteLine("No. of records readed from google bigquery service: " + TotalCount.ToString());
}
catch (Exception e)
{
Console.WriteLine("Error Occurred: " + e.Message);
}
Console.ReadLine();
}
}
}
In this Sample Query get the results from public data set, In table contains 164656 rows but response returns 85000 rows only for the first time, then query again to get the second set of results. (But not known this is the only solution to get all the results).
In this sample contains only 4 fields, even-though it does not return all rows, in my case table contains more than 15 fields, I get response of ~4000 rows out of ~10k rows, I need to query again and again to get the remaining results for selecting 1000 rows takes time up to 2 minutes in my methodology so I am expecting best way to select all the records within single response.
Answer from User #:Pentium10
There is no way to run a query and select a large response in a single shot. You can either paginate the results, or if you can create a job to export to files, then use the files generated in your app. Exporting is free.
Step to run a large query and export results to files stored on GCS:
1) Set allowLargeResults to true in your job configuration. You must also specify a destination table with the allowLargeResults flag.
Example:
"configuration":
{
"query":
{
"allowLargeResults": true,
"query": "select uid from [project:dataset.table]"
"destinationTable": [project:dataset.table]
}
}
2) Now your data is in a destination table you set. You need to create a new job, and set the export property to be able to export the table to file(s). Exporting is free, but you need to have Google Cloud Storage activated to put the resulting files there.
3) In the end you download your large files from GCS.
It my turn to design the solution for better results.
Hoping this might help someone. One could retrieve next set of paginated result using PageToken. Here is the sample code for how to use PageToken. Although, I liked the idea of exporting for free. Here, I write rows to flat file but you could add them to your DataTable. Obviously, it is a bad idea to keep large DataTable in memory though.
public void ExecuteSQL(BigqueryService bqservice, String ProjectID)
{
string sSql = "SELECT r.Dealname, r.poolnumber, r.loanid FROM [MBS_Dataset.tblRemitData] R left join each [MBS_Dataset.tblOrigData] o on R.Dealname = o.Dealname and R.Poolnumber = o.Poolnumber and R.LoanID = o.LoanID Order by o.Dealname, o.poolnumber, o.loanid limit 100000";
QueryRequest _r = new QueryRequest();
_r.Query = sSql;
QueryResponse _qr = bqservice.Jobs.Query(_r, ProjectID).Execute();
string pageToken = null;
if (_qr.JobComplete != true)
{
//job not finished yet! expecting more data
while (true)
{
var resultReq = bqservice.Jobs.GetQueryResults(_qr.JobReference.ProjectId, _qr.JobReference.JobId);
resultReq.PageToken = pageToken;
var result = resultReq.Execute();
if (result.JobComplete == true)
{
WriteRows(result.Rows, result.Schema.Fields);
pageToken = result.PageToken;
if (pageToken == null)
break;
}
}
}
else
{
List<string> _fieldNames = _qr.Schema.Fields.ToList().Select(x => x.Name).ToList();
WriteRows(_qr.Rows, _qr.Schema.Fields);
}
}
The Web UI automatically flattens the data. This means that you see multiple rows for each nested field.
When you run the same query via the API, it won't be flattened, and you get fewer rows, as the nested fields are returned as objects. You should check if this is the case at you.
The other is that indeed you need to paginate through the results. Paging through list results has this explained.
If you want to do only one job, than you should write your query ouput to a table, than export the table as JSON, and download the export from GCS.

Deleting the last row in a table. SQL/Netbeans

In java, i am trying to delete the last row of my database. The database has 15 rows and i want to the delete the 15th one. The columns are called Initials and Score.
Intials Scores
rows# 1. ADS 2343
2. DDE 5454
15. TBK 332
I can't have it selecting TBK because i'm wanting it to delete the 15th one no matter what it is so a new one can be added. Everywhere I've looked it's always has to be specific or a delete all rows. Can anyone help? Many thanks to those who help. :)
Assuming id is an identity column
DELETE FROM table
WHERE id = (SELECT MAX(id) FROM table)
OP : I am trying to delete the last row of my database.
make resultset updatable : ResultSet.CONCUR_UPDATABLE);
set cursor to last record : resultSet.last();
delete last Record : resultSet.deleteRow();
for further use of rs you should set : resultSet.beforeFirst();
private static int delLastRow(ResultSet resultSet) {
if (resultSet == null) {
return 0;
}
try {
resultSet.last();
int delID = resultSet.getInt(1);
resultSet.deleteRow();
System.out.println("Deleted id :" + delID);
resultSet.beforeFirst();
return delID;
} catch (SQLException exp) {
exp.printStackTrace();
} finally {
try {
resultSet.beforeFirst();
} catch (SQLException exp) {
exp.printStackTrace();
}
}
return 0;
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//rs will be scrollable, will not show changes made by others,
//and will be updatable
String sql;
sql = "SELECT * FROM `order details`";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Deleted id :"+ delLastRow(rs));
....
}

Oracle columns update not happpening

I am trying to update a few columns in a Oracle table from my C# code.
Here is my method:
private static bool UpdateOracleTable(OracleTable table, string whereClause, List<int> entIDs)
{
try
{
var tableName = table.ToString();
using (OracleConnection conn = new OracleConnection(_oracleConnection))
{
conn.Open();
foreach (var id in entIDs)
{
whereClause = String.Format(whereClause, id);
var query = Resources.UpdateOracle;
query = String.Format(query, tableName, "20", DateTime.Now.ToString("yyyy/MM/dd"), whereClause);
using (OracleCommand cmd = new OracleCommand(query, conn))
{
cmd.ExecuteNonQuery();
}
}
}
return true;
}
catch (Exception ex)
{
Log.Debug(LogType.Error, ex);
return false;
}
}
Here is the Query:
UPDATE
{0}
SET
SYNC_STATUS = '{1}'
,SYNC_DATE = TO_DATE('{2}', 'yyyy/mm/dd')
{3}
And the where clause will look something like:
WHERE ID = {0}
This method updates about 10 records, and the rest stays null. This mehod does return true, and I have debugged, no exception is thrown.
Why does it not update all records?
This isn't an answer but might help debug the problem.
Instead of the like:
cmd.ExecuteNonQuery();
put in this:
int count = cmd.ExecuteNonQuery();
if (count == 0)
{
Console.WriteLine("");
}
Put a break on the Console.WriteLine("") and run it. The debugger will stop if no rows were updated. You can then check the query, and whether or not that ID actually exists.
The problem was with the WHERE clause. Since it contains a place holder {0}, after I I formatted the WHERE clause, the ID always stayed to the value it was formatted with first.
This is what my new method looks like.
private static bool UpdateOracleTable(OracleTable table, string whereClause, List<int> entIDs)
{
try
{
var tableName = table.ToString();
using (OracleConnection conn = new OracleConnection(_oracleConnection))
{
conn.Open();
foreach (var id in entIDs)
{
string originalWhere = whereClause;
originalWhere = String.Format(originalWhere, id);
var query = Resources.UpdateOracle;
query = String.Format(query, tableName, "20", DateTime.Now.ToString("yyyy/MM/dd"), originalWhere);
using (OracleCommand cmd = new OracleCommand(query, conn))
{
bool success = cmd.ExecuteNonQuery() > 0;
}
}
}
return true;
}
catch (Exception ex)
{
Log.Debug(LogType.Error, ex);
return false;
}
}
As can be seen, I added a variable 'originalWhere', that gets formatted, but most importantly, is being set to original WHERE clause parameter passed, so that it will always contain the place holder.

Topic views do not show up in jTable

I try to code a forum using java swing. Firstly, on click for the jTable, it will lead to eForumContent.class which pass in the id together.
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int id = 0;
eForumTopics topics = new eForumTopics(id);
topics.retrieveDiscussion();
eForumThreadContent myWindow = new eForumThreadContent(id);
myWindow.getJFrame().setVisible(true);
}
});
I initialize the id first. But I set my id in database table to auto number. Then I call the retrieveDiscussion method to get the id from database and pass it to eForumThreadContent. Here are my codes for retrieveDiscussion method.
public boolean retrieveDiscussion(){
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
db.setUp("IT Innovation Project");
String dbQuery = "SELECT * FROM forumTopics WHERE topic_id = '" + id
+ "'";
rs = db.readRequest(dbQuery);
db.terminate();
return success;
}
}
Then, inside the eForumThreadContent, I want to display the content of chosen thread using a table. So I use the id which I pass in just now and insert into my sql statement. Here are my codes.
public eForumThreadContent(int id) {
this.id = id;
}
if (jTable == null) {
Vector columnNames = new Vector(); // Vector class allows dynamic
// array of objects
Vector data = new Vector();
try {
DBController db = new DBController();
db.setUp("IT Innovation Project");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String dsn = "IT Innovation Project";
String s = "jdbc:odbc:" + dsn;
Connection con = DriverManager.getConnection(s, "", "");
String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = '"+id+"'";
java.sql.Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
resultSet.close();
((Connection) statement).close();
} catch (Exception e) {
System.out.println(e);
}
jTable = new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i < jTable.getColumnCount(); i++) {
column = jTable.getColumnModel().getColumn(i);
if (i == 1) {
column.setPreferredWidth(400); // second column is bigger
}else {
column.setPreferredWidth(200);
}
}
String header[] = { "Title", "Description", "Posted by" };
for (int i = 0; i < jTable.getColumnCount(); i++) {
TableColumn column1 = jTable.getTableHeader().getColumnModel()
.getColumn(i);
column1.setHeaderValue(header[i]);
}
jTable.getTableHeader().setFont( new Font( "Dialog" , Font.PLAIN, 20 ));
jTable.getTableHeader().setForeground(Color.white);
jTable.getTableHeader().setBackground(new Color(102, 102, 102));
jTable.setEnabled(false);
jTable.setRowHeight(100);
jTable.getRowHeight();
jTable.setFont( new Font( "Dialog" , Font.PLAIN, 18 ));
}
return jTable;
}
However, the table inside eForumThreadContent is empty even when I clicked on certain thread. It also gives me an error message.
[Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at DBController.database.DBController.readRequest(DBController.java:27)
at kioskeForum.entity.eForumTopics.retrieveDiscussion(eForumTopics.java:67)
at kioskeForum.ui.eForumDiscussion$3.mouseClicked(eForumDiscussion.java:296)
I research online to get an idea for topic views using id. But my table does not show up anything. Can somebody enlighten me how to fix it? Or any other ways to display topic views in java swing? Thanks in advance.

How do I get rid of the concurrency issue with update statements with an auto-incrementing key?

I have run into the following error message at run-time with my novice's Sql / Visual C# database program:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
I have done some searching around about this issue and have seen several threads about it, but they aren't enough for me to resolve the issue. I noticed people talking about how it can often occur when you're using an auto-incrementing primary key. That is the case in this program. Also there is no multithreading or anything else like that that I am coding into this program. Therefore I think the auto-increment is possibly the only problem.
That being said, how do I get the update command to work, despite the auto-increment? I can hardly stand on two feet when it comes to database programming, so please be detailed, if you don't mind. Also the command I'm using is via a SqlCommandBuilder object. I have set that object to new SqlCommandBuilder(DataAdapter), and I have not done anything special with it.
Thanks.
This edit is the second one. The first one is below.
Due to my inexperience with database programming, I am unable to say this for sure. However I have good reason to believe that the problem I am experiencing has to do with new rows not getting added to the database completely until the program terminates. I do not understand why they are waiting until program termination to do that, or if they are waiting until then, just what exactly what about the program's termination causes them to suddenly get saved completely. However I have forgotten to mention that this error only occurs on rows that I have added during that specific execution of the program. If the row was already added on a previous execution or through pre-existing table data, everything's fine. I am getting the same error with the delete method, and it also only occurs with new rows.
How do I get these rows to be fully saved to everything so that this doesn't happen? What about the program's termination is causing these rows to get fully saved? Thanks!
Due to a request, I have left here two code snippets. The first one will be the method in which the the problem occurs. The next one will include the entire class. There are only two classes in the entire program, and the other class doesn't seem important to me in this particular issue.
private void btnUpdate_Click(object sender, EventArgs e)
{
if (recordShown)
{
con.Open();
currentRow[1] = tbFirstName.Text;
currentRow[2] = tbMiddleName.Text;
currentRow[3] = tbLastName.Text;
currentRow[4] = tbSuffix.Text;
currentRow[5] = tbHomePhone.Text;
currentRow[6] = tbCellPhone.Text;
currentRow[7] = tbOtherPhone.Text;
currentRow[8] = tbStreetAddress.Text;
currentRow[9] = tbCityAndState.Text;
currentRow[10] = tbCountry.Text;
currentRow[11] = tbEmail.Text;
dAdapter.Update(dataset, "Contacts");
con.Close();
}
else
{
MessageBox.Show("Please locate/add a record first.");
}
}
Next snippet:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Dakota
{
public partial class Form1 : Form
{
SqlConnection con;
DataSet dataset;
SqlDataAdapter dAdapter;
DataRow currentRow;
string primaryKey;
SqlCommandBuilder cmdBuilder;
bool recordShown = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataset = new DataSet();
con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;" +
"AttachDbFilename=C:\\Users\\Sterling\\Documents\\Contacts.mdf;" +
"Integrated Security=True;Connect Timeout=30;User Instance=True";
con.Open();
string getData = "SELECT * FROM tblContacts";
dAdapter = new SqlDataAdapter(getData, con);
dAdapter.Fill(dataset, "Contacts");
cmdBuilder = new SqlCommandBuilder(dAdapter);
cmdBuilder.ConflictOption = ConflictOption.OverwriteChanges;
con.Close();
}
private void clearTextBoxes()
{
tbFirstName.Clear();
tbMiddleName.Clear();
tbLastName.Clear();
tbSuffix.Clear();
tbHomePhone.Clear();
tbCellPhone.Clear();
tbOtherPhone.Clear();
tbStreetAddress.Clear();
tbCityAndState.Clear();
tbCountry.Clear();
tbEmail.Clear();
}
private void fillTextBoxes(int row)
{
DataRow dr = dataset.Tables["Contacts"].Rows[row];
tbFirstName.Text = dr.ItemArray.GetValue(1).ToString();
tbMiddleName.Text = dr.ItemArray.GetValue(2).ToString();
tbLastName.Text = dr.ItemArray.GetValue(3).ToString();
tbSuffix.Text = dr.ItemArray.GetValue(4).ToString();
tbHomePhone.Text = dr.ItemArray.GetValue(5).ToString();
tbCellPhone.Text = dr.ItemArray.GetValue(6).ToString();
tbOtherPhone.Text = dr.ItemArray.GetValue(7).ToString();
tbStreetAddress.Text = dr.ItemArray.GetValue(8).ToString();
tbCityAndState.Text = dr.ItemArray.GetValue(9).ToString();
tbCountry.Text = dr.ItemArray.GetValue(10).ToString();
tbEmail.Text = dr.ItemArray.GetValue(11).ToString();
}
private void fillTextBoxes(DataRow dr)
{
tbFirstName.Text = dr.ItemArray.GetValue(1).ToString();
tbMiddleName.Text = dr.ItemArray.GetValue(2).ToString();
tbLastName.Text = dr.ItemArray.GetValue(3).ToString();
tbSuffix.Text = dr.ItemArray.GetValue(4).ToString();
tbHomePhone.Text = dr.ItemArray.GetValue(5).ToString();
tbCellPhone.Text = dr.ItemArray.GetValue(6).ToString();
tbOtherPhone.Text = dr.ItemArray.GetValue(7).ToString();
tbStreetAddress.Text = dr.ItemArray.GetValue(8).ToString();
tbCityAndState.Text = dr.ItemArray.GetValue(9).ToString();
tbCountry.Text = dr.ItemArray.GetValue(10).ToString();
tbEmail.Text = dr.ItemArray.GetValue(11).ToString();
}
private void btnSearch_Click(object sender, EventArgs e)
{
string searchFor = tbSearchFor.Text;
string column;
if (rbFirstName.Checked)
{
column = "firstName";
}
else
{
column = "lastName";
}
DataRow[] rows = dataset.Tables["Contacts"].Select(column + "='" + searchFor + "'");
int number = rows.Length;
if (number == 0)
{
MessageBox.Show("No such records were found.");
}
else if (number > 1)
{
string[] strings = new string[rows.Length];
for (int i = 0; i < strings.Length; i++)
{
bool hasFirst = false;
bool hasMiddle = false;
strings[i] = "";
if (rows[i].ItemArray.GetValue(1).ToString() != "")
{
hasFirst = true;
strings[i] += rows[i].ItemArray.GetValue(1).ToString();
}
if (rows[i].ItemArray.GetValue(2).ToString() != "")
{
hasMiddle = true;
if (hasFirst)
{
strings[i] += " ";
}
strings[i] += rows[i].ItemArray.GetValue(2).ToString();
}
if (rows[i].ItemArray.GetValue(3).ToString() != "")
{
if ((hasFirst && !hasMiddle) || (hasMiddle))
{
strings[i] += " ";
}
strings[i] += rows[i].ItemArray.GetValue(3).ToString();
}
if (rows[i].ItemArray.GetValue(4).ToString() != "")
{
strings[i] += " " + rows[i].ItemArray.GetValue(4).ToString();
}
}
// int choice;
Form2 form2 = new Form2(strings);
if (form2.ShowDialog(this) == DialogResult.OK)
{
primaryKey = rows[form2.choice].ItemArray.GetValue(0).ToString();
// choice = form2.choice;
fillTextBoxes(rows[form2.choice]);
currentRow = rows[form2.choice];
recordShown = true;
}
}
else
{
primaryKey = rows[0].ItemArray.GetValue(0).ToString();
currentRow = rows[0];
fillTextBoxes(rows[0]);
recordShown = true;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
con.Open();
DataRow row = dataset.Tables["Contacts"].NewRow();
row[1] = tbFirstName.Text;
row[2] = tbMiddleName.Text;
row[3] = tbLastName.Text;
row[4] = tbSuffix.Text;
row[5] = tbHomePhone.Text;
row[6] = tbCellPhone.Text;
row[7] = tbOtherPhone.Text;
row[8] = tbStreetAddress.Text;
row[9] = tbCityAndState.Text;
row[10] = tbCountry.Text;
row[11] = tbEmail.Text;
currentRow = row;
dataset.Tables["Contacts"].Rows.Add(row);
dAdapter.Update(dataset, "Contacts");
recordShown = true;
con.Close();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (recordShown)
{
con.Open();
currentRow[1] = tbFirstName.Text;
currentRow[2] = tbMiddleName.Text;
currentRow[3] = tbLastName.Text;
currentRow[4] = tbSuffix.Text;
currentRow[5] = tbHomePhone.Text;
currentRow[6] = tbCellPhone.Text;
currentRow[7] = tbOtherPhone.Text;
currentRow[8] = tbStreetAddress.Text;
currentRow[9] = tbCityAndState.Text;
currentRow[10] = tbCountry.Text;
currentRow[11] = tbEmail.Text;
dAdapter.Update(dataset, "Contacts");
con.Close();
}
else
{
MessageBox.Show("Please locate/add a record first.");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
con.Open();
currentRow.Delete();
dAdapter.Update(dataset, "Contacts");
clearTextBoxes();
recordShown = false;
con.Close();
}
}
}
Thanks!
Here is one explanation but I am not sure if it is exactly what you are seeing:
http://blogs.msdn.com/b/spike/archive/2010/04/07/concurrency-violation-the-updatecommand-affected-0-of-the-expected-1-records.aspx
This might be more on track and if so points to some missing lines that should come after you declare cmdBuilder:
dAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
dAdapter.InsertCommand = cmdBuilder.GetInsertCommand();
dAdapter.DeleteCommand = cmdBuilder.GetDeleteCommand();
http://www.codeguru.com/forum/archive/index.php/t-337168.html
Also, you might need to call:
dAdapter.Fill(dataset, "Contacts");
before the con.Close() for all three operations (Insert, Update, and Delete).
On an unrelated note, you could reduce duplicate code by changing the "fillTextBoxes(int row)" method to be just:
private void fillTextBoxes(int row)
{
DataRow dr = dataset.Tables["Contacts"].Rows[row];
fillTextBoxes(dr);
}
A couple things, it looks like you are not passing it back the ID of your identity column when calling update. Wouldn't it need to know the ID when doing an update?
In addition to the comment that srutzky made about the redundant code in fillTextBoxes, you might also consider not referencing your columns by ordinal value and instead reference them by their actual column name. If you were to add a column to your DB, it would break all of your code that is doing things like:
tbLastName.Text = dr.ItemArray.GetValue(3).ToString();
Instead, you might do something like:
tbLastName.Text = dr.ItemArray.GetValue("LastName").ToString();
I don't know offhand if GetValue takes the column name as a parameter, but I'm sure it is something like that.