Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi here is windows form code i want this code for my website project.can any one help me ??how can i give code for my aspx page.i wanna display same output in my aspx page as displayed in winform

private void button1_Click(object sender, EventArgs e)
      {

          dataGridView1.Rows.Clear();
          dataGridView1.ColumnCount = 10;
          dataGridView1.RowCount = 10;
          dataGridView1.Columns[0].Width = 100;
          dataGridView1.Columns[1].Width = 100;
          dataGridView1.Columns[2].Width = 20;
          dataGridView1.Columns[3].Width = 100;
          dataGridView1.Columns[4].Width = 100;
          dataGridView1.Columns[5].Width = 20;
          dataGridView1.Columns[6].Width = 100;
          dataGridView1.Columns[7].Width = 100;
          //dataGridView1.Rows(0).Cells(0).Value = "Semister";
          dataGridView1.Columns[0].HeaderText = "I Row";
          dataGridView1.Columns[1].HeaderText = "II Row";
          dataGridView1.Columns[2].HeaderText = "";
          dataGridView1.Columns[3].HeaderText = "III Row";
          dataGridView1.Columns[4].HeaderText = "IV Row";
          dataGridView1.Columns[5].HeaderText = "";
          dataGridView1.Columns[6].HeaderText = "V Row";
          dataGridView1.Columns[7].HeaderText = "VI Row";

          int a = 1, b = 0,c=0;
          DateTime d1 = DateTime.Today.Date;
          string str1="";
          str1 = comboBox1.Text;
          d1 = new DateTime(int.Parse(str1.Substring(6, 4)), int.Parse(str1.Substring(3, 2)), int.Parse(str1.Substring(0, 2)));

          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
          cmd.Connection = conn;
          cmd.CommandText = "select Regno,deskno,deskside,semister from AllotmentTab where RoomNo='" + comboBox3.Text + "' and examdate='" + d1 + "' and esession='" + comboBox2.Text + "'  order by Deskno,deskside";
          dr = cmd.ExecuteReader();
          while (dr.Read())
          {
              b = int.Parse(dr.GetValue(1).ToString());
              if (b > 14)
              { b = b - 14;
              c = 6;
              }
              else if (b > 7)
              { b = b - 7;
              c = 3;
              }
              else
              {
                  //b = b - 7;
                  c = 0;
              }
              if (dr.GetValue(2).ToString() == "L")
              {
                  dataGridView1.Rows[b].Cells[c].Value = dr.GetValue(0).ToString() + " (" + dr.GetValue(3).ToString() + ")";
              }
              else
              {
                  dataGridView1.Rows[b].Cells[c + 1].Value = dr.GetValue(0).ToString() + " (" + dr.GetValue(3).ToString() + ")";
              }
              a++;

              if ((a % 7) == 0)
              {

              }

          }



          //Alloted Faculty :
          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd5 = new SqlCommand();
          SqlDataReader dr5;
          cmd5.Connection = conn;
          cmd5.CommandText = "select facultycode from fAllotmentTab where RoomNo='" + comboBox3.Text + "' and examdate='" + d1 + "' and esession='" + comboBox2.Text + "'";
          dr5 = cmd5.ExecuteReader();
          if (dr5.Read())
          {
              labelmsg.Text = "Alloted Faculty : " + dr5.GetValue(0).ToString();
          }
          else { labelmsg.Text = "Alloted Faculty : "; }


      }

      private void RoomwiseDisForm_Load(object sender, EventArgs e)
      {
          for (int i = 101; i < 111; i++)
          {
              comboBox3.Items.Add(i.ToString());
          }

          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
          cmd.Connection = conn;
          cmd.CommandText = "select ExamDate from AllotmentTab group by ExamDate  order by ExamDate";
          dr = cmd.ExecuteReader();
          while (dr.Read())
          {
              comboBox1.Items.Add(string.Format("{0:dd-MM-yyyy}",dr.GetValue(0)));

          }

      }


