473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting Nullable and Insert From DGV

maheshwag
I am trying to insert data from dgv to sql as per below.

Expand|Select|Wrap|Line Numbers
  1. TransID      Amount                 Amount1
  2. -------------------------------------------------
  3. 1         12000.00    
  4. 1                                    12000.00              
  5.  
  6.  
The above transaction shows that the column “Amount1 “ rows “1” has Null value where as same column’s rows “2” has value. I wants to insert the multiple records into table. The above transaction is sample.

I am trying to insert it from below which is fail to insert.

Expand|Select|Wrap|Line Numbers
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.  if ((Keys)e.KeyChar == Keys.Enter) 
  4.  {
  5.   string mess = "Confirm";
  6.   string cap = "Wanna Save";
  7.   MessageBoxButtons bt = MessageBoxButtons.YesNo;
  8.   DialogResult result;
  9.   result = MessageBox.Show(mess, cap, bt);
  10.   if (result == DialogResult.Yes) 
  11.   {
  12.    string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
  13.    SqlConnection con = new SqlConnection(connstr);
  14.    con.Open();
  15.    for (int i = 0; i < dataGridView1.Rows.Count-1; i++) 
  16.    {
  17.      decimal? amt = null;
  18.      decimal? amt1 = null;
  19.      amt = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
  20.      amt1 = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());// try to casting null but fail
  21.  
  22.    if (amt.HasValue || amt1.HasValue)  
  23.    {
  24.     string sql = " insert into dummy(amount,amount1) values (@amount,@amount1)";
  25.     SqlCommand cmd = new SqlCommand(sql, con);
  26.     cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = amt;
  27.     cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = amt1;                                
  28.  
  29.    if (amt == null || amt1 == null)
  30.    {
  31.     cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
  32.     cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
  33.  
  34.    }
  35.     cmd.ExecuteNonQuery();
  36.     amt = null;
  37.     amt1 = null;
  38.  
  39.                         }
  40.  
  41.                     }
  42.  
  43.                 }
  44.             }
  45.  
  46.         }
  47.  
  48.  
  49.  
how to solve it?.
Jan 5 '11 #1

✓ answered by hype261

Yes that is expected. I mentioned this in my first post. You are going to have to test both variables to see if they are null.

Sometime like this.

Expand|Select|Wrap|Line Numbers
  1.  if (dataGridView1.Rows[i].Cells[0].Value!=null )  
  2.  { 
  3.      cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString()); 
  4. else   
  5. cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value; 
  6.  
  7.  if (dataGridView1.Rows[i].Cells[1].Value!=null )  
  8.  { 
  9.  cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());                             
  10. else   
  11. cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value; 
  12.  
  13.  

4 2011
hype261
207 100+
Why not do this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(dataGridView1.Rows[i].Cells[0].Value != null)
  3. {
  4.    cmd.Parameters.Add("@amount",SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
  5. }
  6. else
  7. {
  8.    cmd.Parameters.Add("@amount",SqlDbType.Decimal).Value = DBNull.Value;
  9.  
  10. }
  11.  
You would have to do this for both parameters.
Jan 5 '11 #2
hi
hype261

It's throw Null reference exception. I explain How.

See I have try your code like below:

Expand|Select|Wrap|Line Numbers
  1.  string sql = " insert into dummy(amount,amount1)values(@amount,@amount1)";
  2.                             SqlCommand cmd = new SqlCommand(sql, con);
  3.                             if (dataGridView1.Rows[i].Cells[0].Value!=null ) 
  4.                             {
  5.                                 cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
  6.                                 cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString()); //this line throw exception error like "Null referenceException was un handle" 
  7.                             }
  8.                             else  
  9.                             {
  10.                                 cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
  11.                                 cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
  12.                             }
  13.                             cmd.ExecuteNonQuery();
  14.  
  15.  
When I enter the data as above said amount1 coloumn's first rows remain nullable hence it's throw NullException Error.
Jan 5 '11 #3
hype261
207 100+
Yes that is expected. I mentioned this in my first post. You are going to have to test both variables to see if they are null.

Sometime like this.

Expand|Select|Wrap|Line Numbers
  1.  if (dataGridView1.Rows[i].Cells[0].Value!=null )  
  2.  { 
  3.      cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString()); 
  4. else   
  5. cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value; 
  6.  
  7.  if (dataGridView1.Rows[i].Cells[1].Value!=null )  
  8.  { 
  9.  cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());                             
  10. else   
  11. cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value; 
  12.  
  13.  
Jan 6 '11 #4
hi
hype261

thanks for help dear
Jan 6 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ron | last post by:
Mandatories: Ver 7.3.4, Redhat Linux 8.0, P4, 2GB RAM I want to add a 'nullable' foreign key to a column in a table. I have tables "company" and "project" which may be related by...
4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
7
by: Rajesh.............................. | last post by:
What is the impact of using a nullable column vs a not nullable column for partitioning a Union ALL View? I have a Union ALL View with ten underlying tables unioned based on different values for a...
10
by: Tyler Durden | last post by:
Hi, I have this annoying problem with a cast failing from a DAL that was created in 1.1 that I have included in a 2.0 app. Every stored proc in our system uses an integer parameter to return...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
9
by: MLM450 | last post by:
Is there a way to get the address of the underlying value of a nullable variable without copying the value? I tried the following, but the compiler doesn't like it. I need to do this because I am...
3
by: senfo | last post by:
I'm trying to pass a Nullable type as a parameter to the TryParse() method (both int and float types) and I'm not having any luck. I've tried casting as well as passing just the T.Value property;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.