Connecting Tech Pros Worldwide Forums | Help | Site Map

Gridview and update

Newbie
 
Join Date: Feb 2009
Posts: 4
#1: Feb 15 '09
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...
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14.  
  15. public partial class scenarios : System.Web.UI.Page
  16. {
  17.     SqlConnection con = new SqlConnection("...;");
  18.     SqlCommand cmd = new SqlCommand();
  19.     SqlDataReader dr;
  20.     SqlDataAdapter da;
  21.     common obj = new common();
  22.     string sql;
  23.     string s;
  24.  
  25.     protected void Page_Load(object sender, EventArgs e)
  26.     {
  27.         Session["projid"] = "12345";
  28.         Session["projname"] = "ProjectA";
  29.         if (!IsPostBack)
  30.         {
  31.             pid.Text = Session["projid"].ToString();
  32.             pname.Text = Session["projname"].ToString();
  33.             sql = "select * from scenarios where pid = '" + Session["projid"].ToString() + "'";
  34.             da = new SqlDataAdapter(sql, con);
  35.             DataTable dt = new DataTable();
  36.             da.Fill(dt);
  37.             int prowct = dt.Rows.Count;
  38.             if (prowct == 0)
  39.             {
  40.                 btnadd.Visible = true;
  41.                 btnupdate.Visible = false;
  42.                 GridView1.DataBind();
  43.             }  
  44.             else
  45.             {
  46.                 btnadd.Visible = true;
  47.                 btnupdate.Visible = false;
  48.                 GridView1.DataBind();
  49.             }  
  50.  
  51.  
  52.         }
  53.  
  54.     }
  55.     protected void btnadd_Click(object sender, EventArgs e)
  56.     {
  57.         con.Open();
  58.         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 + "')";
  59.         cmd = new SqlCommand(sql, con);
  60.         cmd.ExecuteNonQuery();
  61.         obj.msgbox("Values are inserted");
  62.         con.Close();
  63.         clearall();
  64.         GridView1.DataBind();
  65.  
  66.     }
  67.  
  68.     public void clearall()
  69.     {
  70.         tbcat.Text = "";
  71.         tbscenario.Text = "";
  72.         ddlprob.ClearSelection();
  73.         ddlimpact.ClearSelection();
  74.         tbmitig.Text = "";
  75.     }
  76.     protected void btnreset_Click(object sender, EventArgs e)
  77.     {
  78.         clearall();
  79.     }
  80.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  81.     {
  82.         btnadd.Visible = false;
  83.         btnupdate.Visible = true;
  84.         s = GridView1.SelectedValue.ToString();
  85.         sql = "select * from scenarios where ID = '" + s + "'";
  86.         con.Open();
  87.         cmd = new SqlCommand(sql, con);
  88.         dr = cmd.ExecuteReader();
  89.         if (dr.Read())
  90.         {
  91.             tbcat.Text = dr[3].ToString();
  92.             tbscenario.Text = dr[4].ToString();
  93.             ddlprob.SelectedIndex = int.Parse(dr[5].ToString());
  94.             ddlimpact.SelectedIndex = int.Parse(dr[7].ToString());
  95.             tbmitig.Text = dr[9].ToString();
  96.         }
  97.         con.Close();
  98.  
  99.     }
  100.     protected void btnupdate_Click(object sender, EventArgs e)
  101.     {
  102.         //cmd.Connection = con;
  103.         con.Open();
  104.         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 + "'";
  105.         cmd = new SqlCommand(sql, con);
  106.         cmd.ExecuteNonQuery();
  107.         obj.msgbox("Values are updated");
  108.         con.Close();
  109.         clearall();
  110.         GridView1.DataBind();
  111.  
  112.     }
  113. }