and this is design code..
C#
private void InitializeComponent()
       {
           this.button1 = new System.Windows.Forms.Button();
           this.labelmsg = new System.Windows.Forms.Label();
           this.comboBox3 = new System.Windows.Forms.ComboBox();
           this.label1 = new System.Windows.Forms.Label();
           this.dataGridView1 = new System.Windows.Forms.DataGridView();
           this.comboBox1 = new System.Windows.Forms.ComboBox();
           this.label2 = new System.Windows.Forms.Label();
           this.comboBox2 = new System.Windows.Forms.ComboBox();
           this.label3 = new System.Windows.Forms.Label();
           this.button2 = new System.Windows.Forms.Button();
           ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
           this.SuspendLayout();
           //
           // button1
           //
           this.button1.Location = new System.Drawing.Point(475, 28);
           this.button1.Name = "button1";
           this.button1.Size = new System.Drawing.Size(133, 31);
           this.button1.TabIndex = 22;
           this.button1.Text = "Display";
           this.button1.UseVisualStyleBackColor = true;
           this.button1.Click += new System.EventHandler(this.button1_Click);
           //
           // labelmsg
           //
           this.labelmsg.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
           this.labelmsg.Location = new System.Drawing.Point(24, 89);
           this.labelmsg.Name = "labelmsg";
           this.labelmsg.Size = new System.Drawing.Size(431, 16);
           this.labelmsg.TabIndex = 21;
           this.labelmsg.Text = "Alloted Faculty :";
           //
           // comboBox3
           //
           this.comboBox3.FormattingEnabled = true;
           this.comboBox3.Location = new System.Drawing.Point(27, 35);
           this.comboBox3.Name = "comboBox3";
           this.comboBox3.Size = new System.Drawing.Size(93, 24);
           this.comboBox3.TabIndex = 20;
           //
           // label1
           //
           this.label1.AutoSize = true;
           this.label1.Location = new System.Drawing.Point(24, 16);
           this.label1.Name = "label1";
           this.label1.Size = new System.Drawing.Size(65, 16);
           this.label1.TabIndex = 19;
           this.label1.Text = "Semister";
           //
           // dataGridView1
           //
           this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
           this.dataGridView1.Location = new System.Drawing.Point(27, 109);
           this.dataGridView1.Margin = new System.Windows.Forms.Padding(4);
           this.dataGridView1.Name = "dataGridView1";
           this.dataGridView1.Size = new System.Drawing.Size(971, 485);
           this.dataGridView1.TabIndex = 18;
           //
           // comboBox1
           //
           this.comboBox1.FormattingEnabled = true;
           this.comboBox1.Location = new System.Drawing.Point(172, 35);
           this.comboBox1.Name = "comboBox1";
           this.comboBox1.Size = new System.Drawing.Size(165, 24);
           this.comboBox1.TabIndex = 24;
           //
           // label2
           //
           this.label2.AutoSize = true;
           this.label2.Location = new System.Drawing.Point(169, 16);
           this.label2.Name = "label2";
           this.label2.Size = new System.Drawing.Size(39, 16);
           this.label2.TabIndex = 23;
           this.label2.Text = "Date";
           //
           // comboBox2
           //
           this.comboBox2.FormattingEnabled = true;
           this.comboBox2.Items.AddRange(new object[] {
           "M",
           "A"});
           this.comboBox2.Location = new System.Drawing.Point(362, 35);
           this.comboBox2.Name = "comboBox2";
           this.comboBox2.Size = new System.Drawing.Size(93, 24);
           this.comboBox2.TabIndex = 26;
           this.comboBox2.Text = "M";
           //
           // label3
           //
           this.label3.AutoSize = true;
           this.label3.Location = new System.Drawing.Point(359, 16);
           this.label3.Name = "label3";
           this.label3.Size = new System.Drawing.Size(58, 16);
           this.label3.TabIndex = 25;
           this.label3.Text = "Session";
           //
           // button2
           //
           this.button2.Location = new System.Drawing.Point(865, 35);
           this.button2.Name = "button2";
           this.button2.Size = new System.Drawing.Size(133, 31);
           this.button2.TabIndex = 27;
           this.button2.Text = "Close";
           this.button2.UseVisualStyleBackColor = true;
           this.button2.Click += new System.EventHandler(this.button2_Click);
           //
           // RoomwiseDisForm
           //
           this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.ClientSize = new System.Drawing.Size(1028, 607);
           this.Controls.Add(this.button2);
           this.Controls.Add(this.comboBox2);
           this.Controls.Add(this.label3);
           this.Controls.Add(this.comboBox1);
           this.Controls.Add(this.label2);
           this.Controls.Add(this.button1);
           this.Controls.Add(this.labelmsg);
           this.Controls.Add(this.comboBox3);
           this.Controls.Add(this.label1);
           this.Controls.Add(this.dataGridView1);
           this.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
           this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
           this.Name = "RoomwiseDisForm";
           this.Text = "RoomwiseDisForm";
           this.Load += new System.EventHandler(this.RoomwiseDisForm_Load);
           ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
           this.ResumeLayout(false);
           this.PerformLayout();

       }




