Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Monday, March 26, 2012

Newbie : Updating problem with Datagrid

Hi,

I have a page with a datagrid control. What I would like to do is when
I click on the update statement, I will update to the datagrid. However
the problem I am facing currently is when I click on update, the value
which I got from the controls generated by the datagrid edit template
is empty. Is there something I am missing out on? I have attached the
source codes below, please advice. Thanks!

Web User Control :

<asp:DataGrid id="participateGrid" runat="server"
AutoGenerateColumns="False" CellPadding="1" CellSpacing="1"
Width="100%" BorderColor="White" BorderStyle="None"
OnEditCommand="Edit" OnDeleteCommand="Delete" OnUpdateCommand="Update"
ShowHeader="True">
<HeaderStyle BackColor="#dedfde" CssClass="tableLabel"></HeaderStyle>
<ItemStyle CssClass="tableItem" BackColor="#f7e7e7"></ItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="Outlet">
<EditItemTemplate>
<asp:DropDownList ID="ddlOutlet" Runat="server">
<asp:ListItem>...</asp:ListItem>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox id="txtRemarks" TextMode="MultiLine" Rows="2"
Columns="15" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="<img
src='editimage.jpg'>" UpdateText="<img src='updateimage.gif'>"
</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
Text="<img src='delete.gif'"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<asp:ImageButton ImageUrl="image.gif" id="btn_new"
runat="server"></asp:ImageButton
This is the codebehind file update method :

protected void Update (object sender, DataGridCommandEventArgs e)
{
DropDownList outlet = (DropDownList)e.Item.Cells[0].FindControl
("ddlOutlet");
TextBox remarks = ((TextBox)e.Item.Cells[1].FindControl
("txtRemarks"));

DataRow dr = dt.Rows[e.Item.DataSetIndex];

dr["Outlet"] = outlet.SelectedItem.Text;
dr["Remarks"] = remarks.Text;

saveData();

participateGrid.EditItemIndex = -1;
BindGrid();
}

private void saveData ()
{
//stores the information in a viewstate
ViewState ["Participate"] = dt;
}

//bind the data
private void BindGrid()
{
participateGrid.DataSource = dv;
participateGrid.DataBind();
}Are u binding your datagrid on page load? Page load event occurs before
datagrid update event.
If your answer is yes, control postback on your page load something like
this:

if (!IsPostBack)
{
BindGrid();
}

--
Thanks,
Yunus Emre ALPZEN

<ycquak@.gmail.com> wrote in message
news:1107496691.787654.33590@.c13g2000cwb.googlegro ups.com...
> Hi,
> I have a page with a datagrid control. What I would like to do is when
> I click on the update statement, I will update to the datagrid. However
> the problem I am facing currently is when I click on update, the value
> which I got from the controls generated by the datagrid edit template
> is empty. Is there something I am missing out on? I have attached the
> source codes below, please advice. Thanks!
> Web User Control :
> <asp:DataGrid id="participateGrid" runat="server"
> AutoGenerateColumns="False" CellPadding="1" CellSpacing="1"
> Width="100%" BorderColor="White" BorderStyle="None"
> OnEditCommand="Edit" OnDeleteCommand="Delete" OnUpdateCommand="Update"
> ShowHeader="True">
> <HeaderStyle BackColor="#dedfde" CssClass="tableLabel"></HeaderStyle>
> <ItemStyle CssClass="tableItem" BackColor="#f7e7e7"></ItemStyle>
> <Columns>
> <asp:TemplateColumn HeaderText="Outlet">
> <EditItemTemplate>
> <asp:DropDownList ID="ddlOutlet" Runat="server">
> <asp:ListItem>...</asp:ListItem>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Remarks">
> <EditItemTemplate>
> <asp:TextBox id="txtRemarks" TextMode="MultiLine" Rows="2"
> Columns="15" Runat="server"></asp:TextBox>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:EditCommandColumn ButtonType="LinkButton" EditText="<img
> src='editimage.jpg'>" UpdateText="<img src='updateimage.gif'>">
> </asp:EditCommandColumn>
> <asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
> Text="<img src='delete.gif'"></asp:ButtonColumn>
> </Columns>
> </asp:DataGrid>
> <asp:ImageButton ImageUrl="image.gif" id="btn_new"
> runat="server"></asp:ImageButton>
>
> This is the codebehind file update method :
> protected void Update (object sender, DataGridCommandEventArgs e)
> {
> DropDownList outlet = (DropDownList)e.Item.Cells[0].FindControl
> ("ddlOutlet");
> TextBox remarks = ((TextBox)e.Item.Cells[1].FindControl
> ("txtRemarks"));
> DataRow dr = dt.Rows[e.Item.DataSetIndex];
> dr["Outlet"] = outlet.SelectedItem.Text;
> dr["Remarks"] = remarks.Text;
> saveData();
> participateGrid.EditItemIndex = -1;
> BindGrid();
> }
> private void saveData ()
> {
> //stores the information in a viewstate
> ViewState ["Participate"] = dt;
> }
> //bind the data
> private void BindGrid()
> {
> participateGrid.DataSource = dv;
> participateGrid.DataBind();
> }
Below is my page load event. I suspect something is wrong with the
loadData method, is it?

