Try this:
HTML Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="97px">
<asp:ListItem Selected="True">Sem-I</asp:ListItem>
<asp:ListItem>Sem-II</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<p>
</p>
</form>
</body>
</html>
aspx.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mydatabase;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("insert student values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.ToString() + "')", con);
int i=cmd.ExecuteNonQuery();
if (i > 0)
{
Label1.Text = "Inserted Successfully.";
string[] row = new string[] { TextBox1.Text, DropDownList1.SelectedItem.ToString() };
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Items");
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Class", Type.GetType("System.String"));
dt.Rows.Add(row);
GridView1.DataSource = ds;
GridView1.DataBind();
ds.Dispose();
dt.Dispose();
}
}
}
Sina, if this helps please login to Mark As Answer. | Alert Moderator