-----------
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="scenarios.aspx.cs" Inherits="scenarios" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8.     <style type="text/css">
  9.         .style1
  10.         {
  11.             width: 100%;
  12.         }
  13.         .style2
  14.         {
  15.             text-align: right;
  16.         }
  17.     </style>
  18. </head>
  19. <body style="font-family: Verdana; font-size: x-small">
  20.     <form id="form1" runat="server">
  21.     <div>
  22.  
  23.         <table class="style1">
  24.             <tr>
  25.                 <td>
  26.                     &nbsp;</td>
  27.                 <td class="style2">
  28.                     &nbsp;</td>
  29.                 <td>
  30.                     &nbsp;</td>
  31.                 <td>
  32.                     &nbsp;</td>
  33.             </tr>
  34.             <tr>
  35.                 <td>
  36.                     &nbsp;</td>
  37.                 <td class="style2">
  38.                     Project Name:</td>
  39.                 <td>
  40.                     <asp:Label ID="pname" runat="server"></asp:Label>
  41.                 </td>
  42.                 <td>
  43.                     &nbsp;</td>
  44.             </tr>
  45.             <tr>
  46.                 <td>
  47.                     &nbsp;</td>
  48.                 <td class="style2">
  49.                     &nbsp;</td>
  50.                 <td>
  51.                     <br />
  52.                 </td>
  53.                 <td>
  54.                     &nbsp;</td>
  55.             </tr>
  56.             <tr>
  57.                 <td>
  58.                     &nbsp;</td>
  59.                 <td class="style2">
  60.                     Project ID:</td>
  61.                 <td>
  62.                     <asp:Label ID="pid" runat="server"></asp:Label>
  63.                 </td>
  64.                 <td>
  65.                     &nbsp;</td>
  66.             </tr>
  67.             <tr>
  68.                 <td>
  69.                     &nbsp;</td>
  70.                 <td class="style2">
  71.                     &nbsp;</td>
  72.                 <td>
  73.                     &nbsp;</td>
  74.                 <td>
  75.                     &nbsp;</td>
  76.             </tr>
  77.             <tr>
  78.                 <td>
  79.                     &nbsp;</td>
  80.                 <td class="style2">
  81.                     Category:</td>
  82.                 <td>
  83.                     <asp:TextBox ID="tbcat" runat="server" Width="183px" Font-Names="Verdana" 
  84.                         Font-Size="X-Small"></asp:TextBox>
  85.                 </td>
  86.                 <td>
  87.                     &nbsp;</td>
  88.             </tr>
  89.             <tr>
  90.                 <td>
  91.                     &nbsp;</td>
  92.                 <td class="style2">
  93.                     &nbsp;</td>
  94.                 <td>
  95.                     &nbsp;</td>
  96.                 <td>
  97.                     &nbsp;</td>
  98.             </tr>
  99.             <tr>
  100.                 <td>
  101.                     &nbsp;</td>
  102.                 <td class="style2">
  103.                     Disaster Scenario:</td>
  104.                 <td>
  105.                     <asp:TextBox ID="tbscenario" runat="server" Height="62px" Width="183px" 
  106.                         Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
  107.                 </td>
  108.                 <td>
  109.                     &nbsp;</td>
  110.             </tr>
  111.             <tr>
  112.                 <td>
  113.                     &nbsp;</td>
  114.                 <td class="style2">
  115.                     &nbsp;</td>
  116.                 <td>
  117.                     &nbsp;</td>
  118.                 <td>
  119.                     &nbsp;</td>
  120.             </tr>
  121.             <tr>
  122.                 <td>
  123.                     &nbsp;</td>
  124.                 <td class="style2">
  125.                     Probability:</td>
  126.                 <td>
  127.                     <asp:DropDownList ID="ddlprob" runat="server" Font-Names="Verdana" 
  128.                         Font-Size="X-Small">
  129.                         <asp:ListItem Value="0">Select</asp:ListItem>
  130.                         <asp:ListItem Value="1">Low</asp:ListItem>
  131.                         <asp:ListItem Value="2">Medium</asp:ListItem>
  132.                         <asp:ListItem Value="3">High</asp:ListItem>
  133.                     </asp:DropDownList>
  134.                 </td>
  135.                 <td>
  136.                     &nbsp;</td>
  137.             </tr>
  138.             <tr>
  139.                 <td>
  140.                     &nbsp;</td>
  141.                 <td class="style2">
  142.                     &nbsp;</td>
  143.                 <td>
  144.                     &nbsp;</td>
  145.                 <td>
  146.                     &nbsp;</td>
  147.             </tr>
  148.             <tr>
  149.                 <td>
  150.                     &nbsp;</td>
  151.                 <td class="style2">
  152.                     Impact:</td>
  153.                 <td>
  154.                     <asp:DropDownList ID="ddlimpact" runat="server" Font-Names="Verdana" 
  155.                         Font-Size="X-Small">
  156.                         <asp:ListItem Value="0">Select</asp:ListItem>
  157.                         <asp:ListItem Value="1">High</asp:ListItem>
  158.                         <asp:ListItem Value="2">Medium</asp:ListItem>
  159.                         <asp:ListItem Value="3">Low</asp:ListItem>
  160.                     </asp:DropDownList>
  161.                 </td>
  162.                 <td>
  163.                     &nbsp;</td>
  164.             </tr>
  165.             <tr>
  166.                 <td>
  167.                     &nbsp;</td>
  168.                 <td class="style2">
  169.                     &nbsp;</td>
  170.                 <td>
  171.                     &nbsp;</td>
  172.                 <td>
  173.                     &nbsp;</td>
  174.             </tr>
  175.             <tr>
  176.                 <td>
  177.                     &nbsp;</td>
  178.                 <td class="style2">
  179.                     Mitigation:</td>
  180.                 <td>
  181.                     <asp:TextBox ID="tbmitig" runat="server" Height="62px" Width="183px" 
  182.                         Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
  183.                 </td>
  184.                 <td>
  185.                     &nbsp;</td>
  186.             </tr>
  187.             <tr>
  188.                 <td>
  189.                     &nbsp;</td>
  190.                 <td class="style2">
  191.                     &nbsp;</td>
  192.                 <td>
  193.                     &nbsp;</td>
  194.                 <td>
  195.                     &nbsp;</td>
  196.             </tr>
  197.             <tr>
  198.                 <td>
  199.                     &nbsp;</td>
  200.                 <td class="style2">
  201.                     <asp:Button ID="btnupdate" runat="server" Height="20px" 
  202.                         onclick="btnupdate_Click" Text="Update" Width="50px" />
  203.                     <asp:Button ID="btnadd" runat="server" Height="20px" Text="Add" Width="50px" 
  204.                         onclick="btnadd_Click" />
  205.                 </td>
  206.                 <td>
  207. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  208.                     <asp:Button ID="btnreset" runat="server" Height="20px" Text="Reset" 
  209.                         Width="50px" onclick="btnreset_Click" />
  210.                 </td>
  211.                 <td>
  212.                     &nbsp;</td>
  213.             </tr>
  214.             <tr>
  215.                 <td>
  216.                     &nbsp;</td>
  217.                 <td class="style2" colspan="2">
  218.                     <br />
  219.                     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  220.                         AllowSorting="True" DataKeyNames="ID" DataSourceID="SqlDataSource1" 
  221.                         onselectedindexchanged="GridView1_SelectedIndexChanged" 
  222.                         style="text-align: left">
  223.                         <Columns>
  224.                             <asp:CommandField ShowSelectButton="True" />
  225.                         </Columns>
  226.                     </asp:GridView>
  227.                     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  228.                         ConnectionString="<%$ ConnectionStrings:bcpertConnectionString %>" 
  229.                         SelectCommand="SELECT * FROM [scenarios] WHERE ([pid] = @pid)">
  230.                         <SelectParameters>
  231.                             <asp:SessionParameter Name="pid" SessionField="projid" Type="String" />
  232.                         </SelectParameters>
  233.                     </asp:SqlDataSource>
  234.                 </td>
  235.                 <td>
  236.                     &nbsp;</td>
  237.             </tr>
  238.         </table>
  239.  
  240.     </div>
  241.     </form>
  242. </body>
  243. </html>
