I am trying to insert data from dgv to sql as per below. -
TransID Amount Amount1
-
-------------------------------------------------
-
1 12000.00
-
1 12000.00
-
-
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. -
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
if ((Keys)e.KeyChar == Keys.Enter)
-
{
-
string mess = "Confirm";
-
string cap = "Wanna Save";
-
MessageBoxButtons bt = MessageBoxButtons.YesNo;
-
DialogResult result;
-
result = MessageBox.Show(mess, cap, bt);
-
if (result == DialogResult.Yes)
-
{
-
string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
-
SqlConnection con = new SqlConnection(connstr);
-
con.Open();
-
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
-
{
-
decimal? amt = null;
-
decimal? amt1 = null;
-
amt = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
-
amt1 = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());// try to casting null but fail
-
-
if (amt.HasValue || amt1.HasValue)
-
{
-
string sql = " insert into dummy(amount,amount1) values (@amount,@amount1)";
-
SqlCommand cmd = new SqlCommand(sql, con);
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = amt;
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = amt1;
-
-
if (amt == null || amt1 == null)
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
-
-
}
-
cmd.ExecuteNonQuery();
-
amt = null;
-
amt1 = null;
-
-
}
-
-
}
-
-
}
-
}
-
-
}
-
-
-
how to solve it?.
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. -
if (dataGridView1.Rows[i].Cells[0].Value!=null )
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
-
}
-
-
if (dataGridView1.Rows[i].Cells[1].Value!=null )
-
{
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
-
}
-
-
4 2011
Why not do this... -
-
if(dataGridView1.Rows[i].Cells[0].Value != null)
-
{
-
cmd.Parameters.Add("@amount",SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount",SqlDbType.Decimal).Value = DBNull.Value;
-
-
}
-
You would have to do this for both parameters.
hi
hype261
It's throw Null reference exception. I explain How.
See I have try your code like below: -
string sql = " insert into dummy(amount,amount1)values(@amount,@amount1)";
-
SqlCommand cmd = new SqlCommand(sql, con);
-
if (dataGridView1.Rows[i].Cells[0].Value!=null )
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
-
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"
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
-
}
-
cmd.ExecuteNonQuery();
-
-
When I enter the data as above said amount1 coloumn's first rows remain nullable hence it's throw NullException Error.
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. -
if (dataGridView1.Rows[i].Cells[0].Value!=null )
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value.ToString());
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount", SqlDbType.Decimal).Value = DBNull.Value;
-
}
-
-
if (dataGridView1.Rows[i].Cells[1].Value!=null )
-
{
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());
-
}
-
else
-
{
-
cmd.Parameters.Add("@amount1", SqlDbType.Decimal).Value = DBNull.Value;
-
}
-
-
hi
hype261
thanks for help dear
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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;...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |