不要保存空值 [英] dont save null values
问题描述
保存按钮代码
将datagridview数据保存到数据库代码中,如下所示;
Save button code
saving the datagridview data''s in to database code as follows;
try
{
for (int i = 0; i < datagridView.RowCount; i++)
{
for (int j = 2; j < datagridView.ColumnCount; j++)
{
if (datagridView[j, i].Value.ToString().Trim() != "" && datagridView[j, i].Value.ToString().Trim() != null)
{
sql = "insert into Tb_SCh_TIme_Table([Sch_Date], [Session], [Course],[Faculty_Code])" + " values ('" + Convert.ToDateTime(datagridView.Rows[i].Cells[0].Value.ToString()) + "', " + int.Parse(datagridView.Rows[i].Cells[1].Value.ToString()) + ", '" + datagridView[j, i].Value.ToString() + "','" + datagridView.Columns[j].HeaderText.ToString() + "')";
try
{
GFun.Error = "";
GFun.InsertAccessData(sql);
if (GFun.Error.ToString() != "")
{
MessageBox.Show(GFun.Error.ToString(), "Error");
return;
}
GFun.OleDbCon.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString(), "Error");
return;
}
}
}
}
}
当我保存错误时显示对象引用没有设置对象的实例。
错误显示在下面的行中如下;
if(datagridView [j,i] .Value.ToString()。Trim()!=&& datagridView [j,i] .Value.ToString()。Trim()!= null)
i不想为我写的上述行保存空值和修剪(空格)值。
但是当我保存错误时显示对象引用没有设置为对象的实例。
来自我上面代码的上一行有什么错误?
如何编写代码来检查datagridview数据中的空值和修剪(空格)值不要保存在数据库中。
帮助我。
谢谢* Rgds,
Narasi man P
when i save errors shows the object reference not set an instance of object.
the error shows in the below line as follows;
if (datagridView[j, i].Value.ToString().Trim() != "" && datagridView[j, i].Value.ToString().Trim() != null)
i do not want to save null values and trim(space) values for that i written the above line.
but when i save error shows object reference not set to an instance of object.
from my above code in the previous line what is the mistake?
how can i write the code to check the null values and trim(space) values in datagridview data''s not to be save in the database.
help me.
Thanks * Rgds,
Narasiman P
推荐答案
hi
i think
datagridView [j,i]中的值。值为null。因此您无法将 null 转换为字符串。
这就是为什么错误会出现在代码
datagridView [j,i] .Value.ToString()
所以你可以使用这段代码
hi
i think
the value in datagridView[j, i].Value is null.so you can not able to convert a null into string.
thats why error come in the code
datagridView[j, i].Value.ToString()
so you may use this code
try
{
for (int i = 0; i < datagridView.RowCount; i++)
{
for (int j = 2; j < datagridView.ColumnCount; j++)
{
if(string.IsNullOrEmpty(datagridView[j, i].Value))
{
sql = "insert into Tb_SCh_TIme_Table([Sch_Date], [Session], [Course], [Faculty_Code])" + " values ('" + Convert.ToDateTime(datagridView.Rows[i].Cells[0].Value.ToString()) + "', " + int.Parse(datagridView.Rows[i].Cells[1].Value.ToString()) + ", '" + datagridView[j, i].Value.ToString() + "','" + datagridView.Columns[j].HeaderText.ToString() + "')";
try
{
GFun.Error = "";
GFun.InsertAccessData(sql);
if (GFun.Error.ToString() != "")
{
MessageBox.Show(GFun.Error.ToString(), "Error");
return;
}
GFun.OleDbCon.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString(), "Error");
return;
}
}
}
}
}
转换.ToString()
[ ^ ]是您可以使用的另一种方法避免这种错误。
Convert.ToString()
[^] is another method that you can use to avoid such errors.
这篇关于不要保存空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!