使文本框默认值等于0

时间:2016-06-11 03:19:29

标签: c# asp.net text default-value

我设置了textbox = 0

import pygame, random, sys
from time import sleep
from pygame.locals import *
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
NAVYBLUE = (0,0,45)
GREY = (128,128,128)
screen_width = 900
screen_height = 650

pygame.init()
OpenScreen = pygame.display.set_mode([screen_width, screen_height])


screen = pygame.display.set_mode([screen_width, screen_height])
class Block(pygame.sprite.Sprite):
    def __init__(self, image):
        aliens = pygame.image.load('aliens.png')
        aliens = pygame.transform.scale(aliens, (20,20))
        super(Block, self).__init__()

        self.image = aliens

        self.rect = self.image.get_rect()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.mouse.set_visible(0)
        ship = pygame.image.load('spaceship.png')
        ship = pygame.transform.scale(ship, (20,30))
        super(Player, self).__init__()

        self.image = ship
        self.rect = self.image.get_rect()
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.x = pos[0]

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        bullets = pygame.image.load('missile.png')
        bullets = pygame.transform.scale(bullets, (15,25))
        super(Bullet, self).__init__()
        self.image = bullets
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.y -= 3

all_sprites_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()


for i in range(17):
    block = Block(Block)
    block.rect.x = (40 * i) + 110
    block.rect.y = 50
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(17):
    block = Block(Block)
    block.rect.x = (40 * i) + 110
    block.rect.y = 75
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(17):
    block = Block(Block)
    block.rect.x = (40 * i) + 110
    block.rect.y = 100
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(17):
    block = Block(Block)
    block.rect.x = (40 * i) + 110
    block.rect.y = 125
    block_list.add(block)
    all_sprites_list.add(block)




player = Player()
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
score = 0
time = 2000
player.rect.y = 615
font = pygame.font.SysFont(None, 30)



while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_ESCAPE]:
            done = True

        elif pressed[pygame.K_SPACE]:
            while not done:

                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        done = True

                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        bullet = Bullet()
                        bullet.rect.x = player.rect.x + 3.5
                        bullet.rect.y = player.rect.y
                        all_sprites_list.add(bullet)
                        bullet_list.add(bullet)



                all_sprites_list.update()

                for bullet in bullet_list:
                    block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
                    for block in block_hit_list:

                        bullet_list.remove(bullet)
                        all_sprites_list.remove(bullet)
                        score += 1
                    if bullet.rect.y < -10:
                        bullet_list.remove(bullet)
                        all_sprites_list.remove(bullet)

                screen.fill(NAVYBLUE)
                scoredisplay = font.render("Score {0}".format(score), 1, (WHITE))
                screen.blit(scoredisplay, (5, 10))
                all_sprites_list.draw(screen)
                timedisplay = font.render("Time {0}".format(time), 1, (WHITE))
                screen.blit(timedisplay, (5, 30))
                pygame.display.flip()

                time -= 1
                if time == 0:
                    done = True
                    screen.fill(WHITE)
                    font= pygame.font.SysFont("impact", 36)
                    display = font.render("YOU RAN OUT OF TIME!! :( Your final score was {}".format(score), 1, (BLACK))
                    screen.blit(display, (100,200))
                    pygame.display.flip()
                    pygame.time.wait(1500)

                if len(block_list)  == 0:
                    done == True
                    screen.fill(WHITE)
                    font = pygame.font.SysFont("impact", 55)
                    display = font.render('You Won!',True ,(BLACK))
                    screen.blit(display,(350,200))
                    pygame.display.flip()
                    pygame.time.wait(1500)
                clock.tick(60)

pygame.quit()

我的代码背后是:

<p>Credit Balance: </p><asp:TextBox ID="txtCredit" runat="server" style="width:70px" Text = "0"></asp:TextBox>

我想要发生的是,如果它有价值,它会显示它。如果没有,默认值等于0.但我收到错误

scn.Open();

SqlCommand cmd = new SqlCommand("SELECT CreditRequest FROM UserData WHERE Username=@Username", scn);

cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];

object value = cmd.ExecuteScalar();

if (value != null)
{
    txtCredit.Text = Convert.ToDecimal(value).ToString("#,##0.00");
}

3 个答案:

答案 0 :(得分:1)

我怀疑,值为DBNull,您可以使用DBNull方法防范Convert.IsDBNull

if (!Convert.IsDBNull(value))
{
    txtCredit.Text = Convert.ToDecimal(value).ToString("#,##0.00");
}

答案 1 :(得分:0)

不使用null,而是使用DBNull。

if(value != DBNull)
{
// update textbox
}

答案 2 :(得分:0)

你应该考虑的事情:

  • 不是设置text属性,而是发送了VALUE。
  • 转换前验证您的值。您正在将它与null进行比较,但它是一个DBNull。除了Null验证之外,请检查此信息。
祝你好运!