Click here to Skip to main content
15,887,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
        }


        
    }

   
}
Posted
Updated 18-Apr-15 20:51pm
v2
Comments
Abhinav S 19-Apr-15 3:10am    
Can you be a little more specific? What is not incrementing?
Member 11604789 19-Apr-15 4:50am    
In protected void Button2_Click(object sender, EventArgs e){} i intialiazed i to 0 to get the first record when i click the button 2.Then in protected void Button3_Click(object sender, EventArgs e){} i write the i++.But i in button3_click increamenting only once .so it becomes 1.then again i click the button 3.but i is not increamenting.I have four records in table.But it is displaying only 2 records.

C#
count = GridView1.Rows.Count;

GridView1.DataBind();

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.
 
Share this answer
 
Comments
Richard MacCutchan 19-Apr-15 7:59am    
And you expect us to guess what that is supposed to mean? If you want help here then you need to provide some proper information. It is no use just dumping all your code and expecting people to figure what is or is not going on. Please do some basic diagnosis and collect some more details on the problem. When you have done that then edit your question and add the information that will help us to help you.
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

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


C#
protected void Button1_OnClick(object sender, EventArgs e)
    {
        Cache["i"] = (int)Cache["i"]+ 1;
        Response.Write(Cache["i"]);
    }
 
Share this answer
 
Thank you for your replies.I got the answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900