inserting a value into the sql db, error at cmd.executenonquery

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(); Onclick Cheap Youtube Views 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.
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);       
        }
}

If you are trying to update just the password for an existing item, you’re using the wrong command. You should use SQL Update command, instead of insert command:

 using (SqlCommand cmd = new SqlCommand("update Emplo set [Password] = @pass where Name = @Name", con))
         {
             cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = CreateRandomPassword(6);
             cmd.ExecuteNonQuery();
         }

The other option that I’ve infered on your code is to do that within a single insert query:

using(SqlCommand cmd = new SqlCommand("Insert into Emplo ([Name],[Designation],[Dept],[D O B],[Sex],[Address],[Password]) values (@Name,@des,@dept,@DOB,@Sex,@Address,@pass)", 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;                
 cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = CreateRandomPassword(6);


con.Open();
                 cmd.ExecuteNonQuery();
                 Response.Write("<script>alert('Registered successfully......!')</script>");
             }

A few notices about this, even that its just an school project, it’s always good to learn something new:

  1. The “where” clause at the update query that I posted, usually should be filtered using and Id, but your actual code does not include that. So I suggest to add an Id (in case it does not have one already) to you table and use that within the where clause.
  2. It’s not a good practice to write the sql as in your code or the example. I recommend to use stored procedures. Not sure what DB is the one that you’re using, but here’s an article on how to write those for sql server. Basically, most of the db managers that use TSQL shouldn’t be that different.

Regards