private void Page_Load(object sender, System.EventArgs e)
{

if (ViewState ["Participate"] == null)
{
loadData();
ViewState["Participate"] = dt;
}

else
{
dt = (DataTable)ViewState["Participate"];
dv = new DataView (dt);
}

if (!IsPostBack)
{
Session ["ParticipateCounter"] = gridCounter;
}
else
{
gridCounter = (int)Session ["ParticipateCounter"];
}

BindGrid();
}

//page load event

//initialise the local objects
private void loadData ()
{
dt = new DataTable();
dt.Columns.Add (new DataColumn ("Outlet", typeof(string)));
dt.Columns.Add (new DataColumn ("Remarks", typeof(string)));

dv = new DataView(dt);

}
As I said, put BindGrid method into if (!IsPostBack)

--
Thanks,
Yunus Emre ALPZEN

"yew chong" <ycquak@.gmail.com> wrote in message
news:1107505653.693376.107750@.z14g2000cwz.googlegr oups.com...
> Below is my page load event. I suspect something is wrong with the
> loadData method, is it?
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (ViewState ["Participate"] == null)
> {
> loadData();
> ViewState["Participate"] = dt;
> }
> else
> {
> dt = (DataTable)ViewState["Participate"];
> dv = new DataView (dt);
> }
> if (!IsPostBack)
> {
> Session ["ParticipateCounter"] = gridCounter;
> }
> else
> {
> gridCounter = (int)Session ["ParticipateCounter"];
> }
> BindGrid();
> }
> //page load event
> //initialise the local objects
> private void loadData ()
> {
> dt = new DataTable();
> dt.Columns.Add (new DataColumn ("Outlet", typeof(string)));
> dt.Columns.Add (new DataColumn ("Remarks", typeof(string)));
> dv = new DataView(dt);
> }
yep, got it! Thanks! Managed to get the right values.
Hmm, maybe would just like to ask another question. Why is it that when
I click on update, after posting back, I still get the edit box?

I have set my EditItemIndex to -1 already. Why is it not being set to
-1? Thanks!

Regards
Yew Chong
Surely it caused by event order. Everytime event order is as follows.
1. Page_Load
2. Click events
3. Changed events
Every time the page is posted client. Firstly your datagrid is binded then
events like datagrid events are handled. So edit item index is set after the
datagrid is binded.
--
Thanks,
Yunus Emre ALPZEN

"yew chong" <ycquak@.gmail.com> wrote in message
news:1107571021.594077.69610@.l41g2000cwc.googlegro ups.com...
> yep, got it! Thanks! Managed to get the right values.
> Hmm, maybe would just like to ask another question. Why is it that when
> I click on update, after posting back, I still get the edit box?
> I have set my EditItemIndex to -1 already. Why is it not being set to
> -1? Thanks!
> Regards
> Yew Chong
Thanks a lot! I got this working as well.
Really thanks for the time taken to answer my questions.

Newbie : Updating problem with Datagrid

