Jump to content

updating a DB with nulls


10 weber
 Share

Recommended Posts

Using ASP.NET (C#):This is a part of the Page_Load() method:

if (data.Read()) //if the user has already voted{    [b]sql = "UPDATE survey SET html='" + Request.Form["lang1"];    sql += "', css='" + Request.Form["lang2"];    sql += "', js='" + Request.Form["lang3"];    sql += "', xml='" + Request.Form["lang4"];    sql += "', asp='" + Request.Form["lang5"];    sql += "', aspnet='" + Request.Form["lang6"];    sql += "', php='" + Request.Form["lang7"];    sql += "', sql='" + Request.Form["lang8"];    sql += "' WHERE userName='" + (string)Session["userName"] + "'";    data = Query(Server.MapPath("App_Data/10WEBERdatabase.mdf"), sql);[/b]}else{    row["userName"] = Session["userName"];    row["html"] = Request.Form["lang1"];    row["css"] = Request.Form["lang2"];    row["js"] = Request.Form["lang3"];    row["xml"] = Request.Form["lang4"];    row["asp"] = Request.Form["lang5"];    row["aspnet"] = Request.Form["lang6"];    row["php"] = Request.Form["lang7"];    row["sql"] = Request.Form["lang8"];    tbl.Rows.Add(row);    SqlCommandBuilder cmndBuilder = new SqlCommandBuilder(da);    da.Update(ds, "survey");}

and this is the full Query() method:

public static SqlDataReader Query(string path, string sql){    string cnctString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + path + ";Integrated Security=True;User Instance=True";    SqlConnection cnct = new SqlConnection(cnctString);    cnct.Open();    SqlCommand cmnd = new SqlCommand(sql, cnct);    SqlDataReader data = cmnd.ExecuteReader();    return data;}

I succeeded to insert nulls when creating a new row in the DB (when the user votes for the first time), but when users try to change their votes it doesn't work (it inserts space into the cell instead).note: lang1 to lang8 refer to checkboxes.

Edited by 10 weber
Link to comment
Share on other sites

If the lang1 through lang8 values are what you want to have as null, then what you're seeing happen is correct. When you do this:UPDATE table SET field=''That's not a null value, that's an empty string. Those aren't the same. If you want to update the value to null you do this:UPDATE table SET field=NULLNotice the lack of quotes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...