Store the values from gridview to string in asp.net - c#

I am storing the value of gridview into a string. I have two lines of code.
string UserName = GridView1.Rows[e.RowIndex].Cells[4].Text.ToString(); 1
string UserName = GridView1.Rows[e.RowIndex].Values["UserName"].ToString(); 2
Both will have same Values. Which is the better option to use 1 or 2 ?

I prefer to use the second one, so that if the column index change in time, it's not necessary to rewrite all indexes in code again.

The second one:
Its more future proof, changing the structure of the table would
require you to change the cell index.
Its easier to read, without looking at the variable we can see you are asking for the usernames, We don't have to check what values are in the 4th column.

Related

Find each string in a list from a table column

I have a table that has about 1 million rows. One of the columns is a string, let's call it column A.
Now I need to work on a list L of about 1,000 strings, mostly one or two words, and I need to find all the records in the table where column A contains one of the 1,000 strings in the list L.
The only way I can think of is to use each string in L to do a full table scan, find if the string is a substring of column A content of each row. But that will be O(n2), and for a million rows it will take a very long time.
Is there a better way? Either in SQL or in C# code?
One million rows is a relatively small number these days. You should be able to pull all strings from column A, along with your table's primary key, into memory, and do a regex search using a very long regex composed from your 1000 strings:
var regex = new Regex("string one|string two|string three|...|string one thousand");
Since regex gets compiled into a final automaton, you would get reasonably fast scanning times for your strings. Once your filtering is complete, collect the IDs, and query full rows from the table using them.
The best way to do is is using linq. Lets say that you have your list
List<string> test = new List<string>{"aaa","ddd","ddsc"};
then using Linq you can constract
var match = YourTable.Where (t=> test.Contains(t.YourFieldName);
I suggest looking into full text search, it won't decrease the count of the operations you have to perform but it will increase the performance.
Assuming you use Sql server (you should always use the relevant tag to specify the rdbms),
you can create a DataTable from your List<string> and send it to a stored procedure as a table valued parameter.
Inside the stored procedure you can use a simple join of that table valued parameter to your table on database_table.col contains(table_parameter.value) (using full text search).
Of course, things will go a lot faster if you create a full text index as suggested in the comments by Glorfindel

Linq: Is it possible to change a value of an array from a specific index?

I have this aray
string[] neworden = { "toggler-down", "toggler-down", "toggler-down", "toggler-down", "toggler-down" };
when I click in one of the five column of my table in the webform, I set a variable which indicate the column clicked.
For example : var indexColumn = 3
I would like to change to toggle-up in the column clicked
is it possible in Linq?
Why would you do this with LINQ?
You can assign to a specific element of the array like this,
neworden[indexColumn] = neworden[indexColumn] == "toggler-down" ?
"toggler-up" : "toggler-down";
how would LINQ help?
Additionally,
Consider using bool to represent values that have two states.
I think what you're looking for is to retrieve the column from the webform Table, then change the ordering and the columname. I don't know which framework you are using but this should be very possible. Look up which kind of table you are using for your framework (MVC, Umbraco?), then how to get columns from your Table object. The columns should have standard methods for mutating them.

Adding values to rows, retrieving them and splitting them

I have a MySQL database with column 'apps_owned'. Everytime a user buys an app, I want their row to have the AppID added to it. For example,
apps_owned = 1, 4, 75 etc.
How would I do this?
Next, I need to split these up into individual numbers in C#. I don't know how I would remove the comma's to get a whole number. Is there any way of doing this. I know how to get the numbers from the database, I just don't know how to add to them or split them. Is there any easier way to do this?
Let the DB do the work, have a table represent a users apps. Then you could simply count, add, delete...
To Split you can do something like:
var result = apps_owned.Split(' ').Select(x => int.Parse(x.ToString.Replace(",", ""))).ToArray<int>();

C#: How to check user console input against a column in a .csv?

Yo,
I am fairly new to C# and I'm making a small text based console RPG game just for practice and I was wondering if there was anyway I could make something similar to this:
Ask user for name
Ask user for password
Check if the name is in column 1 of a .csv file
If it is, check if the password is in column 2 and in the same row as the name
If everything checks out, load the values from columns 3 and 4 in the same row as
the name and password into corresponding values like health and level
Here's an example .csv if I didn't really explain it well:
"charactername", "password123", "health", "level"
"Oswald", "498562a", "100", "4"
"Hammerfist", "98457813", "77", "6"
So just for the sake of clarity, this is what I'm thinking:
Enter your username: Oswald
Enter your password: 498562a
Program reads through column 1 of the file looking for "Oswald" and finds a match
Program then checks to see if the password value is in the same row but in
column 2 of the file and checks to see if it matches the user input, it matches it
Program then loads the health and level values into character variables
Entrance Successful
Is there a way to do this? Am I overlooking a simple solution? Or should I try and go about it a different way? I'm just don't want to hard code the values into a Dictionary or List or Array or whatever. I'm not looking for security at this stage either, it's just for practice and fun.
Thanks in advance! Let me know if you need a better understanding of what I'm trying to do and I'll try to explain the best I can.
There are two ways to go about reading csv
Just like standard text file, line by line. Then parse your line into array of columns and get your values
Using Microsoft.ACE.OLEDB.version
OleDbconnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents\file.csv;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\"";
This way you can load DataTable on the start of your app and query this table just like Sql database.
notice : HDR=Yes means that first row is a header row

How to count number of rows before an ID in SQL Server

I need to count the total number of rows in a table before the certain row ID.
I have this query
select count (ClientID)
FROM [Seek].[dbo].[seekClient]
where ClientID < '12'
which works fine for the case of integer primary key, but I am not sure how to do that in case of GUID ??
Kindly help me in this case.
Thanks
Short answer, this isn't possible, see this link. Most specifically:
Globally unique identifiers are typically not human readable, and they
are not intended to be read or interpreted by humans
Long answer - what's the rest of your table structure? There may be a different way to do what you're trying to do (I would imagine it's possible using a date created field if you have one)
You should use a different column (not id) that defines what you mean by 'before'. It might be, for example, a 'DateOfCreation', 'creation_date' etc. column.

Categories