0

Hello can someone please help me? when i am trying to get a string value from a table on my sql server database table it says that i can not convert string to int, but i dont want to convert the value to int. as the values in the table are "Admin" and "General User".

by the way i am using sql server 2014

the variable that i use to capture the string is cap and i declared it as a string.

and when i write the code.

conn.Open();

        string query_inicio = "select * from usuarios where USU_Usuario = '" + txtusuario.Text + "' AND USU_Contra ='" + txtcontra.Text + "'";
        SqlCommand exe_query_inicio = new SqlCommand(query_inicio, conn);

        SqlDataReader leer_exe;


        try
        {



            leer_exe = exe_query_inicio.ExecuteReader();

            if (leer_exe.Read())
            {
                cap = leer_exe.GetString("Admin");

                MessageBox.Show("CONECTADO");
                if (cap.Equals("Admin"))
                {
                    Reporte_Detallado IB = new Reporte_Detallado();
                    IB.Show(this);

                    this.Hide();
                }
            }
            else if (leer_exe.Read() == false)
            {

              MessageBox.Show("Inicio Fallido, Verifique Conexion");

       }

it underlines the cap = leer_exe.GetString("Admin"); and says that i can't convert string to int.

i have the same code using a mysql datbase and it works. now i am trying to do it with microsoft sql server. so the only thing i changed from the mysql version was instead of mysqlconection and those database code lines to sqlconnection and the other variations.

here is my complete code. i hope someone can help me.

by the way i am coding in c#.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClimateReports
{
public partial class Login : Form
{
   SqlConnection  conn = ConexionBD.ObtenerConexion();
    string cap;

    public Login()
    {
        InitializeComponent();
    }

    private void btncancelar_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

    private void btniniciar_Click(object sender, EventArgs e)
    {
        conn.Open();
        string query_inicio = "select * from usuarios where USU_Usuario = '" + txtusuario.Text + "' AND USU_Contra ='" + txtcontra.Text + "'";
        SqlCommand exe_query_inicio = new SqlCommand(query_inicio, conn);
        SqlDataReader leer_exe;
        try
        {
            leer_exe = exe_query_inicio.ExecuteReader();               
            if (leer_exe.Read())
            {
                cap = leer_exe.GetSqlString("Admin");

                MessageBox.Show("CONECTADO");
                if (cap.Equals("Admin"))
                {
                    Reporte_Detallado IB = new Reporte_Detallado();
                    IB.Show(this);

                    this.Hide();
                }
            }
            else if (leer_exe.Read() == false)
            {

              MessageBox.Show("Inicio Fallido, Verifique Conexion");
       }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        conn.Close();
    }
}
}
2
  • ARGH! Use parameters. Never blindly add user data to your query string. Don't you know about Bobby Tables? Commented Mar 20, 2017 at 23:28
  • not really, but i will look into it. at university that a go here in mexico they dont teach alot of things. but i will look it up. and about the parameters i do know but i was a bit lazy lol Commented Mar 20, 2017 at 23:31

1 Answer 1

4

The problem is this line:

cap = leer_exe.GetString("Admin");

GetString takes an int32 as its argument.

If you want to access a column by its name, you should use Item instead:

cap = leer_exe["Admin"] as string;

Or, if you know what column "Admin" is, you can replace it with its position index. If it's the 4th column in the resultset, you'd use index 3 (because it's base 0):

cap = leer_exe.GetString(3);
Sign up to request clarification or add additional context in comments.

1 Comment

fisrt used the cap = leer_exe["Admin"] as string; and it didnt work but the cap = leer_exe.GetString(3); work perfectly. only that for me it was 6 instead of 3. thank you very much Jonh. i really apreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.