Daniel
Daniel

Reputation: 99

I want to insert null in database if i dont choose an image from imageupload control in asp.net

I have a FileUpload control and when I don't insert an image, I want to insert DBNull into the database. So far I have only errors with DBNull.Value. The table allow null for column ImageData.

Here is the code:

protected void button_sign_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        string str = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
        string Image = "~/userimage/" + str.ToString();
        string name = username_textbox.Text;
        string email = email_textbox.Text;
        string pass = password_textbox.Text;

        string CS = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("insert into Register values(@Username, @Email, @Password, @ImageData)", con);
            cmd.Parameters.AddWithValue("@Username", name);
            cmd.Parameters.AddWithValue("@Email", email);
            cmd.Parameters.AddWithValue("@Password", pass);
            cmd.Parameters.AddWithValue("@ImageData", Image);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            lblMsg.Text = "Înregistrare cu succes";
            Response.AddHeader("REFRESH", "2;URL=login.aspx");
        }
    }
    else
    {
         lblMsg.Text = "Error";
    }
}

Upvotes: 2

Views: 639

Answers (2)

Igor
Igor

Reputation: 62238

If you set the value of Image at the beginning the rest of the code could stay generic.

protected void button_sign_Click(object sender, EventArgs e)
{
    object Image;
    if (FileUpload1.HasFile==true)
    {
        string str = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
        Image = "~/userimage/" + str.ToString();
    }
    else {
        Image = System.DBNull.Value;
    }

    string name = username_textbox.Text;
    string email = email_textbox.Text;
    string pass = password_textbox.Text;

    String CS = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    using(SqlCommand cmd = new SqlCommand("insert into Register values(@Username,@Email,@Password,@ImageData)", con))
    {
        // pick the appropriate SqlDbType type for each parameter
        cmd.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar){Value = name});
        cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar){Value = email});
        cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar){Value = pass});
        cmd.Parameters.Add(new SqlParameter("@ImageData", SqlDbType.VarChar){Value = Image});

        con.Open();
        cmd.ExecuteNonQuery();
        lblMsg.Text = "Înregistrare cu succes";
        Response.AddHeader("REFRESH", "2;URL=login.aspx");
    }

Some other notes though

  • You should specify the Database types using the SqlDbType in your parameters to make sure that the values are translated correctly by the ado.net code.
  • Wrap you Command in a using block as well
  • No need to close the connection, the using block will handle that for you.
  • Do not store passwords in clear text. Instead store a salted hash of the password.

Upvotes: 1

mybirthname
mybirthname

Reputation: 18127

This should be enough

cmd.Parameters.AddWithValue("@ImageData", FileUpload1.HasFile ? Image: DbNull.Value);

Also refactor your code a little bit:

string image = "";
if (FileUpload1.HasFile==true)
{
    string str = FileUpload1.FileName;
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
    image  = "~/userimage/" + str.ToString();
}

string name = username_textbox.Text;
string email = email_textbox.Text;
string pass = password_textbox.Text;

String connString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))
{
    SqlCommand cmd = new SqlCommand("insert into Register values(@Username,@Email,@Password,@ImageData)", con);

    cmd.Parameters.AddWithValue("@Username", name);
    cmd.Parameters.AddWithValue("@Email", email);
    cmd.Parameters.AddWithValue("@Password", pass);
    cmd.Parameters.AddWithValue("@ImageData", FileUpload1.HasFile ? image: DbNull.Value);

    con.Open();
    cmd.ExecuteNonQuery();       
}

lblMsg.Text = "Înregistrare cu succes";
Response.AddHeader("REFRESH", "2;URL=login.aspx");

Don't start your variables with UpperCase letters.

Upvotes: 2

Related Questions