Hi All,
I am facing error while updating the Database from a GridView:
Cannot insert the value NULL into column 'UserId', table 'QuizSoft.dbo.EXAMCategory'; column does not allow nulls. UPDATE fails. The statement has been terminated.
But after refreshing, the page, the where updated. Why do i get this error message each time. the following is my settings:
The error comes from UserId which is not null from the tables
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="LkBtnEdit" runat="server" CommandName="Edit" CssClass="btn btn-primary">Edit</asp:LinkButton>
<asp:LinkButton ID="LkBtnDelete" runat="server" CommandName="Delete" CssClass="btn btn-danger" OnClientClick="return confirm('Are you sure you want to delete?')">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LkBtnUpdate" runat="server" CommandName="Update" CssClass="btn btn-success">Update</asp:LinkButton>
<asp:LinkButton ID="LkBtnCancel" runat="server" CommandName="Cancel" CssClass="btn btn-secondary">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
protected void GridView1_UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
if (GridView1.Rows.Count > 0)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int categoryId = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tCategoryName = (TextBox)row.FindControl("txtCategoryName");
FileUpload fFileUpload1 = (FileUpload)row.FindControl("FileUpload1");
DropDownList ddCategoryIsActive = (DropDownList)row.FindControl("dropDownCategoryIsActive");
TextBox tUserId = (TextBox)row.FindControl("txtUserId");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionQuiz"].ToString());
SqlCommand cmd = new SqlCommand();
string userIdValue = GridView1.Rows[e.RowIndex].Cells[4].Text;
string path = "CategoryImages/";
if (fFileUpload1.HasFile)
{
path += fFileUpload1.FileName;
//save image in folder
fFileUpload1.SaveAs(MapPath(path));
}
else
{
// use previous user image if new image is not changed
Image img = (Image)GridView1.Rows[e.RowIndex].FindControl("imgCatPicPath");
path = img.ImageUrl;
}
if (!string.IsNullOrEmpty(tUserId.Text))
{
try
{
con.Open();
cmd.CommandText = "exam_UpdateCategory";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = categoryId;
cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 50).Value = tCategoryName.Text.Trim();
cmd.Parameters.Add("@CategoryBilde", SqlDbType.NVarChar, 500).Value = path.Trim();
cmd.Parameters.Add("@CategoryIsActive", SqlDbType.Int).Value = int.Parse(ddCategoryIsActive.SelectedValue);
cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = int.Parse(tUserId.Text.Trim());
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
cmd.Dispose();
GridView1.EditIndex = -1;
GridView1.DataBind();
lblResult.Text = "Category Updated Successfully";
// ImageData();
}
catch (SqlException ee)
{
throw ee;
}
}
else
{
lblResult.Text = "userid value was null";
}
}
}
How to solve this issue?
Many thanks in advance