Getting error in visual studio while reading from mysql database [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In the following code I try to fill the combobox
while (myReader.Read())
{
string sName = myReader.GetString("profesor"); <--- here is the error
cb_1najprof.Items.Add(sName);
cb_2najprof.Items.Add(sName);
cb_3najprof.Items.Add(sName);
cb_1najsprof.Items.Add(sName);
cb_2najsprof.Items.Add(sName);
cb_3najsprof.Items.Add(sName);
}
When the code above is run I get the error cannot convert string to int on the indicated line.

You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
How to get data by SqlDataReader.GetValue by column name

GetString takes an integer as an argument, not a string. You have called it with "profesor". Try calling it with eg 1 instead. This integer corresponds to the column you want to read from.

That's because you can't pass a string to the .GetString() method [documentation] You have to pass an int. The int (zero-based) is the column number you want to read.
Yes I know my documentation link is for a SqlDataReader, but the same concept applies for a MySqlDataReader

Related

Convert List<String> to string C# - asp.net - sql server - [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new to this and I am asking for help to convert a string type data, I made an sql connection where I returned a name, address and phone number found in another table
List string eTelefono
while _Reader.Read
Builder
(If you see something in spanish is beacause im from Uruguay)
This the error
error System.String to System.Collections.Generic.List 1 System.String
Does anyone know how to convert List <string> to string?
Thanks!
If you need more captures just ask
The problem is that you're reading in a single string when you do: _Reader["telefono"], but you're trying to cast that to a List<string>.
Instead, you can just take that single string and add it as a single item to a list:
eTelefono = new List<string> { (string) _Reader["telefono"] };
The other option, of course, would be to define the eTelefono field as a string instead of a List<string>, since there's only one value associated with a record in the database anyway.

How to evalute string of IP address in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Now I have a series of addresses with the format "\10.4.1.2\Camera_save". They are in total 18, the only number varies is the 3rd number in the IP address (e.g: 10.4.2.2, 10.4.3.2.....10.4.18.2)
What I need to do is extract the 3rd number from an address, any idea?
P.s The addresses are in an array with 18 elements. I have another array trams[] for storing the extracted 18 numbers.
Ok what I tried is to use string.replace(), like
directory[i].Fullname.Replace('\\10.4.','').Replace('.2\Camera_save','');
The error says the arguments for Replace() are too long and null string is not OK. Since I got this method from another post where the replace() was used exactly like this. So any explanation?
String[] ip = { #"\10.4.1.2\Camera_save", #"\10.4.5.2\Camera_save" };
String[] result = ip.Select(x => x.Split('.')[2]).ToArray();
Parse the string with Uri.Parse, pick the first segment, then parse it like IP address with IPAddress.Parse and then convert them to 4-byte array using the .GetAddressBytes() and pick your third byte.
you can Split() by .:
string number= ip.Split('.')[2];

C# subtraction of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am sorry for that bad Title, but basicaly my problem is really simple. I got 1 string which is basic alphabet and the second string which is gonna be part of the alphabet (8 characters) which user will fill up by himself. If 2 characters are the same, they will get removed and then rest of characters will be in the TextBox3. could someone pls help me ?
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = TextBox2.Text;
I assume you want to check the existence of substring and remove from the parent string, Try this
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = textBox2.Text;
if (alphabet.ToLowerInvariant().Contains(special))
{
textBox3.Text = alphabet.Replace(special, "");
}

How to String Format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've an array of strings below and wanted to format like the followings, what is the best way of doing that? Thanks in advance.
line[0] = "This is line one two tree";
line[1] = "This is Abc Cde";
line[2] = "This is cjdj";
I want it to format to display like this
This is line one two tree
This is Abc Cde..........
This is cjdj.............
You can use the string.PadRight() method, coupled with determining which of the array of strings is the widest:
var width = line.Max(l => l.Length);
foreach (var l in line)
Console.WriteLine(l.PadRight(width, '.'));
You could use:
var output = string.Join(Environment.NewLine, line.Select(l => l.PadRight(line[0].Length, '-').ToArray());
Use string.PadRight to pad each string, up to the specified length, with instances of a specified character.
Use PadRight:
http://msdn.microsoft.com/en-us/library/system.string.padright.aspx
e.g.
int len = line[0].Length;
Console.WriteLine(line[0]);
Console.WriteLine(line[1].PadRight(len,"."));
Console.WriteLine(line[2].PadRight(len,"."));

Database field Enum to C# List [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In my database I have a Enum field. I want to put those Enum attributes in a List in C#.
I searched but I couldn't find the answer. Is this possible?
I use a MySqlDatabase. If I want to get the rows from the database I use:
using (var uow = new UnitOfWorkScope<TrackerEntities>(UnitOfWorkScopePurpose.Reading))
In my application I use the Entity Framework
if you loading data in a data reader, however this is sql-server way. But point here is to convert string into enum
var list = new List<YourEnumType>();
var field= reader["DBFieldName"] != DBNull.Value ? reader["DBFieldName"].ToString()
: "";
var myField=(YourEnumType) Enum.Parse(typeof(YourEnumType ), field);
list.Add(myField);
Also have a look at The ENUM Type
You'll need to make a query against the schema table to list the enum values.
SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TABLE_NAME AND
COLUMN_NAME = #COLUMN_NAME

Categories