|
Hi,
thank you for you guide my friend.
best wishes.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table.
thank you in advance.
here is my code:
OleDBConnectio con=new OleDBConnection(conectionString);
con.Open();
OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con);
OleDBDataReader dr;
dr=com.ExecuteReader();
While(dr.read())
{
Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString());
}
dr.Close();
con.Close();
i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically.
thanks a lot.
|
|
|
|
|
Dgv1.Rows.Clear();
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi, thank you legitimate programmer.
thanks a million for your help
|
|
|
|
|
Member 13325846 wrote: new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)
That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name.
Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have Id and Name columns.
It would also be better to only load the specific columns you need, rather than using SELECT * FROM ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
Thank You For your care.
Yes your right and this was just an example and I am using from a cboBox.
Thanks a million
|
|
|
|
|
Good idea ? Bad idea ? Would you ever use in your own code ? Code smells ?
A recent thread here [^] asked for help with (one-way) binding a group of TextBoxes to integer valus in an Array. I responded with an example of using 'ObservableCollection.
The first version I hard-coded (for POC) to match the thread cited's requirement; then, I had an idea that with such a simple one-way binding scenario a Class inheriting from List<T> ... where the generic parameter represented the Type of objects to be synced ...with a custom indexer ... could work for similar situations.
The next version, I made objects to be synced to the array generic, and put List value change code in a Func template to do the work of whatever a change in the Array/List values required. Here's the code:
public class TUpDater<T> : List<int>
{
public TUpDater(Func<int, int, T, bool> mapfunc, params T [] syncobjects)
{
MapFunc = mapfunc;
SyncObjects = new List<T>();
for (int i = 0; i < syncobjects.Length; i++)
{
SyncObjects.Add(syncobjects[i]);
}
}
public Func<int, int, T, bool> MapFunc { set; get; }
public List<T> SyncObjects { get; }
public new int this[int ndx]
{
set
{
base[ndx] = value;
if (MapFunc?.Invoke(ndx, value, SyncObjects[ndx]) != null)
{
}
}
get
{
return base[ndx];
}
}
} Here's a usage example:
TUpDater<TextBox> tba;
List<TextBox> TBoxes = new List<TextBox>();
List<int> Ints { set; get; }
private void Form1_Load(object sender, System.EventArgs e)
{
tba = new TUpDater<TextBox>(
(ndx, val, tbx) =>
{
tbx.Text = val.ToString();
return true;
}
);
Ints = new List<int> { 11, 22, 33, 44, 55 };
tba.AddRange(Ints);
for (int i = 0; i < 5; i++)
{
TextBox tb = new TextBox { Name = "Ints" + (i + 10), Text = i.ToString() };
tba.SyncObjects.Add(tb);
}
tba[3] = 1000;
var xxx = tba[3];
} Comment: if I take this further, I think I'd provide an optional CTor overload where the user could pass both List Int values, and T objects.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
I don't think it makes sense to inherit from List<T> . It would be almost impossible to ensure that the SyncObjects list had the same number of items as the outer list.
I was also slightly confused by the value stored in the list, until I realised it was meant to be the value of the control at the specified index. It would probably be better to make that a generic type parameter as well.
Perhaps something like this?
public class ControlUpdater<TControl, TValue> : IReadOnlyList<TValue>
{
private readonly List<(TControl control, TValue value)> _items = new List<(TControl, TValue)>();
private readonly Func<TControl, TValue, bool> _setValue;
public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<(TControl, TValue)> items)
{
_setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
if (items != null) AddRange(items);
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, params (TControl, TValue)[] items) : this(setValue, items.AsEnumerable())
{
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<TControl> items)
{
_setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
if (items != null) AddRange(items);
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, params TControl[] items) : this(setValue, items.AsEnumerable())
{
}
public ControlUpdater(Func<TControl, TValue, bool> setValue) : this(setValue, default(IEnumerable<(TControl, TValue)>))
{
}
public void AddRange(IEnumerable<(TControl, TValue)> items) => _items.AddRange(items);
public void AddRange(IEnumerable<TControl> items, TValue value = default(TValue)) => _items.AddRange(items.Select(c => (c, value)));
public void Add((TControl, TValue) item) => _items.Add(item);
public void Add(TControl control, TValue value = default(TValue)) => _items.Add((control, value));
public void Insert(int index, (TControl, TValue) item) => _items.Insert(index, item);
public void Insert(int index, TControl control, TValue value = default(TValue)) => _items.Insert(index, (control, value));
public void RemoveAt(int index) => _items.RemoveAt(index);
public int Count => _items.Count;
public TValue this[int index]
{
get
{
var (_, value) = _items[index];
return value;
}
set
{
var (control, _) = _items[index];
if (_setValue(control, value))
{
_items[index] = (control, value);
}
}
}
public IEnumerator<TValue> GetEnumerator()
{
foreach (var (_, value) in _items)
{
yield return value;
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
} Usage:
var tba = new ControlUpdater<TextBox, int>(
(tbx, value) =>
{
tbx.Text = value.ToString();
return true;
});
var controls = Enumerable.Range(0, 5).Select(i => new TextBox { Name = $"Ints{i + 10}" });
tba.AddRange(controls);
tba[3] = 1000;
var xxx = tba[3];
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard ! I look forward to studying your code.
The issue of guaranteeing one T for each Index is also on my mind: they, imho, should be tightly coupled.
In another version of this, I made the 'CTor accept a Param array Type of KeyValuePair<T, int>. With a KeyValuePair, of course, if you change an 'Int value, you get a copy back, and have to re-assign the copy: there goes read-only !
I am aware there are some implementations of an 'ObservableDictionary out there (including one in MS Microsoft ParallelExtensionsExtras) that might come in handy. was also slightly confused by the value stored in the list, until I realised it was meant to be the value of the control at the specified index. It would probably be better to make that a generic type parameter as well. Agreed: it's funny how my mind kept needing to be refreshed about how that worked
I struggle to evaluate the utility of a code strategy like this; part of me says it's too clever, or, of very limited use. I think of a bright student: wouldn't it be better to teach them how to implement INotify ... or use Observable Collection ... in terms of them learning a more generalizable method ?
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: Good idea ? Bad idea ? Under what circumstance, and for whom?
It's a good idea at least to post it, as it is always good to learn alternative ways of doing things.
Based on the title I'd compare it to classical databinding; a oneliner that doesn't generate the UI (textBox1.Bindings.Add ). This example feels more like a PropertyEditor for (int) collections - something that may make prototyping a bit quicker
What was the question again? Code-review; you don't want that. It is a short example meant to demonstrate an idea, not something to nit-pick at
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I linked to the thread here that "spawned" this post in the first paragraph of this post. Oh, I do want a code review ... I can only benefit from whatever my peers, and mentors, here, say !Eddy Vluggen wrote: I'd compare it to classical databinding; a oneliner that doesn't generate the UI (textBox1.Bindings.Add Good point: here the issue is binding a group of TextBoxes' 'Text property to elements in a List of Int32. Can you post a short example of doing this ? My bias against using WinForm databinding goes way back, and I'd be happy to get rid of it.
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: Oh, I do want a code review ... I can only benefit from whatever my peers, and mentors, here, say ! Too bad you got a reaction of me instead
The boolean in the mapfunc is for obvious for future use, but TBoxes is an abbreviation (hope I spelled that right). Since it is a private property, I wonder whether it should be a private field, an internal/public property, or a read-only property.
BillWoodruff wrote: here the issue is binding a group of TextBoxes' 'Text property to elements in a List of Int32. Can you post a short example of doing this ? A textbox is for text; for an int I prefer a numericUpDown-control.
for (int i=0; i<myIntList.Count)
myNumericUpDownList[i].DataBindings.Add("Text", myIntList[i], "Port");
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Appreciate your comments !Eddy Vluggen wrote: TBoxes is an abbreviation (hope I spelled that right). Since it is a private property, I wonder whether it should be a private field, an internal/public property, or a read-only property. The goal is to keep that list of TextBoxes local, unchanged. In fact, in the code for the Class I use a read-only Property: public List<T> SyncObjects { get; } to store the TextBoxes. In the usage example the property is private, but, yes, it would be better to make that read-only.
Re: your code example: what does "Port" refer to ? Mapping a Property of one Control to another in WinForm is one of the simpler databinding tasks, but, in this case the tricky part is that the List elements change, not the Control ... am I missing the point ?
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: what does "Port" refer to ? In that case it refers to a local port on the machine (copy/paste examples ftw!).
--edit
Ah, should point to the Member name to bind to of the myIntList[i] -object provided. So what is the name of the member in an array? I think I just bumped into your point
--/edit
BillWoodruff wrote: Mapping a Property of one Control to another in WinForm is one of the simpler databinding tasks, but, in this case the tricky part is that the List elements change, not the Control ... am I missing the point ? Yes and no. It is true that binding a single value is simple; when binding a collection, I assign it as a datasource and put it in a grid. Or worse, an ugly propertyeditor.
I'm missing the statement where these textboxes are assigned to the owning control. How is the taborder of your generated textboxes?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: I'm missing the statement where these textboxes are assigned to the owning control. How is the tab-order of your generated textboxes? That stuff is omitted: I just cooked up some TextBoxes on the fly to test with ... you have opened up an interesting idea with this query: could you take the container Control and somehow bind to its ControlCollection ? Never seen an example of that. But, that sure don't mean it ain't doable.
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Nice thought, but how do you discern between controls that want binding, and regular controls/deliberate unbound controls?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
While I don't plan to investigate this line of inquiry, a few ideas come to mind:
1. use Tags on the Controls
2. assume all the Controls in a given ContainerControl are to be watched
3. use reflection and run-time code construction + compilation to build INotify... into selected Controls
But, me brain immediately starts shakin' my head at these ideas
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: But, me brain immediately starts shakin' my head at these ideas That's probably why it is not part of the framework, and sold separately as "MS Access"
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I'm utterly "gobsmacked" by the brilliance and utility of your new "pattern".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Okay, after you finish letting that minor wound fester I look forward to you bringing your find technical mind to the discussion.
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Nah ... too much chance I may stray from your agenda.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Kindly someone help me on this by proving code
|
|
|
|
|
Sorry, this site does not provide code to order.
|
|
|
|
|
I am using below query
SqlConnection con = new SqlConnection("server = MUNESH;Database=datastore;UID=sa;Password=123;");
SqlCommand cmd = new SqlCommand("Select * from data1", con);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
BindingSource bsource = new BindingSource ();
bsource .DataSource = dt;
dataGridView1.DataSource = bsource ;
da.Update(dt);
DataSet ds = new DataSet("New_DataSet");
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
da.Fill( dt );
ds.Tables.Add( dt );
ExcelLibrary. DataSetHelper.CreateWorkBook("MyFirstExcelSheet.xls" ,ds);
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
Once its export the same data for three times in the same excel sheet
|
|
|
|
|