Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Object cannot be cast from DBNull to other types

Author  Topic 

shemayb
Posting Yak Master

159 Posts

Posted - 2008-03-26 : 04:38:48
I really can't fix this exception..Could please someone help me with this?What would be the first thing that i will examine in my code?

Funnyfrog

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-26 : 07:08:39
Change line 13 to "RETURN".



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-26 : 07:13:52
I disagree, try removing lines 10 through 17 and replacing them with:

if a>b then x=d

That should do it.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-26 : 07:14:02


what you have to do is:
rdr["yourCol"] has a value of DbNull
you have to perform the check for this and either cast it to null or a value if it's not a reference type.

show us your code for better help


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2008-03-26 : 21:42:46
here's my code:

protected void btnAddTAC_Click(object sender, System.EventArgs e)
{


PatientDetail patientDetail = PatientCache;

DataTable dt = patientDetail.TACs;

int maxid = 1;

if (dt.Rows.Count > 0)
maxid = Convert.ToInt32(dt.Compute("MAX(tac_keyid)", "")) + 1;(my error is this part)

DataRow dr = dt.NewRow();
dr["keyid"] = maxid;
dr["fromdate"] = DateTime.Parse(calFromDate.Text);
dr["thrudate"] = DateTime.Parse(calThruDate.Text);
dr["tac"] = tbTAC.Text.Trim();
dr["create_date"] = DateTime.Now;

dt.Rows.Add(dr);

BindTCs();

Funnyfrog
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-27 : 06:25:26
yes and since your tac_keyid value is null the whole dt.Compute("MAX(tac_keyid)", "") is null and convert fails.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2008-04-02 : 12:48:18
What makes it null?

Funnyfrog
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-04-02 : 13:08:25
that's its value in the database.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-04-04 : 07:54:48
From your stored procedure avoid the Null values from coming to you datatable.
Something like ...where tac_keyid is not null
Go to Top of Page
   

- Advertisement -