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();
}
}
}