| |

VerySource

 Forgot password?
 Register
Search
View: 641|Reply: 7

DBNull question, ask "everyone"!

[Copy link]

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-8 13:00:01
| Show all posts |Read mode
string filename = "";
Ranch
if (myFile.PostedFile.ContentLength> 0)
{
  string NM = myFile.PostedFile.FileName;
  int i = NM.LastIndexOf (".");
  string extNM = NM.Substring (i);
  filename = "QC" + mid + extNM;
  UploadFile (filename);
}
else
{
  filename = attachmentLb.Text; // this
}

...

SqlParameter YeWuAttachment = new SqlParameter ("@ YeWuAttachment", SqlDbType.VarChar);
YeWuAttachment.Value = (filename == string.Empty? DBNull.Value: filename);
...

Why does the above code go wrong and how can I fix it? thank you all!

error:
CSSModify.cs (233,56): error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-5-29 08:45:01
| Show all posts
YeWuAttachment.Value = (filename == string.Empty? DBNull.Value: filename);
==>

YeWuAttachment.Value = (filename == string.Empty? DBNull.Value: (object) filename);
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-29 14:45:01
| Show all posts
? : The type of the last two values ​​of the operator must be the same
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-29 21:45:01
| Show all posts
Oh, I see!

But again:

CSSHandle.cs (1007,6): error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-5-30 12:15:01
| Show all posts
try:

if (filename == String.Empty)
{
YeWuAttachment.Value = DBNull.value;
} else
{
YeWuAttachment.Value = filename;

}
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-30 16:00:01
| Show all posts
It is possible to separate.
But it's not enough to simplify that way, I can't figure it out!

Therefore, there is only the above question!
What is the reason?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-5-30 22:30:01
| Show all posts
It is possible to separate.
But it's not enough to simplify that way, I can't figure it out!

Therefore, there is only the above question!
What is the reason?
==============
1. ? A: B. The types of A and B must be the same.
 I started to change the type of B to object, this sentence passed,

But it is inconsistent with the Varchar type of the parameter.

So it is written separately.
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-6-1 22:00:01
| Show all posts
Thank you!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list