如果存在值或者不起作用,则从数据库绑定值以使其工作 [英] Binding value from database to marquee its working if there are values or else not working

查看:43
本文介绍了如果存在值或者不起作用,则从数据库绑定值以使其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我的选框正在滚动欢迎来到印度

我已经编写了从数据库绑定ename到marquee的代码所以它必须滚动欢迎来到印度

如果在那个日期有ename两个都应该滚动

否则只欢迎来印度滚动

所以如果没有插入值,它的抛出错误在0位没有行

所以如果插入了值,则表示错误,第1位没有行



以下是代码

Actually my marquee is scrolling with "welcome to india"
I have written code for binding ename from database to marquee so it has to scroll with welcome to india
if there is ename in that date both should scroll
else only welcome to india should scroll
so if there is no value inserted its throwing error there is no row at position 0
so if value is inserted itsthrowing error no row at position 1

below is the code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="marquuee.aspx.cs" Inherits="marquuee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<style>
marquee { font-family:arial; color:#ffffff; font-weight:bold; font-size:54px; text-align:left; height: 50px; padding-top:25px; border: 2px white solid}
.logo { font-family:arial; color:#ffffff; font-size:45px; text-align:left;width:30px; height: 50px; padding-top:2px; border: 2px white solid;
background-color: #000099;
}
.auto-style11 {}
.auto-style12 {
background-color: #000099;
}
</style>
<form id="form1" runat="server">
<div>
<marquee style="font-family:arial; font-size:30px; width: 1282px; background-color: #00FFFF; height: 111px;"red" bgcolor="green" scrollamount="26">
<asp:Literal ID="lt1" runat="server"></asp:Literal> <font color="red">        welcome to ektha</font></marquee>

</div>
</form>
</body>
</html>

<使用System; //使用System.Collections.Generic错误地使用?
;使用System.Linq错误使用
;使用System; //错过使用?
使用System.Collections.Generic;
使用System.Linq;



using System; // missed a using?
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class marquuee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection("Data Source=HOME;Initial Catalog=Registration;Integrated Security=True");
SqlDataAdapter da1 = new SqlDataAdapter("select ImageName from imagepath where convert(varchar(20), CurrentDate, 101) = convert(varchar(20), getdate(), 101)", conn1);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "imagepath");
string s1;


s1 = " <font-size: small; font-style: normal;> ";

s1 += ds1.Tables["imagepath"].Rows[0][0].ToString();
s1 += "    ";
s1 += ds1.Tables["imagepath"].Rows[1][0].ToString();
s1 += "    ";

s1 += ds1.Tables["imagepath"].Rows[2][0].ToString();
lt1.Text = s1.ToString();
}
}

推荐答案

问题在这里 -

Problem is here-
s1 += ds1.Tables["imagepath"].Rows[0][0].ToString();
s1 += "    ";
s1 += ds1.Tables["imagepath"].Rows[1][0].ToString();
s1 += "    ";
 
s1 += ds1.Tables["imagepath"].Rows[2][0].ToString();



您需要在访问数据之前对数据进行检查。



试试这个 -


You need to put checks on data existance before accesing them.

Try this-

if(ds1.Tables["imagepath"].Rows.Count>0)
{
  s1 += ds1.Tables["imagepath"].Rows[0][0].ToString();
}
s1 += "    ";
if(ds1.Tables["imagepath"].Rows.Count>1)
{
  s1 += ds1.Tables["imagepath"].Rows[1][0].ToString();
}
s1 += "    ";
if(ds1.Tables["imagepath"].Rows.Count>2)
{
  s1 += ds1.Tables["imagepath"].Rows[2][0].ToString();
}





希望,它有帮助:)



Hope, it helps :)


这篇关于如果存在值或者不起作用,则从数据库绑定值以使其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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