Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old April 12th, 2015, 10:05 PM
Authorized User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 14
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!

Reply With Quote
  #12 (permalink)  
Old April 13th, 2015, 04:19 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,957
Thanks: 79
Thanked 1,556 Times in 1,533 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Dark-Star (June 27th, 2015)
  #13 (permalink)  
Old April 19th, 2015, 10:42 PM
Authorized User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 14
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.

Reply With Quote
  #14 (permalink)  
Old April 20th, 2015, 02:42 AM
Authorized User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 14
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?
Reply With Quote
  #15 (permalink)  
Old May 3rd, 2015, 11:50 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,957
Thanks: 79
Thanked 1,556 Times in 1,533 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Dark-Star (June 27th, 2015)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 03:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.