Cannot insert the value NULL into column 'id', table
i got the Above error after i alter the table with id column...i m little bit confused can any one rectifie this..
my .cs code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace ApComapaign
{
public partial class admim_newuser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btninsert_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUploadC.PostedFile.FileName);
filename = txtnameC.Text + "_" + filename;
FileUploadC.SaveAs(Server.MapPath("~/Images-candidates/" + filename));
string constring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(constring);
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "[SP_Candidate]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("name", txtnameC.Text);
cmd.Parameters.AddWithValue("image", filename);
cmd.Parameters.AddWithValue("role", ddlCRole.Text);
cmd.Parameters.AddWithValue("party", ddlCparty.Text);
cmd.Parameters.AddWithValue("area", txtarea.Text);
cmd.Parameters.AddWithValue("district", txtdis.Text);
cmd.Parameters.AddWithValue("fb", txtfb.Text);
cmd.Parameters.AddWithValue("blog", txtblog.Text);
cmd.Parameters.AddWithValue("twitter", txttwitt.Text);
cmd.Parameters.AddWithValue("wiki", txtwiki.Text);
cmd.Parameters.AddWithValue("web", txtweb.Text);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
thanks and regards
Loading
Iftikar HussainPosted Aug 1, 2013, 8:41 AM
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
Regards,
Iftikar
aditya immadiPosted Aug 1, 2013, 8:39 AM
Iftikar HussainPosted Aug 1, 2013, 8:30 AM
protected void btninsert_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUploadC.PostedFile.FileName);
filename = txtnameC.Text + "_" + filename;
FileUploadC.SaveAs(Server.MapPath("~/Images-candidates/" + filename));
string constring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(constring);
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "[SP_Candidate]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("name", txtnameC.Text);
cmd.Parameters.AddWithValue("image", filename);
cmd.Parameters.AddWithValue("role", ddlCRole.Text);
cmd.Parameters.AddWithValue("party", ddlCparty.Text);
cmd.Parameters.AddWithValue("area", txtarea.Text);
cmd.Parameters.AddWithValue("district", txtdis.Text);
cmd.Parameters.AddWithValue("fb", txtfb.Text);
cmd.Parameters.AddWithValue("blog", txtblog.Text);
cmd.Parameters.AddWithValue("twitter", txttwitt.Text);
cmd.Parameters.AddWithValue("wiki", txtwiki.Text);
cmd.Parameters.AddWithValue("web", txtweb.Text);
cmd.ExecuteNonQuery();
conn.Close();
}
removed
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
Regards,
Iftikar
aditya immadiPosted Aug 1, 2013, 8:28 AM
Santosh KumarPosted Aug 1, 2013, 7:55 AM
Iftikar HussainPosted Aug 1, 2013, 7:16 AM
Regards,
Iftikar
Sanjeeb LenkaPosted Aug 1, 2013, 7:12 AM
make your id column as primary key.
write click on that column and set primary key
aditya immadiPosted Aug 1, 2013, 7:08 AM
please find the attachment.and please help me how could i rectifie this
Thanks and Regards
Iftikar HussainPosted Aug 1, 2013, 6:59 AM
Regards,
Iftikar
aditya immadiPosted Aug 1, 2013, 6:58 AM
(
@name nvarchar(max),
@image nvarchar(max),
@role nvarchar(max),
@party nvarchar(max),
@area nvarchar(max),
@district nvarchar(max),
@fb nvarchar(max),
@blog nvarchar(max),
@twitter nvarchar(max),
@wiki nvarchar(max),
@web nvarchar(max)
)
as
begin
insert into Candidate1 (name,image,role,party,area,district,fb,blog,twitter,wiki,web) values (@name,@image,@role,@party,@area,@district,@fb,@blog,@twitter,@wiki,@web)
end
aditya immadiPosted Aug 1, 2013, 6:58 AM
Sanjeeb LenkaPosted Aug 1, 2013, 6:54 AM
Iftikar HussainPosted Aug 1, 2013, 6:54 AM
Regards,
Iftikar
Deepak VermaPosted Aug 1, 2013, 6:51 AM
Actually, you're not passing value of column 'Id' from your code.
Value of 'Id' is required if you have not specified it as 'Identity' or 'Allow Null'.
Kunal VaishyaPosted Aug 1, 2013, 6:51 AM
aditya immadiPosted Aug 1, 2013, 6:49 AM
Iftikar HussainPosted Aug 1, 2013, 6:48 AM
Please declare id as identity column
Regards,
Iftikar
Sanjeeb LenkaPosted Aug 1, 2013, 6:47 AM
Did you declare your id column as identity column or not.