Question

Hope everyone are ok, i need your help as i am trying to insert value into a MS SQL table but i am facing a problem as i have a primary column which is AdsID and its value type is Int and its identity increment.

PLEASE: when you answer can you explain in Simply and detailed as i am not professional one in c#.

the error message is: Line: 940 Error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot insert the value NULL into column 'UID', table 'Berava.dbo.ads'; column does not allow nulls. INSERT fails. The statement has been terminated.

and behind code is:

        protected void addadsbtn_Click(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection(sc);
        SqlCommand cmd = new SqlCommand();

        string sqlstatment = "INSERT INTO ads ( CateAct, State, City, AdsTit, AdsDesc, AdsPrice, Img1, img2, img3, img4, img5, Wtags) VALUES ( @CateAct, @State, @City, @AdsTit, @AdsDesc, @AdsPrice, @Img1, @img2, @img3, @img4, @img5, @Wtags)";

        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlstatment;

        //Insert the parameters first
        cmd.Parameters.AddWithValue("@CateAct", Catedrdoads.Text);
        cmd.Parameters.AddWithValue("@State", lanadsadddrdo.Text);
        cmd.Parameters.AddWithValue("@City", adsaddcitytxtbx.Text);
        cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
        cmd.Parameters.AddWithValue("@AdsDesc", Adsadddestxtbox.Text);
        cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
        cmd.Parameters.AddWithValue("@Img1", FileUploadImg1.FileName);
        cmd.Parameters.AddWithValue("@Img2", FileUploadImg2.FileName);
        cmd.Parameters.AddWithValue("@Img3", FileUploadImg3.FileName);
        cmd.Parameters.AddWithValue("@Img4", FileUploadImg4.FileName);
        cmd.Parameters.AddWithValue("@Img5", FileUploadImg5.FileName);
        cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);


        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.SelectCommand = cmd;
        ad.Fill(ds);
        Response.Redirect("User panel.aspx");

    }
Was it helpful?

Solution

So what is the problem? If you bother reading it tells you that clear - not understanding this means "read books about sql". The table has a field that is not allowed t obe NULL and your insert does not insert data, so it would be a NULL. The column is - as it states clearly in the error "UID"

How do you fis that? Well... can we start some basic logic. Table has field that has to ahve a value, error is you do not provide value. SImple logic: Provide value, so the data can be inserted.

Simple enough terms?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top