Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i'm a newbie doing this for college project! I'm trying to insert the value into the Password column and in error at the cmd.ExecuteNonQuery(); shows that column 'Name' doesn't allow NULL characters
ERROR MSG:Cannot insert the value NULL into column 'Name', table 'jimmy.dbo.Emplo'; column does not allow nulls. INSERT fails.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=JIMMY-PC;initial Catalog=jimmy;Integrated Security=true");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using(SqlCommand cmd = new SqlCommand("Insert into Emplo ([Name],[Designation],[Dept],[D O B],[Sex],[Address]) values (@Name,@des,@dept,@DOB,@Sex,@Address)", con))
        {
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
            cmd.Parameters.Add("@des", SqlDbType.VarChar).Value = TextBox2.Text;
            cmd.Parameters.Add("@dept", SqlDbType.VarChar).Value = TextBox3.Text;
            cmd.Parameters.Add("@DOB", SqlDbType.SmallDateTime).Value = TextBox4.Text;
            cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = RadioButton1.SelectedItem.Text;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox5.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Registered successfully......!')</script>");
        }
        //string c=CreateRandomPassword();
        using (SqlCommand cmd = new SqlCommand("insert into Emplo([Password]) values (@pass)", con))
        {
            cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = CreateRandomPassword(6);
            cmd.ExecuteNonQuery();
        }
    con.Close();
    }

    public static string CreateRandomPassword(int PasswordLength)
        { 
        string allowdChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = allowdChars.Length;
        for (int j = 0; j < PasswordLength; j++)
        {
            chars[j] = allowdChars[(int)((allowdChars.Length) * randNum.NextDouble())];
        }
         return new string(chars);       
        }
}
Posted
Comments
Thanks7872 10-Apr-15 2:57am    
Which part of error is not clear to you? It is providing you with Table name,column name and the issue. What help do you need?
Jimmy-IN 10-Apr-15 3:18am    
i got it!! the problem was inserting the password into the table!! i was using new insert query for that and it was assuming the query is adding the whole new info into the table which starts with 'name' column which was not allowed to be NULL and giving error.
now i added the password query in the 1st query itself so its working fine now.
Sorry for my bad English.

1 solution

Answered only to remove from unanswered queue - solved by OP.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900