Click here to Skip to main content
15,891,864 members
Home / Discussions / ASP.NET
   

ASP.NET

 
General"Unable to cast object of type " in Gridview control when click on add button [modified] Pin
amistry_petlad18-Mar-08 9:51
amistry_petlad18-Mar-08 9:51 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
pmarfleet18-Mar-08 11:19
pmarfleet18-Mar-08 11:19 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
Not Active18-Mar-08 11:35
mentorNot Active18-Mar-08 11:35 
QuestionRe: "Unable to cast object of type " in Gridview control when click on add button Pin
amistry_petlad18-Mar-08 13:32
amistry_petlad18-Mar-08 13:32 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
Not Active18-Mar-08 14:20
mentorNot Active18-Mar-08 14:20 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
amistry_petlad19-Mar-08 5:04
amistry_petlad19-Mar-08 5:04 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
Not Active19-Mar-08 5:57
mentorNot Active19-Mar-08 5:57 
GeneralRe: "Unable to cast object of type " in Gridview control when click on add button Pin
amistry_petlad20-Mar-08 3:36
amistry_petlad20-Mar-08 3:36 
Ya solve the problem ,But I have a question when I have used command button in the gridview now when i
click the "Insert" for new record the linkbutton every time use the "update" action so rather then inserting new record.
what should I do?
I had debug the application and explicitly put the value then it takes but when i use routine its default takes "Update" every time.
Please look at little code for understanding.

when click on addbutton

 protected void btnAdd_Click(object sender, EventArgs e)<br />
    {<br />
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnectionString"].ConnectionString);<br />
        SqlDataAdapter da = new SqlDataAdapter("SELECT subcat_id,subcatename,description,Pid  FROM Subcatagory", con);<br />
        DataTable dt = new DataTable();<br />
        da.Fill(dt);<br />
<br />
        // Here we'll add a blank row to the returned DataTable<br />
        DataRow dr = dt.NewRow();<br />
        dt.Rows.InsertAt(dr, 0);<br />
        //Creating the first row of GridView to be Editable<br />
        GridView1.EditIndex = 0;<br />
        GridView1.DataSource = dt;<br />
        GridView1.DataBind();<br />
        <br />
        //Changing the Text for Inserting a New Record<br />
<br />
        //dt.Reset();<br />
         //here the lick button shown "insert"<br />
        ((LinkButton)GridView1.Rows[0].Cells[5].Controls[0]).Text = "Insert";<br />
        Session["operation"] = "Insert"; <br />
<br />
    }<br />
<br />
When control leaves the scope it automatic use "Update"<br />


For row updating

<br />
<br />
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)<br />
    {<br />
<br />
           <big>// The following link button every time shown me update value</big><br />
<br />
        if (((LinkButton)GridView1.Rows[0].Cells[5].Controls[0]).Text == "Insert" || Session["operation"] == "Insert")<br />
        {<br />
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnectionString"].ConnectionString);<br />
            SqlCommand cmd = new SqlCommand();<br />
            cmd.CommandText = "INSERT INTO Subcatagory(subcatename,description,Pid) VALUES(@subcatename,@description,@Pid)";<br />
            //cmd.CommandText = "INSERT INTO Subcatagory(subcatename,description,Pid) VALUES('llll','kkkkk',1)";<br />
            cmd.Parameters.Add("@subcatename", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;<br />
            cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;<br />
            cmd.Parameters.Add("@Pid", SqlDbType.Int).Value = Int32.Parse(Request.QueryString["Pid"]);        <br />
            cmd.Connection = con;<br />
            con.Open();<br />
            cmd.ExecuteNonQuery();<br />
            con.Close();<br />
<br />
        }<br />
        else<br />
        {<br />
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnectionString"].ConnectionString);<br />
            SqlCommand cmd = new SqlCommand();<br />
            cmd.CommandText = "UPDATE Subcatagory SET subcatename=@subcatename,description=@description WHERE subcat_id=@subcat_id";<br />
            cmd.Parameters.Add("@subcatename", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;<br />
            cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;<br />
            cmd.Parameters.Add("@subcat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);<br />
            cmd.Connection = con;<br />
            con.Open();<br />
            cmd.ExecuteNonQuery();<br />
            con.Close();<br />
}<br />
<br />
<br />
        GridView1.EditIndex = -1;<br />
        BindData();<br />
    }<br />
<br />
<br />
<br />

GeneralFormView Not Inserting Pin
Brady Kelly18-Mar-08 7:50
Brady Kelly18-Mar-08 7:50 
GeneralRe: FormView Not Inserting Pin
Brady Kelly18-Mar-08 8:40
Brady Kelly18-Mar-08 8:40 
GeneralCheck all - checkbox Pin
nour12318-Mar-08 6:53
nour12318-Mar-08 6:53 
GeneralRe: Check all - checkbox Pin
Not Active18-Mar-08 7:08
mentorNot Active18-Mar-08 7:08 
GeneralRe: Check all - checkbox Pin
nour12318-Mar-08 19:26
nour12318-Mar-08 19:26 
GeneralRe: Check all - checkbox Pin
pmartike18-Mar-08 20:36
pmartike18-Mar-08 20:36 
GeneralRe: Check all - checkbox Pin
Not Active19-Mar-08 1:16
mentorNot Active19-Mar-08 1:16 
GeneralRe: Check all - checkbox Pin
nour12319-Mar-08 4:02
nour12319-Mar-08 4:02 
Generalpattern and practices Pin
raquidd2218-Mar-08 6:30
raquidd2218-Mar-08 6:30 
GeneralRe: pattern and practices Pin
Not Active18-Mar-08 7:03
mentorNot Active18-Mar-08 7:03 
GeneralRe: pattern and practices Pin
raquidd2218-Mar-08 8:15
raquidd2218-Mar-08 8:15 
GeneralRedirecting to another page Pin
pmartike18-Mar-08 3:51
pmartike18-Mar-08 3:51 
GeneralRe: Redirecting to another page Pin
eyeseetee18-Mar-08 3:58
eyeseetee18-Mar-08 3:58 
GeneralRe: Redirecting to another page Pin
pmartike18-Mar-08 4:06
pmartike18-Mar-08 4:06 
GeneralRe: Redirecting to another page Pin
Not Active18-Mar-08 6:51
mentorNot Active18-Mar-08 6:51 
GeneralRe: Redirecting to another page Pin
pmartike18-Mar-08 20:29
pmartike18-Mar-08 20:29 
QuestionHow to Dispaly Records on runtime. Pin
raushan_918-Mar-08 3:45
raushan_918-Mar-08 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.