How do I associate colors with a a specific user in a datatable - vb.net

I'm fairly new to using Visual Basic. And I have never worked with databases/datatables before now.
On my datatable (I hope that's the correct word) I have two columns that I want to store color values. I changed the data type to "System.Drawing.Color".
However it would appear I cannot simply type in the string (e.g: "Color.Black") to add it to the database. And when using an update SQL query and make it look for color values on the form, it wants to convert them to string.
Is it possible to store color values like this in this way or do I have use another method?

All colors are presented by a string on either "Color.Black" or "#000000", so the only thing you can do is convert it to a string when you put into DataTable, then convert it back to a color when you retrieve it from the Datatable

Related

VB.NET IIF Statement

In VB.NET I am trying to use SQL Parameter to send data to SQL Server Database, I am trying to convert string to decimal with following code if the value of the datagridrow is nothing then send 0 or use conversion to decimal argument.
SQL.Addparam("#amt", IIf(IsNothing(DataGridView2.Rows(index).Cells(7).Value), 0, CDec(DataGridView2.Rows(index).Cells(7).Value)))
(SQL and Addparam is called from class I created)
But with this code I get error "System.InvalidCastException: 'Conversion from string "" to type 'Decimal' is not valid.'"
I need help on this please.
Thank you
The specific issue is clearly that your "empty" cell does not actually contain a null reference, i.e. Nothing, but rather an empty String. You would need to test for an empty String as well as or instead of Nothing. If your cell might contain either Nothing, an empty String or a Decimal value, this should work:
Dim cellValue = DataGridView2.Rows(index).Cells(7).Value
SQL.Addparam("#amt",
If(String.IsNullOrEmpty(CStr(cellValue)),
Decimnal.Zero,
CDec(cellValue)))
Note the use of If rather than IIf and an actual Decimal value for the zero. String.IsNullOrEmpty will detect Nothing or an empty String in a single call.
That's really not the best solution though.
It appears that you are using an unbound grid and then looping through the rows and saving each row individually. That is really the wrong way to go. What you should be doing is creating a DataTable with the appropriate schema, populating it from the database if required, binding it to the grid, performing the required edits and then just saving the whole DataTable in one go with a single call to Update on a data adapter.
If you do that then an "empty" cell will contain DBNull.Value and ADO.NET will automatically save NULL to your database. If you don't want NULL values in a column then just set the AllowDBNull property to False and the DefaultValue property to whatever you want instead.
To create the DataTable schema, you can call Fill on a data adapter to do that and populate with data, or you can call FillSchema to not retrieve any data. Alternatively, you can just build the schema yourself.

convert text to single type

Dim fontsize As Single = CSng(SynopsisTSCmbFontSize.Text)
rtbSynopsis.Font = New Font(SynopsisTSCmbFonts.Text, fontsize)
to change the fontsize to the value selected in a combo box, the value has to be of the Single type.
the combobox is populated with numbers entered at the design mode, ranging from 7-78. I know that these are entered as strings.
the error is :
I have tried a number of things to convert the text (which are numbers, no letters) from the combobox to single to no avail. try parse did not work, trimming did not work, first convert to INT or DBL, then to SNG did not work.
What is the correct syntax here?
I would have thought that it was pretty standard stuff to change the fontsize.
I found a solution : instead of populating the combobox at design time, I populated it at runtime where I had full control over the type.
Dim i As Single
For i = 5 To 70
SynopsisTSCmbFontSize.Items.Add(i)
TreatmentTSCmbFontSize.Items.Add(i)
Next
once the comboboxes are correctly populated, I can run the rest of the code with no errors
thank you all for your time!

Select & Assign Specific Row of a ListBox to a Variable As String

I have tried to find an answer to this but have had no luck. I am using VB.NET and VisualStudio 2019
I have a listbox (lboxsectionnames)that has several different section names listed (as shown in the screenshot). I am trying to specify a specific row, and assign that row to a variable as text.
For this instance I am trying to get the first row text, however, I would like to be able to specify row # in future and get text as well.
Dim firstSectionName
lboxSectionNames.GetItemText(0)
firstSectionName = lboxSectionNames.SelectedIndex.ToString
ListBox Text Example
ListBox.Items is an ObjectCollection and can be accessed with your index like this
lboxSectionNames.Items(0).ToString()
or using SelectedIndex like this:
lboxSectionNames.Items(lboxSectionNames.SelectedIndex).ToString()
although you could also do the above like this:
lboxSectionNames.SelectedItem.ToString()

Visual Basic Trimming Letters From Integer

Is there a way to trim letters of a string? Or Convert a string to an integer removing its letters from it?
Story:
I have in which I have bunch of options on an application and all of them add up points depending on the option selected by the user. I gave them values like hsq2 hsq4 hsq6 and now I need to be able to get the one that is selected and remove letters from it and add up values. If you know a better way of doing this please let me know also..
The better way would be to use XAML and databinding to a rich ViewModel that has separate properties for each item's label and its score value as an integer. Then a method on your ViewModel would perform the sum of selected values and return it for data binding to the Total Score display. Search for MVVM if you want to know more about that approach.
However, since it looks like you're building a WinForms app, you could use the Tag property of each element to store the integer value. That's how I would have done it 7 years ago.
There are better designs you could use to avoid the issue, but to extract the number from a string, you could use a regex (now you have two problems, as they say!).
Dim match = RegEx.Match("blah2", "\d+")
If match.Success Then
Dim val = CInt(match.Value)
' val = 2
End If

Access 2013 data types

I just started using Access 2013 and there are two new data types, Short Text and Long Text. I need to programatically add a table in SQL and I used to use TEXT(100) or something like it, but what do I use to create a column of type Long Text? I need the field to be like the memo type in previous versions of Access, basically limitless or very large.
You can use Long Text just like the memo type:
In earlier version (before A2007) we had the RTF presentation for Memo
fields. The implementation was - IIRC - storing the text you had on
the screen in Word RTF format with some cryptic codes in {} to
indicate the formatting. In A2007 they changed to the new formatting
known as Rich Text. This is stored in the Memo field with HTML tags
implement the formatting. The thing they changed now is not the UI for
these fields but the name of the datatype in the database. It's not
called MEMO anymore, now it's called LONG TEXT. The formatted text
should AFAIK still be stored in the same way, as HTML coded text. When
you switch from plain text to rich text you do nothing else then tell
Access to use another UI to display the content of the data in the
database. Stored is still the same.
You can read more from here
Oops, I found it. It's just LONGTEXT.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms714540(v=vs.85).aspx