eartherian 0 Newbie Poster

Hello Friends,

I am learning to code in ASP.Net (so forgive silly errors!)...

Presetnly trying to perform the functions of edit and delete using Repeater.


The Code is:

Libraries that I am using

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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.Windows.Forms;

Problematic code

protected void action(object o, RepeaterCommandEventArgs e)
    {
        SqlConnection conn;
        conn = new SqlConnection(@"Data Source=XPWINDOWS7\SQLEXPRESS;Initial Catalog=Pl_Portal;Integrated Security=SSPI");

        if (e.CommandName == "Update")
        {

            foreach (RepeaterItem item in Repeater1.Items)
            {

                SqlCommand cmd = new SqlCommand("update noti set date=@dt,compId=@compId,profile=@profile,repoAdd=@repoAdd,Xper=@XPer,XIIPer=@XIIPer,gradPer=@gradPer,pgPer=@pgPer,gapYr=@gapYr,bkLog=@bkLog,lastDate=@lastDate where notiId=@notiId", conn);
                cmd.Connection.Open();
                cmd.Parameters.Add("@dt", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("dt")).Text);
                cmd.Parameters.Add("@compId", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("compId")).Text);
                cmd.Parameters.Add("@profile", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("profile")).Text);
                cmd.Parameters.Add("@repoAdd", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("repoAdd")).Text);
                cmd.Parameters.Add("@XPer", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("XPer")).Text);
                cmd.Parameters.Add("@XIIPer", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("XIIPer")).Text);
                cmd.Parameters.Add("@gradPer", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("gradPer")).Text);
                cmd.Parameters.Add("@pgPer", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("pgPer")).Text);
                cmd.Parameters.Add("@gapYr", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("gapYr")).Text);
                cmd.Parameters.Add("@bkLog", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("bkLog")).Text);
                cmd.Parameters.Add("@lastDate", ((System.Web.UI.WebControls.TextBox)Repeater1.FindControl("lastDate")).Text);
                cmd.Parameters.Add("@notiId", ((System.Web.UI.WebControls.Label)Repeater1.FindControl("lbl1")).Text);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                cmd.Connection.Close();
            }
        }
        else if (e.CommandName == "Delete")
        {
                 //delete code
        }
    }

    protected void btnsearch_Click(object sender, EventArgs e)
    {
        SqlConnection conn;
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        btnclk = true;

        conn = new SqlConnection(@"Data Source=XPWINDOWS7\SQLEXPRESS;Initial Catalog=Pl_Portal;Integrated Security=SSPI");
        conn.Open();

        if (RadioButtonList1.SelectedIndex == 0)
        {
            String s = tb1.Text;
            String query;

            SqlCommand cmd = new SqlCommand("SELECT noti.[notiId], noti.[handlerId], noti.[date], noti.[compId], noti.[profile], noti.[repoAdd], noti.[XPer], noti.[XIIPer], noti.[gradPer], noti.[pgPer], noti.[gapYr], noti.[bkLog], noti.[lastDate] FROM [noti] WHERE noti.[notiId]="+ @s+"");
            cmd.Parameters.AddWithValue("@s", s);
            query = cmd.CommandText;


            da = new SqlDataAdapter(query, conn);
            da.Fill(dt);
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            da.Dispose();
            conn.Close();
            cmd.Dispose();
            
        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            String s = dropdownsearch.SelectedValue;
            String query;

            //Earlier query was like:
            //SqlCommand cmd = new SqlCommand("SELECT noti.[notiId], noti.[handlerId], noti.[date], company.[name], noti.[profile], noti.[pgPer], noti.[lastDate] FROM [noti],[company] WHERE noti.[compId]=" + @s + "AND noti.[compId]=company.[compID]");


            SqlCommand cmd = new SqlCommand("SELECT noti.[notiId], noti.[handlerId], noti.[date], noti.[compId], noti.[profile], noti.[repoAdd], noti.[XPer], noti.[XIIPer], noti.[gradPer], noti.[pgPer], noti.[gapYr], noti.[bkLog], noti.[lastDate] FROM [noti] WHERE noti.[compId]= @s ");
            cmd.Parameters.AddWithValue("@s", s);
            query = cmd.CommandText;


            da = new SqlDataAdapter(query, conn);
            da.Fill(dt);
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            da.Dispose();
            conn.Close();
            cmd.Dispose();
        }

    }




    protected void rbl1_selectedindexchanged(object sender, System.EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex == 0)
        {
            tb1.Visible = true;
            dropdownsearch.Visible = false;

        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            tb1.Visible = false;
            dropdownsearch.Visible = true;
        }

    }


}

The aspx file....

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="rpt1_ItemDataBound" OnItemCommand="action">    
        <ItemTemplate>
        <div>
        <h1><asp:Label ID="lbl1" Text='<%#Eval("notiID")%>' runat="server"/></h1>
        <h3><asp:Label ID="Label1" Text='<%#Eval("handlerId")%>' runat="server"/></h3>
        <b>Date: </b> <asp:TextBox id="dt" Text='<%# Eval("date") %>' runat="server" />
        <br />
        <b>Company Id: </b> <asp:TextBox id="compId" Text='<%# Eval("compId") %>' runat="server" />
        <br />
        <b>Job Profile: </b> <asp:TextBox id="profile" Text='<%# Eval("profile") %>' runat="server" />
        <br />
        <b>Reporting Address: </b> <asp:TextBox id="repoAdd" Text='<%# Eval("repoAdd") %>' runat="server" />
        <br />
        <b>Tenth Percentage: </b> <asp:TextBox id="XPer" Text='<%# Eval("XPer") %>' runat="server" />
        <br />
        <b>Twelfth Percentage: </b> <asp:TextBox id="XIIPer" Text='<%# Eval("XIIPer") %>' runat="server" />
        <br />
        <b>Graduation Percentage: </b> <asp:TextBox id="gradPer" Text='<%# Eval("gradPer") %>' runat="server" />
        <br />
        <b>MCA Percentage: </b> <asp:TextBox id="pgPer" Text='<%# Eval("pgPer") %>' runat="server" />
        <br />
        <b>Gap Year: </b> <asp:TextBox id="gapYr" Text='<%# Eval("gapYr") %>' runat="server" />
        <br />
        <b>Back Log(s): </b> <asp:TextBox id="bkLog" Text='<%# Eval("bkLog") %>' runat="server" />
        <br />
        <b>Last Date: </b> <asp:TextBox id="lastDate" Text='<%# Eval("lastDate") %>' runat="server" />
        <br />
        <asp:Label ID="Lbl2" Text='' runat="server" BackColor="Crimson" ForeColor="White"/>
        <br />
        <asp:LinkButton id="lnkUpdate" CommandName="Update" Text="Update" Runat="server" /> &nbsp;|&nbsp;
        <asp:LinkButton id="lnkDelete" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure?');" Runat="server" />



        <a href="jobPostDetail.aspx?nt=<%# Eval("notiID")%>">More</a> | <a href="Stu_Applied.aspx?nt=<%# Eval("notiID")%>">List of Students</a>
        
    </div>

        </ItemTemplate>
        </asp:Repeater>

Please show me a path....!!


Thank you!