Why am I getting incorrect syntax near `username`? - asp.net

public partial class adduser : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into login values username = #un,password = #ps, sequirity_que = #sq, ans = #answer,usertype = #ustp ");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
cmd.Connection = con;
if (con.State != System.Data.ConnectionState.Open)
con.Open();
string un = txtusername.Text.Trim();
string ps = txtpass.Text.Trim();
string sq = txtseq.Text.Trim();
string answer = txtans.Text.Trim();
string ustp = DropDownList1.SelectedValue;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("un", un);
cmd.Parameters.AddWithValue("ps", ps);
cmd.Parameters.AddWithValue("sq", sq);
cmd.Parameters.AddWithValue("answer", answer);
cmd.Parameters.AddWithValue("ustp", ustp);
//string qry = "insert into login values (username = '" + txtusername.Text + "',password='" + txtpass.Text + "',sequirity_que='" + txtseq.Text + "',ans='" + txtans.Text + "',usertype='" + DropDownList1.SelectedValue + "') ";
//SqlCommand md = new SqlCommand(qry,con);
int n= cmd.ExecuteNonQuery();
if(n>0)
{
Response.Write("User added");
}
else
{
Response.Write("fail");
}
}
}

This is the correct syntax of INSERT INTO:
SqlCommand cmd = new SqlCommand("insert into login (username, password, sequirity_que, ans, usertype) values (#un, #ps, #sq, #answer, #ustp)");
Some good examples

Related

An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code

