|
The CLR classes are the same, whether you call them from a VB.NET app or a C# app. So if the C# version doesn't work for you, well, something got lost in translation. Wasn't there a ByRef in your VB.NET app?
Midnight Ahri wrote: data = ReceivingClient.Receive(ref(endPoint));
And that is too many parentheses to my taste.
|
|
|
|
|
Hi Guys,
I've made a program to read data from an SQL database and show those in dynamic comboboxes. Now I would like to save all data back to SQL database after changes was made in those comboboxes.
Any ideea how to accomplish this?
Best regards
Vidor
public void showRequill()
{
DataSet dsRequill = new DataSet();
BindingSource bsRequill = new BindingSource();
SqlDataAdapter daRequill = new SqlDataAdapter();
DataTable dtRequill = new DataTable();
int cnt = 0;
try
{
daRequill.SelectCommand = new SqlCommand("select * from RD1 where UserNumber='" + cbWorkerNr.Text + "'", CN);
daRequill.Fill(dsRequill);
daRequill.Fill(dtRequill);
bsRequill.DataSource = dsRequill.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
foreach (DataColumn column in dtRequill.Columns)
{
newLabel = new Label();
newCombo = new ComboBox();
newLabel.Text = column.ColumnName;
newLabel.AutoSize = true;
newLabel.Visible = true;
newCombo.Size = new Size(35, 21);
newCombo.Items.AddRange(new object[] { "U", "S", "E", "B", "L" });
newCombo.Name = "cbRequill_" + cnt.ToString();
newCombo.Visible = true;
newCombo.DataBindings.Clear();
newCombo.DataBindings.Add(new Binding("Text", bsRequill, column.ColumnName));
newCombo.TextChanged += new EventHandler(newCombo_TextChanged);
flpRequill.Controls.Add(newLabel);
flpRequill.Controls.Add(newCombo);
cnt++;
}
}
|
|
|
|
|
Hi,
what i understand is, you have some list of items to display and then after update that items it should be back to the database. correct ?
so what i suggest is, at the time of updating data. Do not use update query. instead remove all those records and insert it again(using single transaction). because you do not have idea about which data is modified.
Or the other solution is, you need to maintain index of the data you get from database. and then match the string value and update only if it modified. but this way leads to so many questions.
let me know above idea works for you,
thanks
-Amit.
|
|
|
|
|
Hi,
The idea seems to be fine, but i don't know how to implement (using single transaction)
The problem is that the column number could be changed so I need to insert somehow with a "foreach" or "for loop". As you can see I managed the reading with foreach and making comboboxes programatically, and I want to save/update in the same way.
Best regards
Vidor
|
|
|
|
|
Hi,
you have UserNumber to get the information. so create one storedProcedure to update your record. in that storedprocedure create one transaction to remove the entries with given UserName and insert Updated Entries. this is what you need to do.
Does it make sense.
Thanks
-amit
|
|
|
|
|
Off topic, but SQL Injection Attack is possible with:
daRequill.SelectCommand = new SqlCommand("select * from RD1 where UserNumber='" + cbWorkerNr.Text + "'", CN);
You should use parameterized queries to be safer.
SQL Injection Attacks and Some Tips on How to Prevent Them[^]
""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi,
I have a modal form where in the consturctor I grab some data from a text file and create a dataset/datatable that holds the data and binds it to the datagridview control on the form. When the form is displayed, the dataset/datatable isn't displayed but when I debug through the code and get to line 3 it tells me that the datasource has bound to the dataset/datatable and I can view the datasource in the visualizer. The debugger steps through to line 4 then the form is displayed...but without the dataset/datatable.
I have added at design time the datagridview with two columns corresponding to the columns from the text file and this is what is displayed and not the dataset I've bound it too in the constructor.
Is there an issue with binding in the constructor
cheers
public frmAdjustments()
{
InitializeComponent();
line2 _data = BuildDataSet ("AdjustmentTypes.txt","AdjustmentsTable","|");
line3 dataGridView1.DataSource = _data;
}
private void button1_Click(object sender, EventArgs e)
{
line1 frmAdjustments frmA = new frmAdjustments();
line4 frmA.ShowDialog();
}
Mat
|
|
|
|
|
I fixed it.
I was just binding the dataset and not specifying the datatable. Line 3 is now
dataGridView1.DataSource = _data.Tables[0];
I assumed as there was only one datatable that it could work out that was the one to use...don't ever assume
Mat
|
|
|
|
|
matleeds wrote: don't ever assume
The one assumption that seems valid most of the time is this: if it doesn't work, I assume I did something wrong.
|
|
|
|
|
Hi,
Please see the Example below,the same way i opened modal dialog,which is showing Data also.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
DataSet ds = new DataSet("myds");
DataTable dt = new DataTable("mydt");
dt.Columns.Add("mycol");
dt.Rows.Add("a");
ds.Tables.Add(dt);
dataGridView1.DataSource = ds.Tables[0];
}
}
//Opening the Modal dialog.
Form2 f = new Form2();
f.ShowDialog();
|
|
|
|
|
Right now a C#.net 2010 application edits the columns for field1 and field2 separately. Now I need to combine the edits together since the two columns are related to each other alot.
Thus here is the following code, can you tell me how you would combine the edits together for these two columns:
(note: data table is obtained from information loaded into an excel 2003 spreadsheet)
public override List<ColumnNames> GetColumnNames()
{
List<ColumnNames> list = base.GetColumnNames();
list.Add(ColumnNames.filed1);
list.Add(ColumnNames.field2);
return list;
}
public override List<RuleError> ValidateData(DateTime receivedDate)
{
base.ValidateData(receivedDate);
DataTable excelData = ExcelDataTable;
string colName = ColumnNames.field2.ToString();
string field2ColName = colMap[colName].ToString();
colName = ColumnNames.filed1.ToString();
string field1ColName = colMap[colName].ToString();
int iEnd = excelData.Rows.Count;
for (int i = DataRowNumber - 1; i < iEnd; i++)
{
DataRow dr = excelData.Rows[i];
{
List<RuleError> list = Validate(dr, receivedDate, didc);
if (list == null)
list = new List<RuleError>();
string field1 = dr[field1ColName].ToString();
re = Validatefield1ible(field1);
if (re != null)
{
list.Add(re);
}
string colVal = dr[field2ColName].ToString();
re = Validatefield2(colVal);
if (re != null)
{
list.Add(re);
}
}
excelData.AcceptChanges();
didc.Dispose();
return masterList;
}
public override List<RuleError> LoadData(ImportWkbk wb, string destDocLoc)
{
string colName = ColumnNames.field2.ToString();
string field2ColName = colMap[colName].ToString();
colName = ColumnNames.filed1.ToString();
string field1ColName = colMap[colName].ToString();
Hashtable htSubs = new Hashtable();
Int64 submissionID = 0;
VCust firstCust = null;
int iEnd = ExcelDataTable.Rows.Count;
for (int i = DataRowNumber - 1; i < iEnd; i++)
{
DataRow dr = ExcelDataTable.Rows[i];
try
{
string field1 = dr[field1ColName].ToString().Trim().ToUpper();
lis.filed1 = field1.Length > 0 ? field1 : null;
string field2 = dr[field2ColName].ToString().Trim().ToUpper();
lis.field2 = field2.Length > 0 ? field2 : null;
didc.SubmitChanges();
didc.Dispose();
return null;
}
public RuleError Validatefield1ible(string field1)
{
RuleError re = null;
if (field1 == null || field1.Trim().Length < 1)
return null;
if (field1.Trim().ToUpper().Equals (field1ibleStatus.PART) ||
field1.Trim().ToUpper().Equals (field1ibleStatus.aLL)
)
{
return null;
}
re = new RuleError(RuleErrorCodes.OTHER, RuleErrorTypes.ERROR, "error 1");
return re;
}
public RuleError Validatefield2(string field2us)
{
RuleError re = null;
if (field2us == null || field2us.Trim().Length < 1)
return null;
if(field2us.Trim().ToUpper().Equals(field2.NO) ||
field2us.Trim().ToUpper().Equals(field2.YES) ||
field2us.Trim().ToUpper().Equals(field2.maybe)
)
{
return null;
}
re = new RuleError(RuleErrorCodes.OTHER, RuleErrorTypes.ERROR, "error 2.");
return re;
}
}
|
|
|
|
|
dcof wrote: how you would combine the edits together for these two columns
What exactly do you mean by the expression "combine the edits together"?
|
|
|
|
|
dcof wrote: Now I need to combine the edits together since the two columns are related to each other alot.
Wrong. Either they represent the same, in which case you only register one of both facts, or they represent different things. If one is dependent on the other, it would not be editable; it should be a calculated column.
dcof wrote: can you tell me how you would combine the edits together for these two columns
Simple; ask the person who came up with this idea on how they would edit "two textboxes" simultaneously. Yes, it's possible, but there's a damn good reason why only one control can have the input-focus in Windows.
dcof wrote: data table is obtained from information loaded into an excel 2003 spreadsheet
The origin of the table isn't relevant to the question.
Bastard Programmer from Hell
|
|
|
|
|
c# winforms
My DataGridView has:
SelectionMode - CellSelect
EditMode - EditOnKeyStroke
It works well for all Keys except fot DeleteKey (when pressed - nothing happend).
I want - if DeleteKey is pressed - selected Cell (or Cells) stay selected, but - empty value (like in Excel).
Must I create a separate KeyDownEvent for DeleteKey only ?
|
|
|
|
|
The delete key (by default) works for a row and not for a cell.
Create a separate KeyDown / KeyPress capture method to handle the key press for delete key.
|
|
|
|
|
Hi;
I want to convert a list to an array but just part of it,because my array is smaller than the list.
int[] copyselection = (int[])selec.ToArray(typeof(int[]));
I want to limit the array elements for example ,I want to copy just 100 elements of it;
Thank you all in advance
|
|
|
|
|
OK, then there are two methods of doing this.
The first uses a smaller memory footprint as you would use a for loop and copy the data you want from each element in the List to the Array.
The larger memory footprint version is a one-line method, but you'll end up converting your entire List to an array, then Array.Copy[^] out the section you want.
|
|
|
|
|
|
you're too generous.
|
|
|
|
|
Thank you 
|
|
|
|
|
Might I suggest a for loop:
int Limit = selec.Count > 100 ? 100 : selec.Count;
for ( int LoopCount = 0; LoopCount < Limit; LoopCount++ )
copyselection[LoopCount] = selec[LoopCount];
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
you're really too generous.
|
|
|
|
|
I think the Take extension method of IEnumerable can be used for this purpose as shown below:
List<int> data = new List<int>(){1,2,3,4,5,6,7,8,9,10};
int[] array = data.Take(5).ToArray();
The Take method takes specified number of elements or the total elements of the list if the total is less than the specified number.
|
|
|
|
|
Best answer!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|