can any one help me..
Posted
Updated 5-Mar-12 23:30pm
v3
Comments
Richard MacCutchan 6-Mar-12 4:51am    
Help with what? Please explain your problem clearly.
ythisbug 6-Mar-12 5:03am    
hi i want code for my aspx page
Richard MacCutchan 6-Mar-12 5:22am    
What code, code to provide what features or service? Do you think "i want code for my aspx page" is a clear explanation?
ythisbug 6-Mar-12 5:29am    
bro that code is for winform now i created aspx page for that i want code same out put i want to display in aspx page.so please suggest me.

The first thing you need to do is take your form and redesign it for the web page - I know this seems obvious, but it's really the starting point for hooking the UI up. While you could keep the control names the same, I think you should take the time to rename them as you are using the default values here, which is not the best thing you can do. The reason for renaming them is so that you can tell later on, what they are meant to do by looking at the name, rather than having to hunt around on the form seeing which control happens to match comboBox5.

The biggest issue you face with your code is that it all happens on a single form. There's no tier design here at all. With most web applications you would look to split the functionality over multiple "tiers" so that you can isolate and test your code with ease. If I were tackling this, I would look to move the database code into one project, business logic into another and then have the UI just map onto these. A key point for you to consider though is that, as your web application is disconnected from the server, you should be looking to acquire the connection to the database as late as possible and release it as soon as possible. Also, you should really look to use database parameters, rather than using string concatenation - if you don't, you're leaving yourself wide open to SQL Injection Attacks.
 
Share this answer
 
Comments
ythisbug 6-Mar-12 5:49am    
<div id="dvToolbar" runat="server" style="background-color: #B4C6DF">
 <asp:ImageButton ID="Search" runat="server" ImageUrl="~/Images/Search.gif"
Height="16px" Width="18px" OnClick="Search_Click" />
    </div>
<table>
<tr>
<td>
Semister<td>
<asp:DropDownList ID="ddlSemister" runat="server">

</td>
<td>
Date
</td>
<td>
<asp:DropDownList ID="ddlDate" runat="server">

</td>
<td>
Session
</td>
<td>
<asp:DropDownList ID="ddlSession" runat="server">

</td>
</td>
</tr>
<tr><td>
<asp:Label ID="labelmsg" runat="server">
</td></tr>
<tr>

<td>
Room Allotment
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grdTimeTable" runat="server" AutoGenerateColumns="false" PageSize="8">
<columns>
<asp:TemplateField HeaderText="I ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>


<asp:TemplateField HeaderText="II ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>


<asp:TemplateField HeaderText="III ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>


<asp:TemplateField HeaderText="IV ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>


<asp:TemplateField HeaderText="V ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>


<asp:TemplateField HeaderText="VI ROW">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "RegNo")%>




</td>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblMessage" runat="server">
</td>
</tr>
</tr>
</table>
here i created brother
Hello, I don't give you code for your project because you have to do it by yourself.
But
I make a few suggestion to improve your code:

1. Don't use repetitious code ("dataGridView1.Columns[0].Width = 100") use "for(int i =0;i<8;i++)" for it.

2. Use Array :
C#
string headers = new string[8]{"I Row","II Row",...};

//in a for loop:
{
    dataGridView1.Columns[i].HeaderText = headers[i];
}


3. Injection Risk: (Security warning)
Do not use cmd.CommandText = "Blah Blah Blah " + some control value + ....
use command.Parameters

4. It's better to use Switch/Case instead of using if/else if/else if/else if/...

5. Try to use "one Line if" instead of if/else, for example:
C#
//if (dr5.Read())
//{
//    labelmsg.Text = "Alloted Faculty : " + dr5.GetValue(0).ToString();
//}
//else { labelmsg.Text = "Alloted Faculty : "; }

labelmsg.Text = (dr5.Read())? "Alloted Faculty : " + dr5.GetValue(0).ToString(): "Alloted Faculty : ";

6. Close the connection after SQL operation. Have a try/catch/finally
for example:

C#
try
{
   SqlCommand cmd = new SqlCommand();
            SqlDataReader dr;
  //...
}
catch(Exception ex)
{
  //...
}
finally
{
   conn.Close();
}


7. Why don't you use ASP.Net Content instead of program logic to generate the controls?
 
Share this answer
 
v2
Comments
ythisbug 6-Mar-12 6:42am    
wow really nice.thanks brother
Shahin Khorshidnia 6-Mar-12 7:12am    
you're welcome.
El_Codero 6-Mar-12 7:16am    
good hints Shahin!5.
Shahin Khorshidnia 6-Mar-12 10:18am    
Thank you Björn

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



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