Precious Pari Precious Pari - 1 month ago 13
ASP.NET (C#) Question

c# .net winforms data insertion error



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 crudwithlogin
{
public partial class student_registration : Form
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Android\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");

public student_registration()
{
InitializeComponent();
}

private void student_registration_Load(object sender, EventArgs e)
{
label1.Text = "You are logged in as "+((Form)this.MdiParent).Controls["label1"].Text;
}

private void label1_Click(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void label3_Click(object sender, EventArgs e)
{

}

private void label6_Click(object sender, EventArgs e)
{

}

private void label7_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into student values ('" + textBox1.Text + "','" + textBox2.Text + "''" + dateTimePicker2.Value.ToString() + "','" + textBox4.Text + "','" + comboBox2.Text + "','" + textBox6.Text + "','" + dateTimePicker1.Value.ToString() + "')";
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Data Added Successfully. ");
}
}
}





hello Experts i am creating a window form app using c#, i am trying to insert data
into database but i am receiving an error of values and columns do not match even though i am entering the right value for right attribute. i have 8 attributes (id with auto increment) for remaining 7 columns i am entering 7 values but i get error of value not match. please help me out to solve this problem i have attached the code and photos of problem

Error i received is shown in given pic.
enter image description here
database photo is here
enter image description here

Answer
('" + textBox1.Text + "','" + textBox2.Text + "''" + dateTimePicker2.Value.ToString() + "','" 
//missed , ------------------------------------^

Also use Command.Parameters in future and you will avoid problems like this ! Also you will be protected from SqlInjection. Here example how to define your params.

 SqlCommand cmd = conn.CreateCommand();
 cmd.CommandType = CommandType.Text;
 cmd.CommandText = @"insert into TableNameFromDB values (@ID, @Title)";
 cmd.Parameters.AddWithValue("@ID", yourIDValue);
 cmd.Parameters.AddWithValue("@Title", yourTitleValue);
 cmd.ExecuteNonQuery();
 conn.Close();

Also don't share one connection in your app, use multiple connections. Connection pool is your friend. Check what is using block and use it for auto disposing of your IDisposable objects( example: SqlConnection).

Comments