I'm getting the above error in this code`
protected void btnsubmit_Click(object sender, EventArgs e)
{
string veturaid = Request.QueryString["VetID"].ToString();
int vetuid = int.Parse(veturaid);
SqlConnection con = new SqlConnection("Data Source=DESKTOP-SUV91Q2\\AGONAJREDINI;Initial Catalog=VeturaOnline;Integrated Security=True; MultipleActiveResultSets=true ");
con.Open();
SqlCommand cmd = new SqlCommand("Select PerdID from Perdoruesit where Username='" + btnuser.Text + "'", con);
SqlDataReader reader = cmd.ExecuteReader();
string str = "";
while (reader.Read())
{
str = (reader["PerdID"].ToString());
id = int.Parse(str);
}
SqlCommand cmd2 = new SqlCommand("Select * from Veturat where VetID='" + vetuid + "'", con);
SqlDataReader reader1 = cmd2.ExecuteReader();
string modeli = "";
while (reader1.Read())
{
modeli = (reader["Modeli"].ToString());
}
reader1.Close();`
I'm getting the error at this line modeli = (reader["Modeli"].ToString());
Any idea why it is happening? Thank you in advance.

Return ID in asp.net

I want to return userid against username and password
e.g.
1 abc a
11 def ab
when I enter abc in username textbox and a in password textbox then want to return 1 in label how to modify this code
protected void Button4_Click(object sender, EventArgs e)
{
string query = #"Data Source=HOME\SQL;Initial Catalog=The_Coffe;Integrated Security=True";
SqlConnection con = new SqlConnection(query);
con.Open();
string b = "Select C_ID from Customer_Register where Email='" + TextBox1.Text + "' AND password='" + TextBox2.Text + "'";
SqlCommand cam = new SqlCommand(b, con);
cam.Parameters.AddWithValue("#Email", TextBox1.Text);
cam.Parameters.AddWithValue("#password", TextBox2.Text);
cam.ExecuteNonQuery();
SqlDataReader dr = cam.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
TextBox1.Text = Convert.ToString(dr["Email"]);
TextBox2.Text = Convert.ToString(dr["password"]);
Session["UserName"] = TextBox1.Text;
Session["UserID"] = ID.Text;
//Response.Redirect("Fronpa.aspx");
}
}
else
{
Label4.Visible = true;
Label4.Text = "Incorrect Email or Password..";
}
con.Close();
}
For Returning UserId in the above code, you can use
Label1.Text = dr["id"].toString(); inside the while (dr.Read()) code block

RowUpdating Asp.net

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DB_Function.OpenConniction();
string ExpID = GridView2.DataKeys[e.RowIndex].Value.ToString();
string CompNm = ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string JobTitel = ((TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string WorkPlace = ((TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string StartDate = ((TextBox)GridView2.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string EndDate = ((TextBox)GridView2.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string ReasonLeave = ((TextBox)GridView2.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
string CompPhone = ((TextBox)GridView2.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
if(DB_Function.UpdateExperiance(ExpID)>0)
{
GridView1.EditIndex = -1;
lblState.Text = " Row is Updated ";
lblState.ForeColor = System.Drawing.Color.Blue;
}
}
public static int UpdateExperiance(string ExpID , string CompNm , string JobTitel , string WorkPlace , string StartDate , string EndDate , string ReasonLeave , string CompPhone)
{
string Strcon = #"Data Source=MAHMOD-PC\SQLEXPRESS;Initial Catalog=Task;Integrated Security=True";
SqlConnection con = new SqlConnection(Strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_U_Experiances";
cmd.Parameters.AddWithValue("#ExpID", ExpID);
cmd.Parameters.AddWithValue("#CompNm", CompNm);
cmd.Parameters.AddWithValue("#JobTitel", JobTitel);
cmd.Parameters.AddWithValue("#WorkPlace", WorkPlace);
cmd.Parameters.AddWithValue("#StartDate", StartDate);
cmd.Parameters.AddWithValue("#EndDate", EndDate);
cmd.Parameters.AddWithValue("#ReasonLeave", ReasonLeave);
cmd.Parameters.AddWithValue("#CompPhone", CompPhone);
con.Open();
return cmd.ExecuteNonQuery();
}
You aren't rebinding your data after you do the database update.
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DB_Function.OpenConniction();
string ExpID = GridView2.DataKeys[e.RowIndex].Value.ToString();
string CompNm = ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string JobTitel = ((TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string WorkPlace = ((TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string StartDate = ((TextBox)GridView2.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string EndDate = ((TextBox)GridView2.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string ReasonLeave = ((TextBox)GridView2.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
string CompPhone = ((TextBox)GridView2.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
if(DB_Function.UpdateExperiance(ExpID)>0)
{
GridView1.EditIndex = -1;
lblState.Text = " Row is Updated ";
lblState.ForeColor = System.Drawing.Color.Blue;
BindData();
}
}
Where BindData is where you retrieve your data:
private void BindData()
{
GridView2.DataSource = (your data source);
GridView2.DataBind();
}

Pop up if text box is blank at update

I have a web form that uses a text box to update a comments field in the database. I would like to check for a blank before update and have a reminder box that says "Comment section blank, continue?", then have a yes / no option. This would allow the user to return to the update without making changes. The text box is visible when the user selects edit on the row in a gridview. I will provide code below so that anyone can see what I am doing. If this cannot be done this way, simply a message box saying that the comment section is blank would suffice.
Here is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;
namespace AnnoTracker
{
public partial class WebForm1 : System.Web.UI.Page
{
public static class MyVariables
{
public static int PI = 0;
public static string JN = "";
public static string ST = "";
public static string AT = "";
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindData();
dlAnnoType.SelectedValue = "Agency Error";
}
}
protected void EditSummary(object sender, GridViewEditEventArgs e)
{
gvSummary.EditIndex = e.NewEditIndex;
string _custName = gvSummary.DataKeys[e.NewEditIndex].Value.ToString();
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
gvSummary.EditIndex = -1;
BindData();
}
protected void gvSummary_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvSummary.PageIndex = e.NewPageIndex;
MyVariables.PI = e.NewPageIndex;
BindData();
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
{
DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");
string _custName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
string BUquery = "select distinct Unit from vw_BU where Business='" + _custName + "'";
SqlCommand BUcmd = new SqlCommand(BUquery);
dlBU.DataSource = GetData(BUcmd);
dlBU.DataTextField = "Unit";
dlBU.DataValueField = "Unit";
dlBU.DataBind();
dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;
DropDownList dlPA = (DropDownList)e.Row.FindControl("dlPA");
string _PAcustName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
string PAquery = "select PA from PA where Business='" + _PAcustName + "' order by PA";
SqlCommand PAcmd = new SqlCommand(PAquery);
dlPA.DataSource = GetData(PAcmd);
dlPA.DataTextField = "PA";
dlPA.DataValueField = "PA";
dlPA.DataBind();
dlPA.Items.FindByValue((e.Row.FindControl("lblPA") as Label).Text).Selected = true;
DropDownList dlET = (DropDownList)e.Row.FindControl("dlET");
string ETquery = "select distinct ErrorType from ErrorType order by ErrorType";
SqlCommand ETcmd = new SqlCommand(ETquery);
dlET.DataSource = GetData(ETcmd);
dlET.DataTextField = "ErrorType";
dlET.DataValueField = "ErrorType";
dlET.DataBind();
dlET.Items.FindByValue((e.Row.FindControl("lblET") as Label).Text).Selected = true;
DropDownList dlAA = (DropDownList)e.Row.FindControl("dlAA");
string AAquery = "select distinct AAA from ActualAgencyError";
SqlCommand AAcmd = new SqlCommand(AAquery);
dlAA.DataSource = GetData(AAcmd);
dlAA.DataTextField = "AAA";
dlAA.DataValueField = "AAA";
dlAA.DataBind();
dlAA.Items.FindByValue((e.Row.FindControl("lblAA") as Label).Text).Selected = true;
}
}
protected void UpdateSummary(object sender, GridViewUpdateEventArgs e)
{
string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
string ET = (gvSummary.Rows[e.RowIndex].FindControl("dlET") as DropDownList).SelectedItem.Value;
string AA = (gvSummary.Rows[e.RowIndex].FindControl("dlAA") as DropDownList).SelectedItem.Value;
string PA = (gvSummary.Rows[e.RowIndex].FindControl("dlPA") as DropDownList).SelectedValue;
string AnnotationNumber = gvSummary.DataKeys[e.RowIndex].Value.ToString();
string sgkComments = (gvSummary.Rows[e.RowIndex].FindControl("tbsgkComm") as TextBox).Text;
string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
string query = "update vw_GridviewSource set [BusinessUnit] = #BU, [ErrorType] = #ET, [sgkComments] = #sgk, [ActualAgencyError] = #AA, [PA] = #PA where [AnnotationNumber] = #AnnoNum";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#BU", BU);
cmd.Parameters.AddWithValue("#AnnoNum", AnnotationNumber);
cmd.Parameters.AddWithValue("#ET", ET);
cmd.Parameters.AddWithValue("#AA", AA);
cmd.Parameters.AddWithValue("#sgk", sgkComments);
cmd.Parameters.AddWithValue("#PA", PA);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
//gvSummary.DataBind();
BindData();
}
private void BindData()
{
if (MyVariables.JN != "" && MyVariables.ST != "" && MyVariables.AT != "")
{
dlJobName.SelectedValue = MyVariables.JN;
dlJobName.DataBind();
dlStage.SelectedValue = MyVariables.ST;
dlStage.DataBind();
dlAnnoType.SelectedValue = MyVariables.AT;
dlAnnoType.DataBind();
}
String conString = System.Configuration.ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
string query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource order by [Page_ID]";
SqlCommand cmd = new SqlCommand();
if (dlJobName.SelectedValue != "" & dlStage.SelectedValue != "")
{
query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
//cmd.Parameters.AddWithValue("#Name", dlJobName.SelectedValue);
//cmd.Parameters.AddWithValue("#Stage", dlStage.SelectedValue);
}
if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue != "" && dlStage.SelectedValue != ""))
{
query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' and Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
}
if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue.Length < 2 && dlStage.SelectedValue.Length < 2) )
{
query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' order by [Page_ID]";
}
cmd.CommandText = query;
SqlConnection con = new SqlConnection(conString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
gvSummary.DataSource = ds;
gvSummary.PageIndex = MyVariables.PI;
gvSummary.DataBind();
//string #Name;
//string #Stage;
//#Name = dlJobName.SelectedValue;
//#Stage = dlStage.SelectedValue;
//string query = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where Name = '" + #Name + "' and AnnotationDate = '" + #Stage + "'";
//SqlCommand cmd = new SqlCommand(query);
//gvSummary.DataSource = GetData(cmd);
//gvSummary.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void dlJobName_SelectedIndexChanged(object sender, EventArgs e)
{
MyVariables.JN = dlJobName.SelectedValue;
dlStage.DataBind();
MyVariables.ST = dlStage.SelectedValue;
gvSummary.DataBind();
BindData();
}
protected void dlStage_SelectedIndexChanged(object sender, EventArgs e)
{
MyVariables.JN = dlJobName.SelectedValue;
//dlStage.DataBind();
MyVariables.ST = dlStage.SelectedValue;
MyVariables.AT = dlAnnoType.SelectedValue;
gvSummary.DataBind();
BindData();
}
protected void dlAnnoType_SelectedIndexChanged(object sender, EventArgs e)
{
MyVariables.AT = dlAnnoType.SelectedValue;
MyVariables.JN = dlJobName.SelectedValue;
//dlStage.DataBind();
MyVariables.ST = dlStage.SelectedValue;
gvSummary.DataBind();
BindData();
}
}
}
You show only server side code, and it's more complicated doing chek on server side and show message on client.
Simple sample of chek on client side by javascript:
<script type="text/javascript">
function MyConfirm() {
if (document.getElementById("tb1").value == '')
if (confirm("Comment section blank, continue?"))
return true;
else
return false;
else true;
}
</script>
<asp:TextBox runat="server" ID="tb1" />
<asp:Button runat="server" ID="btn1" onclick="btn1_Click" OnClientClick="return MyConfirm();" Text="Save" />

Save view state data into database Error

i have a problem when put in Response.Redirect
Example, the data only record for the first key in, for all the rest data no record in DB.
Secondly, the purpose i use Response.Redirect is to refresh the webpage, any idea to clear data after insert data into DB ?
kindly advise. thank you.
protected void Page_Load(object sender, EventArgs e)
{
string stat = Request.QueryString["stat"];
if (stat == "insert")
{
StatLabel.Text = "New Record have been Insert";
}
}
private void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Test1", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Test2", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Test3", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
TextBox1.Focus();
TextBox2.Focus();
TextBox3.Focus();
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow oItem in GridView1.Rows)
{
string str1 = oItem.Cells[0].Text;
string str2 = oItem.Cells[1].Text;
string str3 = oItem.Cells[2].Text;
insertData(str1, str2, str3);
}
}
public void insertData(string str1,string str2,string str3)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
Response.Redirect("WebForm1.aspx?stat=insert");
}
}
}
When you are saving into the database, you redirect right after each insert. Hence, none of the subsequent inserts are executed. I would move the redirect out of the loop. Maybe you can pass the number of records inserted in the query string as well.
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow oItem in GridView1.Rows)
{
string str1 = oItem.Cells[0].Text;
string str2 = oItem.Cells[1].Text;
string str3 = oItem.Cells[2].Text;
insertData(str1, str2, str3);
}
Response.Redirect("WebForm1.aspx?stat=insert");
}
public void insertData(string str1,string str2,string str3)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
}
I see your code i have find
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow oItem in GridView1.Rows)
{
string str1 = oItem.Cells[0].Text;
string str2 = oItem.Cells[1].Text;
string str3 = oItem.Cells[2].Text;
insertData(str1, str2, str3);
}
}
public void insertData(string str1,string str2,string str3)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
Response.Redirect("WebForm1.aspx?stat=insert");
}
in this on Button2_Click in for loop after first time it will redirect page so now Your method will be like this
Replace this Method
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow oItem in GridView1.Rows)
{
string str1 = oItem.Cells[0].Text;
string str2 = oItem.Cells[1].Text;
string str3 = oItem.Cells[2].Text;
insertData(str1, str2, str3);
}
Response.Redirect("WebForm1.aspx?stat=insert");
}
public void insertData(string str1,string str2,string str3)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
}

Resources