Hack Forums
[C#] Console app if ReadLine left blank, do not update record sql - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Console app if ReadLine left blank, do not update record sql (/showthread.php?tid=5720640)



Console app if ReadLine left blank, do not update record sql - zetza - Sep 19, 2017

Making a console app, where I need to edit some user credentials. But sometimes I need to edit just one thing, and I want to be able to just press ENTER, when it comes to the other stuff that doesn't need updating.

So it gets the Id to edit from the int editId, and perhaps I want to just edit the address. So I want to be able to just press ENTER on the rest of the records, and not update them.

PHP Code

<?php 
private void EditAccount()
        {
            
Console.WriteLine("\nEnter account number to edit:");
            
int editId = int.Parse(Console.ReadLine());
            
Console.WriteLine("Edit account holder:");
            
string editHolder = Console.ReadLine();
            
Console.WriteLine("Edit address of account holder:");
            
string editAddress = Console.ReadLine();
            
Console.WriteLine("Edit zipcode of account holder:");
            
string editZip = Console.ReadLine();
            
Console.WriteLine("Edit country of account holder:");
            
string editCountry = Console.ReadLine();
            
Console.WriteLine("Edit phone of account holder:");
            
int editPhone = int.Parse(Console.ReadLine());
            
Console.WriteLine("Edit account balance:");
            
int editBalance = int.Parse(Console.ReadLine());


            try
            {
                
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString);
                {
                    
con.Open();
                    
string updateQuery = "UPDATE BankSystem SET accHolder = '" + editHolder + 
"', accAddress = '" + editAddress + 
"', accZip = '" + editZip + 
"', accCountry = '" + editCountry + 
"', accPhone = '" + editPhone + 
"', accBalance = '" + editBalance + 
"' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accHolder", editHolder);
                        
cmd.Parameters.AddWithValue("accAddress", editAddress);
                        
cmd.Parameters.AddWithValue("accZip", editZip);
                        
cmd.Parameters.AddWithValue("accCountry", editCountry);
                        
cmd.Parameters.AddWithValue("accPhone", editPhone);
                        
cmd.Parameters.AddWithValue("accBalance", editBalance);
                        
int rows = cmd.ExecuteNonQuery();
                        
con.Close();
                        
Console.WriteLine("Account was updated.");
                    }
                }
            }
            catch (
SqlException ex)
            {
                
Console.WriteLine("Something went wrong.", ex.ToString());
            }
public 
void editAccount()
        {
            
EditAccount();
        }
        } 



* - SIM-3387369 - Sep 19, 2017

Just use !String.IsNullOrEmpty to check if the user inputted something. If they did, then add to the query string.

Reference: https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx


* - zetza - Sep 19, 2017

(Sep 19, 2017 02:10 AM)Ziqolu Wrote:  Just use !String.IsNullOrEmpty to check if the user inputted something. If they did, then add to the query string.

Reference: https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx
I tried, but I will see if the reference code will do for me, thanks.


* - SIM-3387369 - Sep 19, 2017

(Sep 19, 2017 02:29 AM)zetza Wrote:  I tried, but I will see if the reference code will do for me, thanks.

That reference was just for String.IsNullOrEmpty
Is there problems you're having when using it?

Here is an example of what I was thinking:
PHP Code

<?php 
string updateQuery 
= "UPDATE BankSystem SET";
if(!
String.IsNullOrEmpty(accHolder)){
    
updateQuery = updateQuery + " accHolder = '" + accHolder + "',";
}
updateQuery = updateQuery.TrimEnd(',') + " WHERE accId = " + editId; 

If you don't have experience with TrimEnd: https://msdn.microsoft.com/en-us/library/system.string.trimend(v=vs.110).aspx


* - zetza - Sep 19, 2017

(Sep 19, 2017 02:37 AM)Ziqolu Wrote:  -snip-

I'm having issues with checking the integers' IsNullOrEmpty


* - SIM-3387369 - Sep 19, 2017

