There are some value contain in my database branch column for example "B01",
I am trying to set my branch column back to null value
I had tried
string.Empty
String.Empty
""
null
DBNull.value
Noted* all the method able to set my value back to "" but not my expected result, my expected result is my column back to "NULL" but not ""
if (ddlBranches.SelectedIndex > 0 && ddlLocation.SelectedItem.Text.ToUpper() == "BRANCH")
{
drUpdProb["Branch"] = ddlBranches.SelectedValue.ToString();
drUpdProb["BranchAbbr"] = ddlBranches.SelectedItem.Text;
}
else
{
drUpdProb["Branch"] = DBNull.value;
drUpdProb["BranchAbbr"] = DBNull.value;
}
Try something like the following create a parameter to assign values
cmd.Parameters.AddWithValue("#Branch", drUpdProb["Branch"] ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("#BranchAbbr", drUpdProb["BranchAbbr"] ?? (object)DBNull.Value);
Related
I am getting error on insert and update while i am providing number data in TXT_Firm_Pincode.Text and TXT_Firm_Bank_Ac_No.Text and when i am leaving this both fields empaty its working perfect can anybody help me out.
MC.Cmd_SQL = new OleDbCommand($#"Update TBL_Firm set
Firm_Owner = '{TXT_Firm_Owner.Text}',
Firm_Name = '{TXT_Firm_Name.Text}',
Firm_Address = '{TXT_Firm_Address.Text}',
Firm_City = '{TXT_Firm_City.Text}',
Firm_Pincode = #Firm_Pincode,
Firm_State= '{CB_Firm_State.Text}',
Firm_Con_Person = '{TXT_Firm_Con_Person.Text}',
Firm_Con_No = {TXT_Firm_Con_No.Text},
Firm_GSTN = '{TXT_Firm_GSTN.Text}',
Firm_Bank_Name = '{TXT_Firm_Bank_Name.Text}',
Firm_Bank_Ac_No = #Firm_Bank_Ac_No,
Firm_Bank_Branch = '{TXT_Firm_Bank_Branch.Text}',
Firm_Bank_IFSC= '{TXT_Firm_Bank_IFSC.Text}',
Firm_MailID = '{TXT_Firm_MailID.Text}'
where Firm_ID = {TXT_Firm_ID.Text}", MC.DB_Connection);
MC.Cmd_SQL.CommandType = CommandType.Text;
MC.Cmd_SQL.Parameters.AddWithValue("#Firm_Pincode" , TXT_Firm_Pincode.Text == "" ? DBNull.Value : (object)"Null");
MC.Cmd_SQL.Parameters.AddWithValue("#Firm_Bank_Ac_No", TXT_Firm_Bank_Ac_No.Text == "" ? DBNull.Value : (object)"Null");
MC.Cmd_SQL.ExecuteNonQuery();
Ternary operator condition ? operand1 : operand2 returns operand1 when condition is true and false - otherwise.
It is all right with your operand1 statement, but you need to change operand2 to put text box value in it:
MC.Cmd_SQL.Parameters.AddWithValue("#Firm_Pincode" ,
TXT_Firm_Pincode.Text == "" ? DBNull.Value : TXT_Firm_Pincode.Text);
MC.Cmd_SQL.Parameters.AddWithValue("#Firm_Bank_Ac_No",
TXT_Firm_Bank_Ac_No.Text == "" ? DBNull.Value : TXT_Firm_Bank_Ac_No.Text);
What can I do to detect when in comboBox I have null because user choose nothing, and replace it for empty string? I use for fill comboBox DataSource.
if (comboBoxTransport.SelectedItem.ToString() == null)
comboBoxMaintenance.SelectedItem = "";
this.dataGridViewOffer.DataSource = soc.FindOffer(comboBoxCountry.SelectedItem.ToString(), comboBoxAccommodation.SelectedItem.ToString(),
comboBoxTransport.SelectedItem.ToString(), comboBoxMaintenance.SelectedItem.ToString()).ToList();
I need it to correctly call method:
public List<Oferty1> FindOffer(string country, string accommodation, string transport, string maintenance) {...}
Or in how other way can I convert null to string in this case?
You can use Ternary Operator to check it wherever you need:
comboBoxTransport.SelectedItem == null ? String.Empty : comboBoxTransport.SelectedItem.ToString()
Complete Code:
if (comboBoxTransport.SelectedItem == null) //ToString can not be called if property is null
comboBoxMaintenance.SelectedItem = "";
this.dataGridViewOffer.DataSource =
soc.FindOffer(
comboBoxCountry.SelectedItem == null ? String.Empty : comboBoxCountry.SelectedItem.ToString(),
comboBoxAccommodation.SelectedItem == null ? String.Empty : comboBoxAccommodation.SelectedItem.ToString(),
comboBoxTransport.SelectedItem == null ? String.Empty : comboBoxTransport.SelectedItem.ToString(),
comboBoxMaintenance.SelectedItem == null ? String.Empty : comboBoxMaintenance.SelectedItem.ToString()
).ToList();
Is where a way to check if DateTime is null in linq expression? I've IEnumeable method where I'm returning data from database
return _requestRepository.ExecuteProcReader(
myRequest,
new SqlParameter("#userName", user)).Select(items => new Feed
{
Id = (int)items[0],
Title = items[1].ToString(),
Body = items[2].ToString(),
Link = items[3].ToString(),
PubDate = (DateTime) items[4]
});
And items[4] is a datetime which can be null in database. So, how can check something like
if(items[4] is DateTime)
{
PubDate = (DateTime) items[4]
}
One more option would be to declare PubDate as nullable inside class Feeddeclaration.
Like this:
class Feed {
public DateTime? PubDate {get;set;}
...
}
This will expose truth from database into data access layer and shift your null checks one level up.
See: Nullable types in c#
May be you can use ternary operator here.
return _requestRepository.ExecuteProcReader(myRequest,new SqlParameter("#userName", user)).Select(items => new Feed
{
Id = (int)items[0],
Title = items[1].ToString(),
Body = items[2].ToString(),
Link = items[3].ToString(),
PubDate = ((DateTime)items[4]).HasValue ? (DateTime) items[4] : DateTime.Now
//Or any date you want to use
});
You should also check for DBNull.Value when getting data from a database.
Here's what I'll do :
PubDate = (item[4] == null || item[4] == DBNull.Value ? DateTime.Now : (DateTime)item[4])
If you have multiple fields that can be NULL in database, you can put it in an extension method as :
public static object GetDBValue(this object value, object defaultValue)
{
return value == null || value == DBNull.Value ? defaultValue : value;
}
And call it with :
PubDate = (DateTime)date1.GetDBValue(DateTime.Now);
I'm trying to test for null values of two particular fields, and if they're null, replace the whitespace with the amount of 0s appropriate for that field. My code below gives no errors, but isn't doing anything and the normal values are being used.
var replace1 = "00";
var replace2 = "0";
var downpay = _Reader[26].ToString();
var preauth = _Reader[28].ToString();
if(downpay == null || downpay == " ") { downpay = replace1; }
if(preauth == null || preauth == " ") { preauth = replace2; }
writer.WriteLine("{0}{1}{2}{3}{5}{6}{7}{15}{16}{17}{18}{19}{20} 0{21}{22}{23}{24}{25}{26}{27}{28}{29}{30}{31}{32}{33}{34}{35}{36}{37} {38}{39}{40}{41}{42}{43}{44}{45}{46}{47}{48}{49} {50}", _Reader[1], _Reader[2], _Reader[3], _Reader[4], _Reader[5], _Reader[6], _Reader[7], _Reader[8], _Reader[9], _Reader[10], _Reader[11], _Reader[12], _Reader[13], _Reader[14], _Reader[15], _Reader[16], _Reader[17], _Reader[18], _Reader[19], _Reader[20], _Reader[21], _Reader[22], _Reader[23], _Reader[24], _Reader[25], downpay, _Reader[27], preauth, _Reader[29], _Reader[30], _Reader[31], _Reader[32], _Reader[33], _Reader[34], _Reader[35], _Reader[36], _Reader[37], _Reader[38], _Reader[39], _Reader[40], _Reader[41], _Reader[42], _Reader[43], _Reader[44], _Reader[45], _Reader[46], _Reader[47], _Reader[48], _Reader[49], _Reader[50], date3);
Update: Fixed the issue using the debugger. The values were \00 and \0, even though when i copy from the field in sql server it's empty.
I want to be able to update the table from c# with either a null or int. The dropdown will either have an id as the selected value or empty string, if it's an empty string I want to pass a null otherwise I want the id. I've tried this but getting an error "Input string was not in a correct format." Can anyone help? Thanks
var result = (from p in dc.Prices where p.price_ID == Id select p).FirstOrDefault();
result.no_update = String.IsNullOrEmpty((grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString()) ? System.Data.SqlTypes.SqlInt32.Null.Value : int.Parse((grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString());
dc.SubmitChanges();
Try using this code, it worked for me once
String insertedVal = (grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString()
result.no_update = String.IsNullOrWhiteSpace(insertedVal)
? (object)DBNull.Value : (object)insertedVal