Im Passing a querystring to my load page (ID)
and then trying to SELECT a column of that row (Status) so its eventually only one cell.
My goal is to take some Label(not a textbox) in the page that has some text, and place it in the “Status” cell.
this is my code :
this.conn = new SqlConnection(ConnString);
conn.Open();
string idIndex = Request.QueryString[“ID”];
string sqlquery1 = “INSERT INTO Movies (Status) VALUES (@Status)”;
string sqlquery2 = “SELECT Status FROM Movies WHERE ID=” + idIndex;
cmd = new SqlCommand(sqlquery2, conn);
adapter = new SqlDataAdapter(sqlquery2, conn);
dt = new DataTable();
adapter.Fill(dt);
string data = "";
foreach (DataRow dataRow in dt.Rows)
{
data = dataRow["Status"].ToString();
}
if (data == "")
{
string username = UserNameOrGuest.Text;
cmd = new SqlCommand(sqlquery1, conn);
cmd.Parameters.AddWithValue("Status", username);
ErrorLabel.Text = "Movie rental succeeded!";
cmd.ExecuteNonQuery();
}
else
{
ErrorLabel.Text = "The movie is already rented by somebody else.";
}
}
And I get an exception :
” Cannot insert the value NULL into column ‘Ganere’, table ‘Movie.dbo.Movies’; column does not allow nulls. INSERT fails.
The statement has been terminated.”
(“Ganere” its another column from the table, that I dont want it to pass.)
THanks for the helpers!
That’s not really how a table works. When inserting a record, that record needs to meet the data integrity criteria of the table. In this particular case, it sounds like the table has a required column. (It may even have more required columns and that’s just the first one it encountered in your query.)
Your options at this point are:
nullin that column.