Dynamically Create multiple combobox Codebehind & bind the value through wcf in silverlight - wcf

This is the code i have written to dynamically generate Controls:
private void GenCtrl(string type, int typeid,string sql)
{
string id = Convert.ToString(typeid);
if (type == "TextBox")
{// created for TextBox
}
else if (type == "ComboBox")
{
ComboBoxEdit cb = new ComboBoxEdit();
cb.Name = "cb" + id;
// this is to set unique id if more than 1 combobox created.
cb.Height = 20;
cb.Width = 100;
cb.Margin = new Thickness(2, 2, 0, 0);
cb.HorizontalAlignment = HorizontalAlignment.Left;
cb.Text = "Show All";
stctrl.Children.Add(cb);
ComboBoxEdit cbx = (ComboBoxEdit)cb.FindName(cb.Name);
cb.DisplayMember = "Name";
if(filsql !=null)
ServRef.GetComboBoxlistAsync(sql); //this would retrieve the list and attach to the combobox.
//ctlst.Add(cb.Name, type);
}
}
//WCF Service reference
ServRef.GetComboBoxlistCompleted += new EventHandler<GetComboBoxlistCompletedEventArgs>(ServRef_GetComboBoxlistCompleted);
void ServRef_GetComboBoxlistCompleted(object sender, GetComboBoxlistCompletedEventArgs e)
{
cb.ItemsSource = e.Result;
cb.SelectedIndex = 0;
}
The problem i am facing while binding two combobox is created ex : cb1,cb2( dynamically created)only the control cb2 combox which is the latest get activated at ServRef_GetComboBoxlistCompleted method. i am unable to bind the value for the first control(combobox).
if my question is not clear let me know.

i was able to find using FindName
ComboBoxEdit cbx = (ComboBoxEdit)stackctrl.FindName(cb.Name);

Related

How to change color of a part of text from < to > [duplicate]