(Sep 19, 2017 03:51 AM)zetza Wrote:  I'm having issues with checking the integers' IsNullOrEmpty

Have you tried making the ints string values instead?

Sorry for the late edit, I just realized what I previously said was incorrect. Try this method though :v


* - zetza - Sep 19, 2017

(Sep 19, 2017 03:56 AM)Ziqolu Wrote:  -snip-

I've written it a little differently now, and it keeps giving me an error when I get to editPhone where it says "System.FormatException - The input string wasn't in the right format"

PHP Code

<?php 
private void EditAccount()
        {
            
Console.WriteLine("\nEnter account number to edit:");
            
int editId = int.Parse(Console.ReadLine());
            
Console.WriteLine("Edit account holder:");
            
string editHolder = Console.ReadLine();
            
Console.WriteLine("Edit address of account holder:");
            
string editAddress = Console.ReadLine();
            
Console.WriteLine("Edit zipcode of account holder:");
            
string editZip = Console.ReadLine();
            
Console.WriteLine("Edit country of account holder:");
            
string editCountry = Console.ReadLine();
            
Console.WriteLine("Edit phone of account holder:");
            
int? editPhone = int.Parse(Console.ReadLine());
            
Console.WriteLine("Edit account balance:");
            
int? editBalance = int.Parse(Console.ReadLine());
            try
            {
                
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString);
                
con.Open();
                if (!
string.IsNullOrEmpty(editHolder))
                {
                    
string updateQuery = "UPDATE BankSystem SET accHolder = '" + editHolder + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accHolder", editHolder);
                        
cmd.ExecuteNonQuery();
                    }
                }
                if (!
string.IsNullOrEmpty(editAddress))
                {
                    
string updateQuery = "UPDATE BankSystem SET accAddress = '" + editAddress + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accAddress", editAddress);
                        
cmd.ExecuteNonQuery();
                    }
                }
                if (!
string.IsNullOrEmpty(editZip))
                {
                    
string updateQuery = "UPDATE BankSystem SET accZip = '" + editZip + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("acc>ip", editZip);
                        
cmd.ExecuteNonQuery();
                    }
                }
                if (!
string.IsNullOrEmpty(editCountry))
                {
                    
string updateQuery = "UPDATE BankSystem SET accCountry = '" + editCountry + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accCountry", editCountry);
                        
cmd.ExecuteNonQuery();
                    }
                }
                if (
string.IsNullOrEmpty(editPhone.ToString()))
                {
                    
string updateQuery = "UPDATE BankSystem SET accPhone = '" + editPhone + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accPhone", editPhone);
                        
cmd.ExecuteNonQuery();
                    }
                }
                if (
string.IsNullOrEmpty(editHolder.ToString()))
                {
                    
string updateQuery = "UPDATE BankSystem SET accBalance = '" + editBalance + "' WHERE accId = " + editId;
                    
using (SqlCommand cmd = new SqlCommand(updateQuery, con))
                    {
                        
cmd.Parameters.AddWithValue("accBalance", editBalance);
                        
cmd.ExecuteNonQuery();
                    }
                }
                
con.Close();
                
Console.WriteLine("Account was updated.");
            }
            catch (
SqlException ex)
            {
                
Console.WriteLine("Something went wrong.", ex.ToString());
            }
        } 



* - SIM-3387369 - Sep 19, 2017

(Sep 19, 2017 04:07 AM)zetza Wrote:  -snip-

My other reply was inaccurate at first and I edited it after you started replying unfortunately. Please try using string values instead of int values; that should fix the problem I think. As far as I can tell, int values aren't necessary in your script and it'd be easier to just leave the values as strings. If you're wanting to check if a string is an int though, then you're going to have to use TryParse. Remove the silly ToString's, I haven't slept and that wasn't a good idea, my bad.


* - zetza - Sep 19, 2017

Seems to be working after making the ints to string, but not sure if this is the right way of doing it properly.
Well what the hell, it works.

If it's stupid and it works, it's not stupid :P