本文介绍了在我的代码中我没有增加以获得下一个记录。首先我从0增加到1.然后它不增加。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Drawing.Design;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;

public partial class DeptMarks : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr,dr1;
    DataSet ds;
 
   
 
    int i;
    int count;
    int j=1;
    public void Page_Load(object sender, EventArgs e)
    {
        ds = new DataSet();
        con.Open();
        cmd.Connection = con;
        dt = new DataTable();
        TextBox79.Visible = false;
        cmd.CommandText = "select [USN] from Course_reg where Semester='" + TextBox1.Text + "' and Year='" + TextBox80.Text + "'";
        SqlDataAdapter da = new SqlDataAdapter(cmd);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter(cmd);
       // DataSet ds = new DataSet();
        da.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();
        GridView1.Visible = false;
        GridView1.DataSource = ds.Tables["Course_reg"];

        if (ds.Tables[0].Rows.Count > 0)
        {
            i = 0;
            MessageBox.Show("i3" + i);
            TextBox2.Text = ds.Tables[0].Rows[i]["USN"].ToString();

        }
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter(cmd);
    
        da.Fill(ds);
        GridView1.DataSource = ds;
     
        count = GridView1.Rows.Count;
 
        GridView1.DataBind();
        GridView1.Visible = false;
        GridView1.DataSource = ds.Tables["Course_reg"];
        if (!string.IsNullOrEmpty(TextBox44.Text))
        {
            string user1 = Session["user1"].ToString();
            TextBox79.Text = user1;

            cmd.CommandText = "select * FROM Subject where Sub_Code='" + TextBox44.Text + "' and Semester='" + TextBox1.Text + "'";
            string subcode = (string)cmd.ExecuteScalar();
            if (!string.IsNullOrEmpty(subcode))
            {

                if (DropDownList2.Text == "CAT-1")
                {
                    int m1 = Convert.ToInt32(TextBox41.Text);
                    if (m1 >= 0 && m1 <= 20)
                    {
                        cmd.CommandText = "INSERT INTO Student_marks VALUES('" + TextBox1.Text +
                   "','" + TextBox80.Text +
                   "','" + TextBox2.Text +
                   "','" + TextBox44.Text +
                   "','" + TextBox41.Text +
                   "','" + "','" + "','" + "','" + "','" + TextBox79.Text + "')";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {

                        Label2.Text = "Marks should be in the range of 0-20";
                        Label2.Visible = true;

                    }
                }


                if (DropDownList2.Text == "CAT-2")
                {

                    int m2 = Convert.ToInt16(TextBox41.Text);
                    if (m2 >= 0 && m2 <= 20)
                    {
                        cmd.CommandText = "update Student_marks set CAT_2='" + TextBox41.Text + "' where USN='" + TextBox2.Text + "'and Semester='" + TextBox1.Text + "' and Subject_code='" + TextBox44.Text + "'";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {

                        Label2.Text = "Marks should be in the range of 0-20";
                        Label2.Visible = true;
                    }

                }
                if (DropDownList2.Text == "Makeup")
                {

                    int m3 = Convert.ToInt16(TextBox41.Text);
                    if (m3 >= 0 && m3 <= 20)
                    {
                        cmd.CommandText = "update Student_marks set CAT_3='" + TextBox41.Text + "' where USN='" + TextBox2.Text + "'and Semester='" + TextBox1.Text + "' and Subject_code='" + TextBox44.Text + "'";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        Label2.Text = "Marks should be in the range of 0-20";
                        Label2.Visible = true;
                    }

                }
                if (DropDownList2.Text == "CAM")
                {

                    int m4 = Convert.ToInt16(TextBox41.Text);
                    if (m4 >= 0 && m4 <= 10)
                    {
                        cmd.CommandText = "update Student_marks set CAM='" + TextBox41.Text + "' where USN='" + TextBox2.Text + "'and Semester='" + TextBox1.Text + "' and Subject_code='" + TextBox44.Text + "'";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        Label2.Text = "Marks should be in the range of 0-10";
                        Label2.Visible = true;
                    }

                }
                if (DropDownList2.Text == "ESE")
                {
                    Int32 mr1, mr2, mr3, mr4, mr5, b1 = 0, b2 = 0, cat = 0, ese = 0, total = 0;


                    int m5 = Convert.ToInt16(TextBox41.Text);
                    if (m5 >= 0 && m5 <= 100)
                    {
                        cmd.CommandText = "update Student_marks set ESE='" + TextBox41.Text + "' where USN='" + TextBox2.Text + "'and Semester='" + TextBox1.Text + "' and Subject_code='" + TextBox44.Text + "'";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        Label2.Text = "Marks should be in the range of 0-100";
                        Label2.Visible = true;
                    }
                    cmd.CommandText = "select  * from Student_marks where usn='" + TextBox2.Text + "'";

                    dr = cmd.ExecuteReader();



                    dr.Read();
                    mr1 = int.Parse(dr[4].ToString());

                    mr2 = int.Parse(dr[5].ToString());
                    mr3 = int.Parse(dr[6].ToString());
                    mr4 = int.Parse(dr[7].ToString());
                    mr5 = int.Parse(dr[8].ToString());
                    if (mr1 <= mr2 && mr1 <= mr3)
                    {
                        b1 = mr2;
                        b2 = mr3;

                    }
                    else if (mr2 <= mr1 && mr2 <= mr3)
                    {
                        b1 = mr1;
                        b2 = mr3;


                    }
                    else if (mr3 <= mr1 && mr3 <= mr2)
                    {
                        b1 = mr1;
                        b2 = mr2;

                    }
                    cat = b1 + b2 + mr4;

                    TextBox81.Text = cat.ToString();
                    ese = (mr5 / 2);
                    TextBox82.Text = ese.ToString();
                    total = cat + ese;
                    TextBox83.Text = total.ToString();
                    cmd.CommandText = "insert into Final_marks values ('" + TextBox2.Text + "','" + TextBox1.Text + "','" + TextBox44.Text + "','" + TextBox81.Text + "','" + TextBox82.Text + "','" + TextBox83.Text + "','" + "')";
                    dr.Close();
                    cmd.ExecuteNonQuery();
                    // MessageBox.Show("hui");
                    //cmd.CommandText = "select  * from Student_marks where usn='" + TextBox2.Text + "'";
                    //dr = cmd.ExecuteReader();

                }


            }
            else
            {
                Label1.Text = "This subject is not registered for this semester";
                Label1.Visible = true;
            }
        }



        if (i<count)
        {
            i++;//error

            TextBox2.Text = ds.Tables[0].Rows[i]["USN"].ToString();
        } 
        

        if (i == count)
        {
            Label1.Text = "this is the last record";
            Label1.Visible = true;
        }


        
    }

   
}

推荐答案

count = GridView1.Rows.Count;

GridView1.DataBind();



您要将 count 变量设置为之前的行数 >你已经绑定了数据记录,所以它只计算一个空行。


You are setting the count variable to the number of rows before you have bound the data records, so it will only count the one blank row.


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Cache["i"] = 0;
        }
    }







protected void Button1_OnClick(object sender, EventArgs e)
    {
        Cache["i"] = (int)Cache["i"]+ 1;
        Response.Write(Cache["i"]);
    }



这篇关于在我的代码中我没有增加以获得下一个记录。首先我从0增加到1.然后它不增加。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:17