I'm trying to color parts of a string to be appended to a RichTextBox. I have a string built from different strings.
string temp = "[" + DateTime.Now.ToShortTimeString() + "] " +
userid + " " + message + Environment.NewLine;
This is what the message would look like once it is constructed.
[9:23pm] User: my message here.
I want everything within and including the brackets [9:23] to be one color, 'user' to be another color and the message to be another color. Then I'd like the string appended to my RichTextBox.
How can I accomplish this?
Here is an extension method that overloads the AppendText method with a color parameter:
public static class RichTextBoxExtensions
{
public static void AppendText(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
}
And this is how you would use it:
var userid = "USER0001";
var message = "Access denied";
var box = new RichTextBox
{
Dock = DockStyle.Fill,
Font = new Font("Courier New", 10)
};
box.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Red);
box.AppendText(" ");
box.AppendText(userid, Color.Green);
box.AppendText(": ");
box.AppendText(message, Color.Blue);
box.AppendText(Environment.NewLine);
new Form {Controls = {box}}.ShowDialog();
Note that you may notice some flickering if you're outputting a lot of messages. See this C# Corner article for ideas on how to reduce RichTextBox flicker.
I have expanded the method with font as a parameter:
public static void AppendText(this RichTextBox box, string text, Color color, Font font)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.SelectionFont = font;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
This is the modified version that I put in my code (I'm using .Net 4.5) but I think it should work on 4.0 too.
public void AppendText(string text, Color color, bool addNewLine = false)
{
box.SuspendLayout();
box.SelectionColor = color;
box.AppendText(addNewLine
? $"{text}{Environment.NewLine}"
: text);
box.ScrollToCaret();
box.ResumeLayout();
}
Differences with original one:
Possibility to add text to a new line or simply append it
No need to change selection, it works the same
Inserted ScrollToCaret to force autoscroll
Added SuspendLayout/ResumeLayout calls for better performance
EDIT : sorry this is a WPF answer
I think modifying a "selected text" in a RichTextBox isn't the right way to add colored text.
So here a method to add a "color block" :
Run run = new Run("This is my text");
run.Foreground = new SolidColorBrush(Colors.Red); // My Color
Paragraph paragraph = new Paragraph(run);
MyRichTextBlock.Document.Blocks.Add(paragraph);
From MSDN :
The Blocks property is the content property of RichTextBox. It is a
collection of Paragraph elements. Content in each Paragraph element
can contain the following elements:
Inline
InlineUIContainer
Run
Span
Bold
Hyperlink
Italic
Underline
LineBreak
So I think you have to split your string depending on parts color, and create as many Run objects as needed.
It`s work for me! I hope it will be useful to you!
public static RichTextBox RichTextBoxChangeWordColor(ref RichTextBox rtb, string startWord, string endWord, Color color)
{
rtb.SuspendLayout();
Point scroll = rtb.AutoScrollOffset;
int slct = rtb.SelectionIndent;
int ss = rtb.SelectionStart;
List<Point> ls = GetAllWordsIndecesBetween(rtb.Text, startWord, endWord, true);
foreach (var item in ls)
{
rtb.SelectionStart = item.X;
rtb.SelectionLength = item.Y - item.X;
rtb.SelectionColor = color;
}
rtb.SelectionStart = ss;
rtb.SelectionIndent = slct;
rtb.AutoScrollOffset = scroll;
rtb.ResumeLayout(true);
return rtb;
}
public static List<Point> GetAllWordsIndecesBetween(string intoText, string fromThis, string toThis,bool withSigns = true)
{
List<Point> result = new List<Point>();
Stack<int> stack = new Stack<int>();
bool start = false;
for (int i = 0; i < intoText.Length; i++)
{
string ssubstr = intoText.Substring(i);
if (ssubstr.StartsWith(fromThis) && ((fromThis == toThis && !start) || !ssubstr.StartsWith(toThis)))
{
if (!withSigns) i += fromThis.Length;
start = true;
stack.Push(i);
}
else if (ssubstr.StartsWith(toThis) )
{
if (withSigns) i += toThis.Length;
start = false;
if (stack.Count > 0)
{
int startindex = stack.Pop();
result.Add(new Point(startindex,i));
}
}
}
return result;
}
Selecting text as said from somebody, may the selection appear momentarily.
In Windows Forms applications there is no other solutions for the problem, but today I found a bad, working, way to solve: you can put a PictureBox in overlapping to the RichtextBox with the screenshot of if, during the selection and the changing color or font, making it after reappear all, when the operation is complete.
Code is here...
//The PictureBox has to be invisible before this, at creation
//tb variable is your RichTextBox
//inputPreview variable is your PictureBox
using (Graphics g = inputPreview.CreateGraphics())
{
Point loc = tb.PointToScreen(new Point(0, 0));
g.CopyFromScreen(loc, loc, tb.Size);
Point pt = tb.GetPositionFromCharIndex(tb.TextLength);
g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(pt.X, 0, 100, tb.Height));
}
inputPreview.Invalidate();
inputPreview.Show();
//Your code here (example: tb.Select(...); tb.SelectionColor = ...;)
inputPreview.Hide();
Better is to use WPF; this solution isn't perfect, but for Winform it works.
I created this Function after researching on the internet since I wanted to print an XML string when you select a row from a data grid view.
static void HighlightPhrase(RichTextBox box, string StartTag, string EndTag, string ControlTag, Color color1, Color color2)
{
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0; ; )
{
int jx = s.IndexOf(StartTag, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0) break;
int ex = s.IndexOf(EndTag, ix, StringComparison.CurrentCultureIgnoreCase);
box.SelectionStart = jx;
box.SelectionLength = ex - jx + 1;
box.SelectionColor = color1;
int bx = s.IndexOf(ControlTag, ix, StringComparison.CurrentCultureIgnoreCase);
int bxtest = s.IndexOf(StartTag, (ex + 1), StringComparison.CurrentCultureIgnoreCase);
if (bx == bxtest)
{
box.SelectionStart = ex + 1;
box.SelectionLength = bx - ex + 1;
box.SelectionColor = color2;
}
ix = ex + 1;
}
box.SelectionStart = pos;
box.SelectionLength = 0;
}
and this is how you call it
HighlightPhrase(richTextBox1, "<", ">","</", Color.Red, Color.Black);
private void Log(string s , Color? c = null)
{
richTextBox.SelectionStart = richTextBox.TextLength;
richTextBox.SelectionLength = 0;
richTextBox.SelectionColor = c ?? Color.Black;
richTextBox.AppendText((richTextBox.Lines.Count() == 0 ? "" : Environment.NewLine) + DateTime.Now + "\t" + s);
richTextBox.SelectionColor = Color.Black;
}
Using Selection in WPF, aggregating from several other answers, no other code is required (except Severity enum and GetSeverityColor function)
public void Log(string msg, Severity severity = Severity.Info)
{
string ts = "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
string msg2 = ts + msg + "\n";
richTextBox.AppendText(msg2);
if (severity > Severity.Info)
{
int nlcount = msg2.ToCharArray().Count(a => a == '\n');
int len = msg2.Length + 3 * (nlcount)+2; //newlines are longer, this formula works fine
TextPointer myTextPointer1 = richTextBox.Document.ContentEnd.GetPositionAtOffset(-len);
TextPointer myTextPointer2 = richTextBox.Document.ContentEnd.GetPositionAtOffset(-1);
richTextBox.Selection.Select(myTextPointer1,myTextPointer2);
SolidColorBrush scb = new SolidColorBrush(GetSeverityColor(severity));
richTextBox.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, scb);
}
richTextBox.ScrollToEnd();
}
I prepared a little helper for the RichTextBox control which makes it very easy to generate colored text on the screen:
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Common.Helpers
{
public class ColouredText
{
public string Text;
public Color Foreground;
public Color Background;
public ColouredText(string text, Color foreground, Color background)
{
Text = text;
Foreground = foreground;
Background = background;
}
public ColouredText(string text, Color foreground) : this(text, foreground, Color.Transparent) { }
public ColouredText(string text) : this(text, Color.Transparent, Color.Transparent) { }
}
public static class RichTextBoxHelper
{
private static RichTextBox _AppendText(RichTextBox box, string text, Color foreColor, Color backColor)
{
if (string.IsNullOrEmpty(text)) return box;
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = foreColor;
box.SelectionBackColor = backColor;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
return box;
}
private static void _UpdateText(RichTextBox box, IEnumerable<ColouredText> newTextWithColors)
{
box.Text = "";
foreach (var text in newTextWithColors)
{
var foreColor = text.Foreground; if (foreColor == Color.Transparent) foreColor = box.ForeColor;
var backColor = text.Background; if (backColor == Color.Transparent) backColor = box.BackColor;
_AppendText(box, text.Text, foreColor, backColor);
}
}
public static void UpdateText(this RichTextBox richTextbox, IEnumerable<ColouredText> text)
{
if (richTextbox.InvokeRequired) richTextbox.Invoke((MethodInvoker)(() => { _UpdateText(richTextbox, text); }));
else _UpdateText(richTextbox, text);
}
public static void UpdateText(this RichTextBox richTextbox, ColouredText text)
{
var list = new List<ColouredText>() { text };
if (richTextbox.InvokeRequired) richTextbox.Invoke((MethodInvoker)(() => { _UpdateText(richTextbox, list); }));
else _UpdateText(richTextbox, list);
}
}
}
and now you can use:
var text = new List<ColouredText>()
{
new ColouredText($"text#1 ", Color.Black),
new ColouredText($"text#2 ", Color.Red, Color.Yellow),
new ColouredText($" "),
new ColouredText($"text#2 ", Color.White, Color.Black)
};
richTextBox1.UpdateText(text);
or simpler usage for single-line text:
richTextBox1.UpdateText(new ColouredText($"warning message", Color.Yellow, Color.Red));

checkedlist box uncheck remove items

I've 2 checklistboxes. 1st checklist box is sublist and 2nd checklist box is brandlist. There are multiple items in sublist checklistbox. Once checked it will fill in brandlist checkbox. But the problem is if I uncheck also, it is filled again in brandlist checkbox.
The following code fills in brandlist checkbox. How to remove the items when unchecked? I am very new to dotnet.
private void chklstSubList_SelectedIndexChanged(object sender, EventArgs e)
{
string Sub = chklstSubList.SelectedItem.ToString();
Fill_Brands(Convert.ToInt32(Sub));
}
this is the Fill_Brands function
private void Fill_Brands(int Sub)
{
DataTable sourceTable = new DataTable();
sourceTable = ppfcObj.GetBrandMasterListFromRMS(Sub);
for (int i = 0; i < sourceTable.Rows.Count; i++)
{
string Brand = Sub.ToString() + "-" +
sourceTable.Rows[i][0].ToString().Trim() + "-" +
sourceTable.Rows[i][1].ToString().Trim();
chklstMstrBrandList.Items.Add(Brand);
}
}

JFace TableViewer get multiple selected rows

I want to add a listener to the select button which gets the multiple rows selected in the checklist tableviewer. It then proccedes to check those boxes.
My question is how do I get the list of rows selected in the tableviewer?
Here is the code for the table:
private void createCheckViewer(Composite parent){
tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.MULTI| SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
if ( (GanttFrame.getListOfFunctionTasks()!= null)){
if (!(GanttFrame.getListOfFunctionTasks().isEmpty())){
// Data Rows
for (int i = 0; i < GanttFrame.getListOfFunctionTasks().size(); i++) {
tableViewer.add(GanttFrame.getListOfFunctionTasks().get(i));
GanttFrame.getListOfFunctionTasks().get(i).setCheckId(i);
}
}
}
// flow trace or function trace
String columnHeader;
if (TraceData.getFlowTraceFlag()){
columnHeader = "Flow Traces";
}else{
columnHeader = "Function Traces";
}
// define layout for the viewer
gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1);
gridData.heightHint = 400;
tableViewer.getControl().setLayoutData(gridData);
tableViewer.addCheckStateListener(this.getCheckListListener());
final Table table = tableViewer.getTable();
TableLayout layout = new TableLayout();
TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD);
col.getColumn().setText(columnHeader);
layout.addColumnData(new ColumnWeightData(500));
table.setLayout(layout);
table.setHeaderVisible(true);
table.setLinesVisible(true);
}
Use:
IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
You then have various choices for processing the selection:
Object [] selections = selection.toArray();
List<?> selections = selection.toList();
Iterator<?> iterator = selection.iterator();

RadGrid Dynamic DataBound Columns - Check Each Cell Value

I have a Radgrid who has Dynamic Databound fields which comes from a database query and each time they are different. Now when this data is displayed on the GRID, i want to change the value of Cells where its 0 to " " or "."
Thanks
You need to hook into the CellFormatting event. Here you can check the value of the cell and change it as required.
Something along the lines of:
Private Sub RadGrid_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles RadGrid.CellFormatting
if e.CellElement.RowInfo.Cells("ZeroColumn").Value = "0" then
e.CellElement.RowInfo.Cells("ZeroColumn").Value = "."
end if
End Sub
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 3; i < e.Row.Cells.Count; ++i)
{
TextBox tb = new TextBox();
tb.ID = "txtRow" + e.Row.RowIndex.ToString() + "Column" + i.ToString();
tb.Width = 50;
//if (Convert.ToBoolean(e.Row.Cells[i].Text) == false)
//tb.Text = "0";
//tb.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(tb);
// e.Row.Cells[i].Enabled = Convert.ToBoolean(e.Row.Cells[i].Text);
}
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
}
}
catch (Exception ex)
{ }

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.