Code Giving Error is Given Below, and Error is
1) Make Sure your method arguments are in right format
2) When converting a string to DateTime, prase the string to take the date before putting each variable into the DateTime Object
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cs = new SqlConnection("Data Source = raaj-pc; Initial Catalog = BookSellerNescom; Integrated Security = True");
/* cs.Open();
MessageBox.Show(cs.State.ToString());
cs.Close(); */
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("INSERT INTO tblBooks VALUES(@Book_ID, @Title, @ISBN_No, @Author, @Publisher, @Date_Published, @Available, @Price)", cs);
da.InsertCommand.Parameters.Add("@Book_ID", SqlDbType.Int).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar).Value = textBox2.Text;
da.InsertCommand.Parameters.Add("@ISBN_No", SqlDbType.NChar).Value = textBox3.Text;
da.InsertCommand.Parameters.Add("@Author", SqlDbType.VarChar).Value = textBox4.Text;
da.InsertCommand.Parameters.Add("@Publisher", SqlDbType.VarChar).Value = textBox5.Text;
da.InsertCommand.Parameters.Add("@Date_Published", SqlDbType.Date).Value = textBox6.Text;
da.InsertCommand.Parameters.Add("@Available", SqlDbType.Bit).Value = textBox7.Text;
da.InsertCommand.Parameters.Add("@Price", SqlDbType.Money).Value = textBox8.Text;
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
}
}