Hi,
I have a page with a datagrid control. What I would like to do is when
I click on the update statement, I will update to the datagrid. However
the problem I am facing currently is when I click on update, the value
which I got from the controls generated by the datagrid edit template
is empty. Is there something I am missing out on? I have attached the
source codes below, please advice. Thanks!
Web User Control :
<asp:DataGrid id="participateGrid" runat="server"
AutoGenerateColumns="False" CellPadding="1" CellSpacing="1"
Width="100%" BorderColor="White" BorderStyle="None"
OnEditCommand="Edit" OnDeleteCommand="Delete" OnUpdateCommand="Update"
ShowHeader="True">
<HeaderStyle BackColor="#dedfde" CssClass="tableLabel"></HeaderStyle>
<ItemStyle CssClass="tableItem" BackColor="#f7e7e7"></ItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="Outlet">
<EditItemTemplate>
<asp:DropDownList ID="ddlOutlet" Runat="server">
<asp:ListItem>...</asp:ListItem>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox id="txtRemarks" TextMode="MultiLine" Rows="2"
Columns="15" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="<img
src='editimage.jpg'>" UpdateText="<img src='updateimage.gif'>">
</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
Text="<img src='delete.gif'"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<asp:ImageButton ImageUrl="image.gif" id="btn_new"
runat="server"></asp:ImageButton>
This is the codebehind file update method :
protected void Update (object sender, DataGridCommandEventArgs e)
{
DropDownList outlet = (DropDownList)e.Item.Cells[0].FindControl
("ddlOutlet");
TextBox remarks = ((TextBox)e.Item.Cells[1].FindControl
("txtRemarks"));
DataRow dr = dt.Rows[e.Item.DataSetIndex];
dr["Outlet"] = outlet.SelectedItem.Text;
dr["Remarks"] = remarks.Text;
saveData();
participateGrid.EditItemIndex = -1;
BindGrid();
}
private void saveData ()
{
//stores the information in a viewstate
ViewState ["Participate"] = dt;
}
//bind the data
private void BindGrid()
{
participateGrid.DataSource = dv;
participateGrid.DataBind();
}Are u binding your datagrid on page load? Page load event occurs before
datagrid update event.
If your answer is yes, control postback on your page load something like
this:
if (!IsPostBack)
{
BindGrid();
}
Thanks,
Yunus Emre ALPZEN
<ycquak@.gmail.com> wrote in message
news:1107496691.787654.33590@.c13g2000cwb.googlegroups.com...
> Hi,
> I have a page with a datagrid control. What I would like to do is when
> I click on the update statement, I will update to the datagrid. However
> the problem I am facing currently is when I click on update, the value
> which I got from the controls generated by the datagrid edit template
> is empty. Is there something I am missing out on? I have attached the
> source codes below, please advice. Thanks!
> Web User Control :
> <asp:DataGrid id="participateGrid" runat="server"
> AutoGenerateColumns="False" CellPadding="1" CellSpacing="1"
> Width="100%" BorderColor="White" BorderStyle="None"
> OnEditCommand="Edit" OnDeleteCommand="Delete" OnUpdateCommand="Update"
> ShowHeader="True">
> <HeaderStyle BackColor="#dedfde" CssClass="tableLabel"></HeaderStyle>
> <ItemStyle CssClass="tableItem" BackColor="#f7e7e7"></ItemStyle>
> <Columns>
> <asp:TemplateColumn HeaderText="Outlet">
> <EditItemTemplate>
> <asp:DropDownList ID="ddlOutlet" Runat="server">
> <asp:ListItem>...</asp:ListItem>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Remarks">
> <EditItemTemplate>
> <asp:TextBox id="txtRemarks" TextMode="MultiLine" Rows="2"
> Columns="15" Runat="server"></asp:TextBox>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:EditCommandColumn ButtonType="LinkButton" EditText="<img
> src='editimage.jpg'>" UpdateText="<img src='updateimage.gif'>">
> </asp:EditCommandColumn>
> <asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
> Text="<img src='delete.gif'"></asp:ButtonColumn>
> </Columns>
> </asp:DataGrid>
> <asp:ImageButton ImageUrl="image.gif" id="btn_new"
> runat="server"></asp:ImageButton>
>
> This is the codebehind file update method :
> protected void Update (object sender, DataGridCommandEventArgs e)
> {
> DropDownList outlet = (DropDownList)e.Item.Cells[0].FindControl
> ("ddlOutlet");
> TextBox remarks = ((TextBox)e.Item.Cells[1].FindControl
> ("txtRemarks"));
> DataRow dr = dt.Rows[e.Item.DataSetIndex];
> dr["Outlet"] = outlet.SelectedItem.Text;
> dr["Remarks"] = remarks.Text;
> saveData();
> participateGrid.EditItemIndex = -1;
> BindGrid();
> }
> private void saveData ()
> {
> //stores the information in a viewstate
> ViewState ["Participate"] = dt;
> }
> //bind the data
> private void BindGrid()
> {
> participateGrid.DataSource = dv;
> participateGrid.DataBind();
> }
>
Below is my page load event. I suspect something is wrong with the
loadData method, is it?
private void Page_Load(object sender, System.EventArgs e)
{
if (ViewState ["Participate"] == null)
{
loadData();
ViewState["Participate"] = dt;
}
else
{
dt = (DataTable)ViewState["Participate"];
dv = new DataView (dt);
}
if (!IsPostBack)
{
Session ["ParticipateCounter"] = gridCounter;
}
else
{
gridCounter = (int)Session ["ParticipateCounter"];
}
BindGrid();
}
//page load event
//initialise the local objects
private void loadData ()
{
dt = new DataTable();
dt.Columns.Add (new DataColumn ("Outlet", typeof(string)));
dt.Columns.Add (new DataColumn ("Remarks", typeof(string)));
dv = new DataView(dt);
}
As I said, put BindGrid method into if (!IsPostBack)
Thanks,
Yunus Emre ALPZEN
"yew chong" <ycquak@.gmail.com> wrote in message
news:1107505653.693376.107750@.z14g2000cwz.googlegroups.com...
> Below is my page load event. I suspect something is wrong with the
> loadData method, is it?
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (ViewState ["Participate"] == null)
> {
> loadData();
> ViewState["Participate"] = dt;
> }
> else
> {
> dt = (DataTable)ViewState["Participate"];
> dv = new DataView (dt);
> }
> if (!IsPostBack)
> {
> Session ["ParticipateCounter"] = gridCounter;
> }
> else
> {
> gridCounter = (int)Session ["ParticipateCounter"];
> }
> BindGrid();
> }
> //page load event
> //initialise the local objects
> private void loadData ()
> {
> dt = new DataTable();
> dt.Columns.Add (new DataColumn ("Outlet", typeof(string)));
> dt.Columns.Add (new DataColumn ("Remarks", typeof(string)));
> dv = new DataView(dt);
> }
>
yep, got it! Thanks! Managed to get the right values.
Hmm, maybe would just like to ask another question. Why is it that when
I click on update, after posting back, I still get the edit box?
I have set my EditItemIndex to -1 already. Why is it not being set to
-1? Thanks!
Regards
Yew Chong
Surely it caused by event order. Everytime event order is as follows.
1. Page_Load
2. Click events
3. Changed events
Every time the page is posted client. Firstly your datagrid is binded then
events like datagrid events are handled. So edit item index is set after the
datagrid is binded.
--
Thanks,
Yunus Emre ALPZEN
"yew chong" <ycquak@.gmail.com> wrote in message
news:1107571021.594077.69610@.l41g2000cwc.googlegroups.com...
> yep, got it! Thanks! Managed to get the right values.
> Hmm, maybe would just like to ask another question. Why is it that when
> I click on update, after posting back, I still get the edit box?
> I have set my EditItemIndex to -1 already. Why is it not being set to
> -1? Thanks!
> Regards
> Yew Chong
>
Thanks a lot! I got this working as well.
Really thanks for the time taken to answer my questions.

