is there something wrong in my code? i want to show the value of row 8 in my gridview. i have this code and its not working out for me. No errors but it does not show the value i am expecting to see.
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
string strname = string.Empty;
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkRow");
if (chk != null & chk.Checked)
{
str += GridView1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
strname += gvrow.Cells[8].Text + ',';
}
}
strname = strname.Trim(",".ToCharArray());
lblRecord.Text = "<b>Credit Request: </b>" + strname;
}
tried to debug it. its not getting the cell value at all. what am i missing here?
UPDATE!:
Manged to make it work. its the masterpage cause why its not working. here is my code:
protected void btmDisplay_Click(object sender, EventArgs e)
{
//string data = "";
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
if (chkRow.Checked)
{
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand(@"UPDATE UserData SET CreditRequest = CAST(REPLACE(c.CreditRequest, ',', '') as int) FROM CreditRequests c INNER JOIN Userdata u on c.username=u.username Where c.Username=@Username", scn);
cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];
cmd.ExecuteNonQuery();
}
}
}
}
lblmsg.Text = "Approved";
What i want to happen now is when the checkbox is clicked, that row(specifically row8) will be updated on my sql database. i have tried the code above but its giving me an error:
The parameterized query '(@Username nvarchar(4000))UPDATE UserData SET CreditRequest = CA' expects the parameter '@Username', which was not supplied.
debug update:
UPDATE 2:
i tried having this
SqlCommand cmd = new SqlCommand("Update UserData set CreditRequest = '" + row.Cells[8].Text + "' where Username=@Username", scn);
cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = row.Cells[1].Text;
and my error is Conversion failed when converting the varchar value '5,000
use it
foreach (GridViewRow row in GridView1.Rows) { if (row.RowType == DataControlRowType.DataRow) { System.Web.UI.WebControls.CheckBox chkrow = (row.Cells[0].FindControl("chkrow") as System.Web.UI.WebControls.CheckBox); if (chkrow.Checked) { strname = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString(); } } }
If you are binding the gridview
on Page_Load
make sure you have set if(!IsPostBack)
if(!IsPostBack)
{
//BindGridView here
}
Try this
cmd.Parameters.AddWithValue("@Username", Session["New"].ToString());
UPdate
Remove single qoutes around the value of CreditRequest
SqlCommand cmd = new SqlCommand("Update UserData set CreditRequest = " + row.Cells[8].Text + " where Username=@Username", scn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.