Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2015, 09:05 PM
Authorized User
Points: 70, Level: 1
Points: 70, Level: 1 Points: 70, Level: 1 Points: 70, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Talking Added All Code To _RowUpdating

I added everything to this and it worked flawlessly.

OMG so many hours working through this one. I learned A LOT from this that I can continue to apply in other parts of my application!

I just needed to go back to _RowUpdating like you recommended and figure out the error of my ways.



Thank you again!

 
Old April 13th, 2015, 03:19 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yep, you can save it directly from that event. You can retrieve the bytes from the FileUpload control.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Dark-Star (June 27th, 2015)
 
Old April 19th, 2015, 09:42 PM
Authorized User
Points: 70, Level: 1
Points: 70, Level: 1 Points: 70, Level: 1 Points: 70, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Smile Exclude @parameter strings when null during Update because they don't pass as NULL

I understand that an empty string is not NULL and I can't figure out how to ignore the @parameter during an update if the control has not been filled out by the user.

I pass all the @parameters during a record updated from a asp.net web form. On button click the following code is executed.


Code:
                    // Reusing the string from above
                    // string constr = ConfigurationManager.ConnectionStrings["sqloitmaineConnectionString"].ConnectionString;

                    using (SqlConnection con = new SqlConnection(constr))
                    {

                        string query = "INSERT INTO ProblemDescription (ImageName, Image, ProblemTitleNumb, ProblemDescription, FileType) values (@ImageName, @Data, @ProblemTitleNumb, @ProblemDescription, @DocumentLocation)";

                        using (SqlCommand cmd = new SqlCommand(query))
                        {

                            cmd.Connection = con;

                            TextBox TB3 = DetailsView1.FindControl("TextBox3") as TextBox;
                            TextBox TB5 = DetailsView1.FindControl("TextBox5") as TextBox;

                            cmd.Parameters.AddWithValue("@DocumentLocation", TB3.Text);
                            cmd.Parameters.AddWithValue("@ImageName", filename);
                            cmd.Parameters.AddWithValue("@Data", bytes);
                            cmd.Parameters.AddWithValue("@ProblemTitleNumb", Id3identity);
                            cmd.Parameters.AddWithValue("@ProblemDescription", TB5.Text);

                            con.Open();

                            cmd.ExecuteNonQuery();

                            con.Close();
                            con.Dispose();

I am not sure exactly how to approach this one. Excluding one @parameter would be easy but since there are 3 that can be NULL (once passed into update they wont pass as NULL). I need them to pass as NULL if there is nothing inputted into the controls of the forms.

Any help would be appreciated.

 
Old April 20th, 2015, 01:42 AM
Authorized User
Points: 70, Level: 1
Points: 70, Level: 1 Points: 70, Level: 1 Points: 70, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Lightbulb What do you think?

This is how I solved it:

Code:
using (SqlConnection con = new SqlConnection(constr))
                    {

                        string query = "INSERT INTO ProblemDescription (ImageName, Image, ProblemTitleNumb, ProblemDescription, FileType) values (@ImageName, @Data, @ProblemTitleNumb, @ProblemDescription, @DocumentLocation)";

                        using (SqlCommand cmd = new SqlCommand(query))
                        {

                            cmd.Connection = con;

                            TextBox TB3 = DetailsView1.FindControl("TextBox3") as TextBox;
                            TextBox TB5 = DetailsView1.FindControl("TextBox5") as TextBox;

                                
                                
                                
                                cmd.Parameters.AddWithValue("@ProblemTitleNumb", Id3identity);
                                cmd.Parameters.AddWithValue("@ProblemDescription", TB5.Text);

                                    // Check for no user input in control parameter
                                if (String.IsNullOrEmpty(TB3.Text) == true)
                                {   // Assign NULL to string or not
                                    cmd.Parameters.AddWithValue("@DocumentLocation", DBNull.Value);
                                }
                                else
                                {
                                    cmd.Parameters.AddWithValue("@DocumentLocation", TB3.Text);
                                }
                                if (String.IsNullOrEmpty(filename) == true)
                                {   // Assign NULL to string or not
                                    cmd.Parameters.AddWithValue("@ImageName", DBNull.Value);
                                }
                                else
                                {
                                    cmd.Parameters.AddWithValue("@ImageName", filename);
                                }
                                if  (fs.Length.Equals(0))
                                {   // Assign NULL to byte or not (set the type of parameter to Image) When passing in a NULL value, you need to specify that SqlDbType yourself, explicitly.
                                    SqlParameter ImageParam = new SqlParameter("@Data", SqlDbType.Image);
                                    ImageParam.Value = DBNull.Value;
                                    cmd.Parameters.Add(ImageParam);
                                }
                                else
                                {
                                    cmd.Parameters.AddWithValue("@Data", bytes);
                                }

                                con.Open();

                                cmd.ExecuteNonQuery();

                                con.Close();
                                con.Dispose();

Is there a better way?
 
Old May 3rd, 2015, 10:50 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Another solution is to pass DBNull.Value when then the string is empty:

cmd.Parameters.AddWithValue("@DocumentLocation", !string.IsNullOrEmpty( TB3.Text) ? TB3.Text : DBNull.Value);

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Dark-Star (June 27th, 2015)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Fileupload Brykneval ASP.NET 4 General Discussion 1 August 16th, 2011 03:24 PM
FileUpload msomar C# 2008 aka C# 3.0 5 April 12th, 2010 04:44 PM
image and fileupload control bond007 ASP.NET 2.0 Basics 0 April 28th, 2009 12:30 AM
FileUpload and Image controls fraperz ASP.NET 2.0 Professional 1 July 28th, 2007 04:59 AM
Gridview & DetailsView Database Image Issue ayadrocks ASP.NET 2.0 Basics 1 December 27th, 2005 05:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.