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

Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 1, 2013, 2:47 AM EDT
Did you declare your id column as identity column or not.
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 2:48 AM EDT
Please declare id as identity column
Regards,
Iftikar
aditya immadi186 reputationPosted Aug 1, 2013, 2:49 AM EDT
Kunal VaishyaRank #990 · 2K reputationPosted Aug 1, 2013, 2:51 AM EDT
Deepak VermaRank #672 · 3.6K reputationPosted Aug 1, 2013, 2:51 AM EDT
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'.
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 2:54 AM EDT
Regards,
Iftikar
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 1, 2013, 2:54 AM EDT
aditya immadi186 reputationPosted Aug 1, 2013, 2:58 AM EDT
aditya immadi186 reputationPosted Aug 1, 2013, 2:58 AM EDT
(
@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
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 2:59 AM EDT
Regards,
Iftikar
aditya immadi186 reputationPosted Aug 1, 2013, 3:08 AM EDT
please find the attachment.and please help me how could i rectifie this
Thanks and Regards
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 1, 2013, 3:12 AM EDT
make your id column as primary key.
write click on that column and set primary key
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 3:16 AM EDT
Regards,
Iftikar
Santosh KumarRank #2,303 · 613 reputationPosted Aug 1, 2013, 3:55 AM EDT
aditya immadi186 reputationPosted Aug 1, 2013, 4:28 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 4:30 AM EDT
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 immadi186 reputationPosted Aug 1, 2013, 4:39 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 4:41 AM EDT
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
Regards,
Iftikar