Reputation: 25
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 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 Windows forms app using C#, I am trying to insert data into a database but I am getting 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), and for the remaining 7 columns I am entering 7 values but I get error of value not match.
Please help me solve this problem. I have attached the code and screenshots of problem
Error I get is shown here:
Database schema screenshot is here
Upvotes: 0
Views: 93
Reputation: 18127
('" + 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
).
Upvotes: 1
Reputation: 726
Carefully look your insert query there is a , (coma)missing. Could you please correct your query as following:
"insert into student values ('" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker2.Value.ToString() + "','" + textBox4.Text + "','" + comboBox2.Text + "','" + textBox6.Text + "','" + dateTimePicker1.Value.ToString() + "')";
If yet not works then may be problem with database structure and corresponding values.
Upvotes: 0