C# SQL "Index was outside the bounds of the array." [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've been learning how to create a SQL base with C# and I'm stuck here with a question.
Here I am testing how to return the value of a text row to a string
Why does the compiler make an error here? I read the method list and my database has 3 more values for the 'FirstName' column. Why can I only see an index?
I appreciate your help in advance
{
static void Main(string[] args)
{
string dbName = "URI=file:testeDB.db";
using (var connection = new SqliteConnection(dbName))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT FirstName FROM Persons";
SqliteDataReader reader = command.ExecuteReader();
Console.WriteLine(reader);
if ( reader.Read() )
{
Console.WriteLine("Reading...");
Thread.Sleep(1000);
string myVar = reader.GetString(2); //Index was outside the bounds of the array."
Console.WriteLine(myVar);
}
else
{
Console.WriteLine("Dont Read");
}
}
}
}
}

This happens because the method GetString(int) as the documentation says "Gets the value of the specified column as a String." and according to the code you showed us you are just retrieving one column from the table Persons, that's why you are getting the exception, only GetString(0) will work in this case.

Related

Elements will not add to C# list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I just finished a C# totorial and thought it would be a cool idea to get the most popular type of computers using lists. I was able to get it to work exept the only the most recent element will be added to the list.
namespace computerDatabase
{
class program
{
static void Main()
{
while (true)
{
List<string> computerName = new List<string>();
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
computerName.Add(cName);
if (cName == "list")
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
else
{
computerName.Add(cName.ToLower());
}
}
}
}
}
As pointed out by Jon, you re-initializing your list for every iteration, nullifying any input added to the list. You also do not need to specify a capacity for the list in the constructor as it will grow automatically. I've corrected the relevant parts for you:
List<string> computerName = new List<string>();
while (true)
{
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
if (cName != "list")
computerName.Add(cName.ToLower());
else
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
}

using strings inside if statement throw error "Operator '||' cannot be applied to operands string and string" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator

Simple name generator in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need some help with completing this simple name "generator"
We've just started programming in high school, so I would like a simpler solution. This is all I've got for now, but the output in the textbox is System.String[]
private void Btn_gen_Click(object sender, EventArgs e)
{
textbox.Text = textbox.Text + name((int) count.Value) + Environment.NewLine;
}
private string name(int count)
{
string heslo = "";
string[] names = new string[] {"Abby", "Uther", "Thomas", "Michelle", "Abraham", "Bendy"};
string[] surnames = {"The Mighty", "The Clumsy", "The Strong", "The Lightbringer", "The Pyromancer", "The Necromancer"};
Random gnč = new Random();
for (int i = 1; i <= count; i++)
{
int nč = gnč.Next(0, names.Length);
heslo = heslo + names;
}
return heslo;
}
The problem is that you aren't appending a random name, but rather the type name System.String[], which is the formal type name of your names variable, to your string.
Change heslo = heslo + names to
heslo = heslo + names[nč];
This will take a random element out of the names array.

Autodesk guide project two "a namespace cannot directly contain members from" [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

Code unreachable when comparing types [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...

Categories