------------------------

table creation code:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE [dbo].[scenarios] (
  2.     [ID] [int] IDENTITY (1, 1) NOT NULL ,
  3.     [pid] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  4.     [pname] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  5.     [discateg] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  6.     [disscenario] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  7.     [prob] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  8.     [impact] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  9.     [mitig] [varchar] (300) COLLATE SQL_Latin1_General_CP1_CI_AS NULL 
  10. ) ON [PRIMARY]
  11. GO

Member
 
Join Date: Oct 2007
Posts: 39
#2: Feb 16 '09

re: Gridview and update


Did u check the AutoPostback property of the update button? it should be set to true,
Newbie
 
Join Date: Feb 2009
Posts: 4
#3: Feb 16 '09

re: Gridview and update


still now luck Sreemathy... here is wat i did... anything else i should do???

<asp:Button ID="btnupdate" AutoPostBack = "true" runat="server" Height="20px"
onclick="btnupdate_Click" Text="Update" />
Member
 
Join Date: Jul 2008
Posts: 36
#4: Feb 16 '09

re: Gridview and update


You might like to study SQL injection, for example your statement

Expand|Select|Wrap|Line Numbers
  1. sql = "select * from scenarios where ID = '" + s + "'";
is begging for trouble. Even if you control where the s comes, using parameters shows to your teacher you think about security.
Newbie
 
Join Date: Feb 2009
Posts: 4
#5: Feb 16 '09

re: Gridview and update


yep i understand... but being a newbie i am just learning the stuff...

Any help on the issue i am actually facing??? (mentioned in the first post)
Member
 
Join Date: Jul 2008
Posts: 36
#6: Feb 16 '09

re: Gridview and update


I still recommend you to use parameters. For example the update statement; you use

Expand|Select|Wrap|Line Numbers
  1. 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 + "'";
What if the tbscenario.Text contains value It wasn't my cat ? You get run time error or all the data in screnarios table is updated. Use parameters to prevent this (and SQL injection...)
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#7: Feb 18 '09

re: Gridview and update


Quote:

Originally Posted by shanthidiana View Post

hi All,
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...

What is this "issue with postback"?
Could you please explain this issue in more details?


Check out this article for more information about SQL Injection attacks and please take a look at the article about how to use a database in your program for more information on how to use parametrized SQL queries like Artov was suggesting.

If you change your SQL query to use Parameters you may see an error with your SQL statement more clearly. Have you tried executing the update sql statement in Query Analyzer or some other tool to make sure it works?
Newbie
 
Join Date: Feb 2009
Posts: 4
#8: Feb 21 '09

re: Gridview and update


hi all i fixed the issue at last... the issue was with GridView1_SelectedIndexChanged event... the string s was assigned a value of null everytime... so for this, i used a hidden label instead of getting the value assigned to s... it worked... tat is y i gave the code... if someone wanted to run that from their end... but no one did... anyway thanks for ur help.

Also i have started using parameters now... i should be excused for the above code as i am new to dot net :)
Reply

Tags
gridview