Friday, March 16, 2012

Newbie needs quick help

I need to update my database. I receive the Id in a url string:
https://128bit.clickandpledge.com/Shopping/BNewUser.aspx?I=344
Where ' I ' in the url is the following: 34 would be the id of the record in the database and the last number 4 would be the number I need to subtract from the total available tickets in the database.
So here is how it goes. A user goes to the site, they want to purchase 4 tickets. They enter in 4 in the input the box and off to the payment processor. I do not collect any information on the website itself. After processing the database record ID and the purhased tickets are joined together as one number in the url. I need to parse the first two numbers and use in my where clause and the last number I need to subtract from the available field in the database.
I have no idea how to do this. I know it has to be simple. Help
tonyDim stringI as string
stringI = Request.QueryString("I")
Dim intRec as Integer
intRec = CInt(Left(I,2))
Dim intQty as Integer
intQty = CInt(Right(I,1))

You really should sit at the drawing board a little longer. What happens when you hit record number 99? What happened prior to record 10?
If you insist on using a query string then you should probably use 2 parameters. Ie.,http://www.yoursite.com?I=34&J=4
That saves you the trouble of concatenating them in the first place.
Then the code is simple
int Record = Convert.ToInt16(Request.QueryString["I"]);
int Tickets = Convert.ToInt16(Request.QueryString["J"]);

just my 2 cents.