在我的代码中我没有增加以获得下一个记录。首先我从0增加到1.然后它不增加。 [英] In My Code I Is Not Incrementing To Get The Next Record .First I Is Incremented From 0 To 1 .Then It Is Not Incrementing.

查看:58
本文介绍了在我的代码中我没有增加以获得下一个记录。首先我从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.


它因为在asp.net页面回发每次点击或简单的单词,当你点击页面,它一次又一次地运行。所以,你实际上在做什么是



int i;

这里我的价值将是0

比按钮点击它会先将它增加到1并保持为1



你想要一些可以在整个页面中包含你的值的容器,如会话或缓存



这可以帮到你



Its because in asp.net page postback on every click or in simple words when ever you click on page, it runs again and again. so, what actually you are doing is

int i;
here the value of i will be 0
than on button click it will first increment it to 1 and it will remain as 1

you want some container that can contain your value across the page like session or cache

this might help you

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"]);
    }


感谢您的回复。我得到了答案
Thank you for your replies.I got the answer


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

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