hi All,
I am new to dot net...
i am learning it and I am doing my masters project in c# dot net with sql server 2000 backend...
The issue i am having now is...
i am having a gridview, upon selection of one of the rows, the row data is loaded into the textbox and dropdowns, so that when changes are made and update button is hit, the data is updated in the backend...
all works fine except for the update :( issue is with postback but i dont know how to do that... pls help... giving all the codes below...
-----------
------------------------
table creation code:
I am new to dot net...
i am learning it and I am doing my masters project in c# dot net with sql server 2000 backend...
The issue i am having now is...
i am having a gridview, upon selection of one of the rows, the row data is loaded into the textbox and dropdowns, so that when changes are made and update button is hit, the data is updated in the backend...
all works fine except for the update :( issue is with postback but i dont know how to do that... pls help... giving all the codes below...
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class scenarios : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("...;");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
SqlDataAdapter da;
common obj = new common();
string sql;
string s;
protected void Page_Load(object sender, EventArgs e)
{
Session["projid"] = "12345";
Session["projname"] = "ProjectA";
if (!IsPostBack)
{
pid.Text = Session["projid"].ToString();
pname.Text = Session["projname"].ToString();
sql = "select * from scenarios where pid = '" + Session["projid"].ToString() + "'";
da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
int prowct = dt.Rows.Count;
if (prowct == 0)
{
btnadd.Visible = true;
btnupdate.Visible = false;
GridView1.DataBind();
}
else
{
btnadd.Visible = true;
btnupdate.Visible = false;
GridView1.DataBind();
}
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
con.Open();
sql = "Insert into scenarios values ('" + pid.Text + "','" + pname.Text + "','" + tbcat.Text + "','" + tbscenario.Text + "','" + ddlprob.SelectedValue.ToString() + "','" + ddlprob.SelectedItem.Text + "','" + ddlimpact.SelectedValue.ToString() + "','" + ddlimpact.SelectedItem.Text + "','" + tbmitig.Text + "')";
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
obj.msgbox("Values are inserted");
con.Close();
clearall();
GridView1.DataBind();
}
public void clearall()
{
tbcat.Text = "";
tbscenario.Text = "";
ddlprob.ClearSelection();
ddlimpact.ClearSelection();
tbmitig.Text = "";
}
protected void btnreset_Click(object sender, EventArgs e)
{
clearall();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
btnadd.Visible = false;
btnupdate.Visible = true;
s = GridView1.SelectedValue.ToString();
sql = "select * from scenarios where ID = '" + s + "'";
con.Open();
cmd = new SqlCommand(sql, con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
tbcat.Text = dr[3].ToString();
tbscenario.Text = dr[4].ToString();
ddlprob.SelectedIndex = int.Parse(dr[5].ToString());
ddlimpact.SelectedIndex = int.Parse(dr[7].ToString());
tbmitig.Text = dr[9].ToString();
}
con.Close();
}
protected void btnupdate_Click(object sender, EventArgs e)
{
//cmd.Connection = con;
con.Open();
sql = "update scenarios set discateg = '" + tbcat.Text + "', disscenario= '" + tbscenario.Text + "', probval = '" + ddlprob.SelectedValue.ToString() + "', prob = '" + ddlprob.SelectedItem.Text + "', impactval = '" +ddlimpact.SelectedValue.ToString() + "', impact = '" + ddlimpact.SelectedItem.Text + "', mitig = '" + tbmitig.Text + "' where ID= '" +s + "'";
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
obj.msgbox("Values are updated");
con.Close();
clearall();
GridView1.DataBind();
}
}
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="scenarios.aspx.cs" Inherits="scenarios" %>
<!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>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
text-align: right;
}
</style>
</head>
<body style="font-family: Verdana; font-size: x-small">
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Project Name:</td>
<td>
<asp:Label ID="pname" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
<br />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Project ID:</td>
<td>
<asp:Label ID="pid" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Category:</td>
<td>
<asp:TextBox ID="tbcat" runat="server" Width="183px" Font-Names="Verdana"
Font-Size="X-Small"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Disaster Scenario:</td>
<td>
<asp:TextBox ID="tbscenario" runat="server" Height="62px" Width="183px"
Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Probability:</td>
<td>
<asp:DropDownList ID="ddlprob" runat="server" Font-Names="Verdana"
Font-Size="X-Small">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Low</asp:ListItem>
<asp:ListItem Value="2">Medium</asp:ListItem>
<asp:ListItem Value="3">High</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Impact:</td>
<td>
<asp:DropDownList ID="ddlimpact" runat="server" Font-Names="Verdana"
Font-Size="X-Small">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">High</asp:ListItem>
<asp:ListItem Value="2">Medium</asp:ListItem>
<asp:ListItem Value="3">Low</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
Mitigation:</td>
<td>
<asp:TextBox ID="tbmitig" runat="server" Height="62px" Width="183px"
Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<asp:Button ID="btnupdate" runat="server" Height="20px"
onclick="btnupdate_Click" Text="Update" Width="50px" />
<asp:Button ID="btnadd" runat="server" Height="20px" Text="Add" Width="50px"
onclick="btnadd_Click" />
</td>
<td>
<asp:Button ID="btnreset" runat="server" Height="20px" Text="Reset"
Width="50px" onclick="btnreset_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2" colspan="2">
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataKeyNames="ID" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="text-align: left">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:bcpertConnectionString %>"
SelectCommand="SELECT * FROM [scenarios] WHERE ([pid] = @pid)">
<SelectParameters>
<asp:SessionParameter Name="pid" SessionField="projid" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
table creation code:
Code:
CREATE TABLE [dbo].[scenarios] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [pid] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [pname] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [discateg] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [disscenario] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [prob] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [impact] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [mitig] [varchar] (300) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] GO
Comment