locked
How to save gridview in sql which is created in ASP.NET? RRS feed

  • Question

  • User1112163563 posted

    I am designing a project in asp.net. I one page i have created a gridview having template field as checkbox. When I select any row in gridview using checkbox,that checked row goes to a new gridview on button click. Now what i want to do is to save that new gridviewin sql. How to save that gridview ????
    Pls help.

    protected void Button1_Click2(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("id", typeof(int));
            dt.Columns.Add(dc);
            DataColumn dc1 = new DataColumn("name", typeof(string));
            dt.Columns.Add(dc1);
            DataColumn dc2 = new DataColumn("designation", typeof(string));
            dt.Columns.Add(dc2);
            DataColumn dc3 = new DataColumn("group_name", typeof(string));
            dt.Columns.Add(dc3);
            foreach (GridViewRow grow in GridView2.Rows)
            {
                CheckBox cbox = (CheckBox)grow.FindControl("cbox");
                if (cbox.Checked == true)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = grow.Cells[1].Text;
                    dr["name"] = grow.Cells[2].Text;
                    dr["designation"] = grow.Cells[3].Text;
                    dr["group_name"] = grow.Cells[4].Text;
    
                    dt.Rows.Add(dr);
                }
            }
            ds.Tables.Add(dt);
            GridView3.DataSource = ds;
            GridView3.DataBind();
    
        }
    Wednesday, November 2, 2011 6:46 AM

All replies

  • User1992938117 posted

    After DataBind do the code as:

     foreach (GridViewRow gvr in GridView2.Rows)   //loop through Grid
            {
                SqlConnection conn = new SqlConnection("your_connectionstring");
    
                SqlCommand cmd = new SqlCommand("insert into [Table](columnname1,columname2) values (" + gvr.Cells[0].Text + ",'" + gvr.Cells[2].Text + "')", conn);  
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
    Wednesday, November 2, 2011 6:54 AM
  • User1112163563 posted

    After executing this code,it is giving me an SQLexception "Incorrect syntax near SSE".

    How to remove this?????

    Wednesday, November 2, 2011 11:07 AM
  • User1992938117 posted

    After executing this code,it is giving me an SQLexception "Incorrect syntax near SSE".

    How to remove this?????

    Where?. Atleast show the code.

    Please!!!!!!!!

    Wednesday, November 2, 2011 2:04 PM
  • User1112163563 posted
    Pls check bold and underlined code. There i am geting SQLexception
    
    
    protected void Button1_Click2(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("id", typeof(int));
            dt.Columns.Add(dc);
            DataColumn dc1 = new DataColumn("name", typeof(string));
            dt.Columns.Add(dc1);
            DataColumn dc2 = new DataColumn("designation", typeof(string));
            dt.Columns.Add(dc2);
            DataColumn dc3 = new DataColumn("group_name", typeof(string));
            dt.Columns.Add(dc3);
            foreach (GridViewRow grow in GridView2.Rows)
            {
                CheckBox cbox = (CheckBox)grow.FindControl("cbox");
                if (cbox.Checked == true)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = grow.Cells[1].Text;
                    dr["name"] = grow.Cells[2].Text;
                    dr["designation"] = grow.Cells[3].Text;
                    dr["group_name"] = grow.Cells[4].Text;
    
                    dt.Rows.Add(dr);
                }
            }
            ds.Tables.Add(dt);
            GridView3.DataSource = ds;
            GridView3.DataBind();
    
            foreach (GridViewRow gvr in GridView2.Rows)
            {
                SqlConnection con = new SqlConnection("Data source=VISHWAS\\SQLEXPRESS1;database=project;integrated security=true");
                SqlCommand cmd = new SqlCommand("insert into team (employee_id,name,designation,group_name) values('" + gvr.Cells[0].Text + "','" + gvr.Cells[1].Text + "','" + gvr.Cells[2].Text + "','" + gvr.Cells[3].Text + "'", con);
                con.Open(); ///////Here i am getting exception ,on button click////
                cmd.ExecuteNonQuery();
                con.Close();
            }
    
        }
    
    Thursday, November 3, 2011 1:11 AM
  • User1992938117 posted

    con.Open(); ///////Here i am getting exception ,on button click////

    There is no text as SSE

    "Incorrect syntax near SSE"

    Be clear please what is the error

    Thursday, November 3, 2011 3:32 AM
  • User1112163563 posted

    https://picasaweb.google.com/114861356969445685971/November32011#5670675204173962594

    Just follow this link

    Thursday, November 3, 2011 3:57 AM
  • User1112163563 posted

    When i execute the program, select the rows from gridview using checkbox than click on button to get selected rows into a new gridview SQL Exception occurs. I also dont know what is "SSE".

    Just try to execute the code.

    Thursday, November 3, 2011 6:15 AM
  • User3866881 posted

    Hello Vishwasdeep:)

    I think there must be something wrong with your connection string. I suggest you changing to this:

     SqlConnection con = new SqlConnection("Server=VISHWAS\\SQLEXPRESS1;database=project;integrated security=true)

    Please make sure that your "Server"'s nickname for the real server is quite right. If you are not sure how the connection is, plz open VS and "View"=>"Server Explorer" to open your db and copy the connection string at the right-down panel at the property of "ConnectionString" when you switch the database.

    Of course you can also see the connection string at:

    http://www.connectionstrings.com/

    Thursday, November 3, 2011 9:32 PM
  • User1112163563 posted

    I am still not able to solve this thread. Pls help.

    Sunday, November 6, 2011 12:35 AM
  • User3866881 posted

    I am still not able to solve this thread. Pls help.

    Hello:)

    Sorry to hear that.... Please remove all the try……catch blocks and have a try again to test what exception have you got?

    If you can offer us your image of your screenshot, that's welcomed!

    Best reguards

    Sunday, November 6, 2011 1:57 AM
  • User1112163563 posted

    Just give me your email. i will send you screenshot.

    Tuesday, November 8, 2011 1:39 AM
  • User1992938117 posted

    Just give me your email. i will send you screenshot.

    better to post screenshot here.

    also if possible send us demo code and description that waht you want.

    Tuesday, November 8, 2011 1:50 AM