I have two dynamic textboxes in different Panels. When I click the button save it saves multiple records on the system. for example: I have two textbox on each panel. On the Panel 1 lets say it has (a, b) and On the Panel 2 lets say it has (1 ,2). What I'm expecting a result is a1 and b2. but the result goes like this a1 a2 b1 b2 which is wrong. Please help. here's the code of the button.
For Each text2 As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
For Each text1 As TextBox In pnlTextdesc.Controls.OfType(Of TextBox)()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("prod").ToString)
con.Open()
Try
Using cmd As New SqlCommand("INSERT INTO tblProduct (ProductName,productdescription) VALUES (#ProductName,#productdescription) ")
cmd.Parameters.AddWithValue("#ProductName", text2.text)
cmd.Parameters.AddWithValue("#productdescription", text1.text)
cmd.Connection = con
cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
Next
Next
You get that result because you are using nested For Each loops. If you have two letters and two numbers and, for each letter, you combine it with each number, of course you'll end up with four combinations. What you should do is create two lists of TextBoxes from the two Panels, then use a single For loop to combine them by index, e.g.
Dim list1 = TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)().ToArray()
Dim list2 = TextBox In pnlTextdesc.Controls.OfType(Of TextBox)().ToArray()
For i = 0 To list1.GetUpperBound(0)
Dim text1 = list1(i)
Dim text2 = list2(i)
'...
Next
This assumes that there will be the same number of TextBoxes in each Panel and that they are in corresponding order.
Related
I've a situation to create a 16 digit number from another integer. It should be like a credit card number. So what is our scenario is if that users ID is 1 or 2, that should be hashed to a 16 digit string (Numeric). So that 16 digit should be unique for 1
I tried to .NET builtin functions like generate hash etc.
That doesn't helped me for a perfect solution
Not sure how many users you expect, but this code produces 16 digit integers and using the numbers from 1 to 100 there were no repeats:
Imports System.Security.Cryptography
Dim sha As New SHA1CryptoServiceProvider()
Dim IntList As New List(Of ULong)
For I = 1 To 100000
'Need a byte array for the ComputeHash method
Dim data() As Byte = BitConverter.GetBytes(I)
If BitConverter.IsLittleEndian Then Array.Reverse(data)
'Store the 160 bit hash in a byte array
Dim result As Byte() = sha.ComputeHash(data)
'Bitconverter's result can be too long, so by taking the first 16 digits _
of the results that are too long, and padding the rest to the right with _
0's we end up with unique 16 digit integers
Dim HashInt As ULong = ULong.Parse(BitConverter.ToUInt64(result, 0).ToString.PadRight(16, "0"c).Substring(0, 16))
'Using a list to hold the hash's is just to confirm that each one is unique. _
for the your purposes I would suggest a dictionary(of integer, ulong)
If Not IntList.Contains(HashInt) Then
IntList.Add(HashInt)
End If
Next
UPDATE: modified the code to show that it will produce 100000 unique hash's. IntList.Count = 100000.
For results that end up being less than 16 digits, I padded the end with 0's. This is just convenient. By putting the BitConverter.ToUInt64 result into a string you can insert the 0's anywhere you like.
Maybe you could use this:
string SixteenDigitHash(int value)
{
var rnd = new Random(value);
StringBuilder sb = new StringBuilder(16);
sb.Append(rnd.Next(1,10)); // first digit 1..9
for (int i=1; i<16; i++)
{
sb.Append(rnd.Next(0,10)); // other digits 0..9
}
return sb.ToString();
}
It uses Random to generate (pseudo) random numbers, but uses the value-to-hash as seed so it always generates the same sequence for a given value and different sequences for different values.
One problem: the sequence is not guaranteed to be the same for different versions of the framework. Maybe you should use your own implementation of that Random class so that you know that the sequence is stable.
I am using DevExpress xtragrid that is bound to a data source... all is fine there. I am adding 1 unbound column (balance) that will hold the result of a calculation. The 'balance' column MUST recalculate when the debit and / or credit column changes anywhere in the grid. Given there may be a large amount of records I am hoping a loop statement will not be my only option. Rather I was hoping for a solution using the
Expression editor.
example:
dr cr balance
100 0 100
0 50 50
0 45 5
Create a new column in your data source called Amount. In this column you will store both the debits and credits as positive and negative values
Add a new column to your xtragrid. Name it colRunningBalance
Set the UnboundType of the column to decimal. This tells the grid
you will be processing the data yourself
Add the following event to your form for the grid. The code is in VB.NET but it should be pretty easy to convert to any language
Private Sub gridView_CustomUnboundColumnData(sender As System.Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles
gvCash.CustomUnboundColumnData
Dim view = DirectCast(sender, GridView)
If e.Column.FieldName = "colRunningBalance" And e.IsGetData Then
Dim total = 0D
For i As Integer = -1 To e.ListSourceRowIndex - 1
total += CDec(view.GetListSourceRowCellValue(i + 1, "Amount"))
Next
e.Value = total
End If
End Sub
I have a fixed size table databound to a gridview from a stored procedure:
Note: the table is 17 columns long.
Currently I'm using a For..Next statement to alter the background colour of the percentage row. Can I adapt this to use DataFormatString="{0:0.0%}" instead of the backcolor attribute or should I be doing this another way?
Here's my code which currently turns the background of the row purple:
For colCount As Integer = 1 To 17
For rowCount As Integer = 1 To 1
gvAttainmentGrid.Rows(rowCount).Cells(colCount).Attributes = Drawing.Color.FromArgb(177, 160, 199)
Next
Next
EDIT
I've made some progress. I have updated my For..Next and although this displays OK on page load, when I select a different value in the control or add the element to my pre-render procedure it errors out with the message:
Input string was not in a correct format.
My code now looks like the following:
For colCount As Integer = 1 To 17
For rowCount As Integer = 1 To 1
gvAttainmentGrid.Rows(rowCount).Cells(colCount).Text = FormatPercent(Convert.ToDecimal(gvAttainmentGrid.Rows(rowCount).Cells(colCount).Text), 1)
Next
Next
I have one dataset in which i have say 10 rows..
I want to skip data of first two column means whatever data is there in that first 2 column
sholud not present in that dataset how to do this..?
What is the reason? Are you trying to do this, because you did not want it to appear in the DataGrid... Or are you trying to do some merge operations...
If for datagrid, you can just hide those columns in the DataGrid/Gridview...
If it is otherwise you can remove the columns as stated by Tim...
If you mean a Datatable instead of a dataset you can remove colums:
Dim removeCount As Int32 = 2
For i As Int32 = 1 To removeCount
For ii As Int32 = 0 To myDataTable.Columns.Count - 1
If myDataTable.Columns.CanRemove(myDataTable.Columns(ii)) Then
myDataTable.Columns.RemoveAt(ii)
Exit For
End If
Next
Next
Removes the two first columns(if they are removable and not primary keys).
Or even simpler, when removing the primary key from the datatable is acceptable:
For i As Int32 = 1 To removeCount
If Not myDataTable.Columns.CanRemove(myDataTable.Columns(0)) Then
myDataTable.PrimaryKey = Nothing
End If
myDataTable.Columns.RemoveAt(0)
Next
I was wondering if anyone can point me in the right direction please? Say I am pulling the following table, I would then like to select an ID randomly. I understand how to select a random number using a Randomize() call followed by the relevant syntax but I want to pre-define the range.
i.e. Table Data
ID | Name
4345 Mike
3456 Lee
4567 John
There will be many more names but for this example you could use 3 or 4 etc..
Please help I'm starting to itch :o|
Just to make sure I understand what you want:
Given a table, you want to randomly select one of the ID values from that table.
If so, this should do it:
Dim rand As New Random()
Dim record As Integer = rand.[Next](0, myDataTable.Rows.Count)
Dim randomID As Integer = CInt(myDataTable.Rows(record)("ID"))
We have all the information we need to randomly select a row, and by extension randomly select one of the ID values in the table.
In old Vb you would do
Dim i as integer
i = (Rnd * (maxval-minval)) + minval
Since rnd returns a random number between 0 and 1 you would scale the number to the right range.