hola gente del foro....me encuentro practicando para el parcial de c# q tendre dentro de poco en la facutad y obtengo el error de variable scalar, al obtener el dato del combo(que es clave foránea) y querer darle el alta en la base de datos:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Base4
{
public partial class Altas : Form
{
public Altas()
{
InitializeComponent();
}
private SqlConnection conexion;
private SqlDataAdapter adaptador;
private DataSet datos;
private void Altas_Load(object sender, EventArgs e)
{
conexion
= new SqlConnection
("Data Source=LAARDILLA\\SQLEXPRESS;Initial Catalog=administracion;Integrated Security=True"); adaptador
= new SqlDataAdapter
(); SqlCommand alta
= new SqlCommand
("insert into empleados (dni,nombre,apellido,direccion,fecha_ingreso,salario,dpto_nro) values (@dni,@nombre,@apellido,@direccion,@fecha_ingreso,@salario,@dpto_nro)", conexion
); adaptador.InsertCommand = alta;
adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@dni", SqlDbType
.Int)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@nombre", SqlDbType
.VarChar)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@apellido", SqlDbType
.VarChar)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@direccion", SqlDbType
.VarChar)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@fecha_ingreso", SqlDbType
.VarChar)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@salario", SqlDbType
.Int)); adaptador
.InsertCommand.Parameters.Add(new SqlParameter
("@depto_nro", SqlDbType
.Int));
}
private void button1_Click(object sender, EventArgs e)
{
adaptador.InsertCommand.Parameters["@dni"].Value=textBox1.Text;
adaptador.InsertCommand.Parameters["@nombre"].Value = textBox2.Text;
adaptador.InsertCommand.Parameters["@apellido"].Value = textBox3.Text;
adaptador.InsertCommand.Parameters["@direccion"].Value = textBox4.Text;
adaptador.InsertCommand.Parameters["@fecha_ingreso"].Value = textBox5.Text;
adaptador.InsertCommand.Parameters["@salario"].Value = textBox6.Text;
adaptador.InsertCommand.Parameters["@depto_nro"].Value = comboBox1.SelectedValue;
try
{
conexion.Open();
adaptador.InsertCommand.ExecuteNonQuery();
Borrar();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conexion.Close();
}
}
public void Borrar()
{
datos.Clear();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
}
}
he revisado los tipos de datos de los campos en el sql server y coinciden con los del código....me podrían ayudar por favor a solucionar disclpen si me he expresado mal...son altas horas de la noche jejeje slds y muchas gracias... :-\