如何使用存储过程的三个轮胎插入空日期时间和整数值 [英] How I Insert Null Datetime And Integer Value Using Three Tire With Store Procedure

查看:88
本文介绍了如何使用存储过程的三个轮胎插入空日期时间和整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。我的BLL类文件代码



公共字符串名称{get;组; } $ / $
public int rollno {get;组; } $ / $
public DateTime joindate {get;组; }



2. Dll类文件



public void insertdata(BLL bl)

{

SqlCommand cmd = new SqlCommand(insertt2,con);

cmd.CommandType = CommandType.StoredProcedure;

cmd .Parameters.AddWithValue(@ name,bl.name);

cmd.Parameters.AddWithValue(@ rollno,bl.rollno);

cmd.Parameters.AddWithValue(@ joinDate,bl.joindate);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

} b / b
3.演示文件(aspx.cs)



protected void btnsubmit_Click(object sender,EventArgs e)

{

BLL bll = new BLL();

Dll dll = new Dll();



bll.name = txtname.Text;

if(txtrollno.Text!=)

{

bll.rollno = Int32.Parse(txtrollno.Text);

}

else if(txtrollno.Text ==)

{

bll.rollno = Convert.ToInt32 (DBNull.Value);

}



if(txtjoindate.Text!=)

{

bll.joindate = Convert.ToDateTime(txtjoindate.Text);

}

else if(txtjoindate.Text ==)

{

bll.joindate = Convert .ToDateTime(DBNull.Value);

}

dll.insertdata(bll);



}



这里我发现以下错误如果我转换为toint32或datetime



对象无法从DBNull转换为其他类型。



这里我要提交Null值(rollno和joindate)

请帮助我。

解决方案

Quote:

DBNull - 表示不存在的值



这就是msdn Says.DBNull无法转换成任何东西。当没有Value然后如何转换它?



如果你想在表中插入Null值,那么不要在Query中传递它


你需要做的是去掉你BLL中的属性是可以为空的类型。



  public  < span class =code-keyword> string  name { get ;  set ; } 
public int ? rollno { get ; set ; }
public DateTime? joindate { get ; set ; }





您还需要确保sql列允许空值。


1. My BLL Class File Code

public string name { get; set; }
public int rollno { get; set; }
public DateTime joindate { get; set; }

2. Dll Class File

public void insertdata(BLL bl)
{
SqlCommand cmd = new SqlCommand("insertt2", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", bl.name);
cmd.Parameters.AddWithValue("@rollno", bl.rollno);
cmd.Parameters.AddWithValue("@joinDate", bl.joindate);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

3. Presentation File (aspx.cs)

protected void btnsubmit_Click(object sender, EventArgs e)
{
BLL bll = new BLL();
Dll dll = new Dll();

bll.name = txtname.Text;
if (txtrollno.Text != "")
{
bll.rollno = Int32.Parse(txtrollno.Text);
}
else if (txtrollno.Text == "")
{
bll.rollno = Convert.ToInt32(DBNull.Value);
}

if (txtjoindate.Text != "")
{
bll.joindate = Convert.ToDateTime(txtjoindate.Text);
}
else if (txtjoindate.Text == "")
{
bll.joindate = Convert.ToDateTime(DBNull.Value);
}
dll.insertdata(bll);

}

here i found following error if i convert toint32 or datetime

"Object cannot be cast from DBNull to other types."

here i want to submit Null value(rollno and joindate)
Please Help me.

解决方案

Quote:

DBNull-Represents a nonexistent value


This is what msdn Says.DBNull cannot be converted into anything.When there is no Value then how it can be converted?

If you want to insert Null value in table ,then do not pass it in Query


What you need to do is to declare the properties in you BLL as nullable type.

public string name { get; set; }
public int? rollno { get; set; }
public DateTime? joindate { get; set; }



You also need to make sure that you sql columns allow for nulls.


这篇关于如何使用存储过程的三个轮胎插入空日期时间和整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