calling a web service in asp.net - c#

I have two textbox and a button, and a web service. I want to enter a number(cu_personal_number) in textbox1 and when I click the button, it connects to web service and search in it and return the name belong to that cu_personal_number in the textbox2.
my code did not return value. When I enter a code in textbox1 and enter the button, there is not any value appear in the textbox2.
protected void Button3_Click(object sender, EventArgs e)
{
localhost.Personel lp1 = new localhost.Personel();
DataSet ds1 = lp1.daryafte_view_Personel_Information_with_parameter_cu_personal_number("cu_personal_number");
this.GridView1.DataSource = ds1;
bool found = false;
foreach (System.Data.DataTable tabale in ds1.Tables)
{
foreach (DataRow dr in tabale.Rows)
{
found = true;
txtName.Text = dr["tp_firstabbr_name"].ToString().Trim();
}
}

Check what are the Values your "tabale" return, if there is single row as result then there is no need of Foreach Loop.
if Table has single row the use below to get value.
txtName.value = tabale.rows[0]["tp_firstabbr_name"].tostring()

Related

Textbox losing value after another buttons postback

On my page I have 3 textboxes that hold values for Title, Description, Tips and keywords. When I click on a button it inserts the values into the database. When it posts back, the values are staying in the textboxes, and this is what I want.
The next part of the page has textboxes for Question, CorrectAnswer, Wrong1, Wrong2, Wrong3. When I click on the button to insert them into the database, that works, and after the button fires its event I have those 5 textboxes have a text value of null, so I can continue on adding the question and answers.
But when that button causes its postback, the values in the first textboxes disappear, and I don't want that because I have validation on the title textbox, because you can't add any questions and answers without the title in the textbox.
So how do I keep the values in the first textboxes when the second button causes a postback?
Here is the code for the two buttons, and the btnAddQandA is the button that causes a postback..
protected void btnAddQuizTitle_Click(object sender, EventArgs e)
{
daccess.AddQuizName(tbTitle.Text, taDescription.InnerText, taTips.InnerText, tbKeywords.Text);
Session["TheQuizID"] = daccess.TheID;
string myID = (string)(Session["TheQuizID"]);
int theID = Int32.Parse(myID);
if (tbKeywords.Text != null)
{
string TheKeywordHolder = "";
foreach (ListItem LI in cblGrades.Items)
{
if (LI.Selected == true)
{
TheKeywordHolder = TheKeywordHolder + LI.Value + ",";
}
}
daccess.AddQuizKeywords(theID, tbKeywords.Text);
}
}
protected void btnAddQA_Click(object sender, EventArgs e)
{
int theID = (int)Session["TheQuizID"];
daccess.AddQuizQA(tbQuestion.Text, tbCorrect.Text, tbWrong1.Text, tbWrong2.Text, tbWrong3.Text, theID);
tbQuestion.Text = null;
tbCorrect.Text = null;
tbWrong1.Text = null;
tbWrong2.Text = null;
tbWrong3.Text = null;
}
and here is my pageload event
DataAccess daccess = new DataAccess();
protected void Page_Load(object sender, EventArgs e)
{
daccess.CblGradesDS();
cblGrades.DataSource = daccess.DsCbl;
cblGrades.DataValueField = "GradeID";
cblGrades.DataTextField = "Grade";
cblGrades.RepeatColumns = 8;
cblGrades.RepeatDirection = RepeatDirection.Horizontal;
cblGrades.DataBind();
daccess.CblSubjectsDS();
cblSubjects.DataSource = daccess.DsCbl2;
cblSubjects.DataValueField = "SubjectID";
cblSubjects.DataTextField = "SubjectName";
cblSubjects.RepeatColumns = 4;
cblSubjects.RepeatDirection = RepeatDirection.Horizontal;
cblSubjects.DataBind();
if (!IsPostBack)
{
}
}
These issues are usually caused by control IDs (UniqueIDs to be specific) that do not match before and after the postback. This causes inability of finding values in viewstate. This happens when you modify the controls collection (typically in codebehind) or when you change controls' visibility. It may also happen when you modify IDs before Page_Load event when viewstate is not yet populated. It's good to know how ASP.NET lifecycle works. http://spazzarama.com/wp-content/uploads/2009/02/aspnet_page-control-life-cycle.jpg

Modal popup extender losing ID

Edit:
Added onload method:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["usersName"] != null)
{
object a = Session["_id"];
IDMaster = Convert.ToInt32(a);
GridView1.Columns[10].Visible = true;
GridView1.Columns[11].Visible = true;
}
I'm using a modal pop up extender to warn my customers that the item amount of a specific item is over a certain amount.
I have two buttons within this extender that allows a user to confirm they want an email sending to them when new stock arrives or not.
The trigger for the 'yes' button works perfectly but when i send the row ID to the constructor of my class used to store the email details it is always set to 0, even though the variable is global.
Here is my code to explain the issue further:
Button within my modal to add items to the cart:
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
TextBox t = (TextBox)row.FindControl("txtQuan");
*********Gain the item row ID (this is what needs to be passed*******
object ID = GridView1.DataKeys[row.RowIndex].Value;
*********This ID should be passed but is setting to 0************
rowID = Convert.ToInt32(ID);
string qty = t.Text;
int stockToAdd = Convert.ToInt32(qty);
DBHandler add = new DBHandler(rowID);
int qtyCheck = add.getStockQty();
if (stockToAdd > qtyCheck)
{
Button2_ModalPopupExtender.Show();
}
else{
SqlConnection con;
con = add.openDB();
con.Open();
DBHandler idCheck = new DBHandler(rowID);
int rows = idCheck.checkCartRows();
if (rows > 0)
{
int qtyNow = idCheck.getCartQty();
int updateStock = qtyNow + stockToAdd;
idCheck.updateQty(rowID, updateStock);
updatePanel();
}
else
{
idCheck.insertCart(qty);
updatePanel();
}
add.close();
}
}
The following code shows my confirm onclick button method. Note the ID 'rowID' that was stored in the above method when the add to cart button was clicked. This rowID is is setting to 0 instead of holding the rowID value.
protected void btnOK_Click(object sender, EventArgs e)
{
***** rowID is setting to 0*******
DBoutOfStockEmail insertNewEmailDetail = new DBoutOfStockEmail(IDMaster, rowID);
DBMembershipHandler getEmail = new DBMembershipHandler(IDMaster);
string emailToSend = getEmail.emailOfMember();
insertNewEmailDetail.insertDetails(emailToSend);
}
To summaries this question: Why is the rowID variable setting to '0' when i click the yes button with the modal pop up extender?
Based on my initial Comment I am going to post this as an answer
This can be from several things.. are you checking or doing IsPostBack checks, are you holding the Value(s) in a Session Variable..? ViewState is it enabled or disabled..? can you show what your Page_Load Event Looks like

Template checkbox checked property is getting false in Gridview for delete operation

I have used template CheckBox in gridview for multiple rows deletion, when I select multiple rows by selecting the CheckBox and perform the delete operation it seems to be selected CheckBox is not returning true on the code below.
protected void Button6_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
LinkButton ch = new LinkButton();
ch = (LinkButton)row.FindControl("l1");
id = Convert.ToInt16(ch.CommandArgument);
CheckBox chs = new CheckBox();
chs = ((CheckBox)row.FindControl("c1"));
if (chs.Checked == true)
{
DeleteSelected(id);
}
}
}
private void DeleteSelected(short id)
{
var ch = from a in empd.Employees where (a.ID == id) select a;
empd.Employees.DeleteAllOnSubmit(ch);
empd.SubmitChanges();
display_emp();
}
Could someone please assist me why CheckBox is not returning true value in calling function.
Well now I have fixed the connection everything, please just refresh the database.Please get the web application in the below link.
http://www.ziddu.com/download/20716096/WebApplication.zip.html
Please help me on why selected checkbox are not returning true in the code.
Details: Add if(IsPostback) at the start otherwise it just resets your grid controls. Example below
Solution:
if (!IsPostBack)
{
empd = new Employee_DetailsDataContext();
empd.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DemosConnectionString1"].ConnectionString;
display_emp();
}

Deleting from .net gridview

Im looking to programmatically delete from a GridView.
I have implemented the gridview's OnRowDeleting="Delete_Record" event handler, and this is called when the user clicks the delete link on the GridView, but as the row doesn't seem to be selected the code which does the delete does not work.
To test further I can click select, to select the row (commenting out the code behind the select) then if I click delete the code runs fine.
Is there a way to have the row selected as the first step in the Delete_Record?
GridViewRow row = gvResults.SelectedRow;
string id = row.Cells[1].Text;
ASB_DataDataContext db = new ASB_DataDataContext();
var delCase = from dc in db.Inputts
where dc.counter == Convert.ToInt32(id)
select dc;
foreach (var nCase in delCase)
{
db.Inputts.DeleteOnSubmit(nCase);
db.SubmitChanges();
}
Since you are using the event you should be having the required index as part of the GridViewDeleteEventArgs e which should be like say e.RowIndex
you can then access your needed parameter as grid.Rows[e.RowIndex].Cells[1].Text
You can achieve by the code shown below
public void GridView1_Delete_Record (Object sender, GridViewDeleteEventArgs e)
{
string id = gridView1.Rows[e.RowIndex].Cells[2].Text;
ASB_DataDataContext db = new ASB_DataDataContext();
var delCase = from dc in db.Inputts
where dc.counter == Convert.ToInt32(id)
select dc;
foreach (var nCase in delCase)
{
db.Inputts.DeleteOnSubmit(nCase);
db.SubmitChanges();
} Message.Text = "";
}
Set db.Inputts as DataSource of your datagrid. Just add next two lines after your code:
datagridView1.DataSource = null;
datagridView1.DataSource = db.Inputts;
Setting datasource to null is for clearing previous data from datagrid;

OnPageIndexChanged Clashing with OnItemCommand

I am facing a slight problem with both events clashing with one another.
I have a column displaying a link button, so that when user clicks on it, it would direct them to another page displaying the data in the specific row of the datagrid.
Below the datagrid, I have number pages, which just so happens to be a link button as well and when user clicks on page 2, it would display the page 2 data of the datagrid.
The problem now is that, when I want to click to page 2, it would redirect to the next page, which is the event of my ItemCommand.
Are there any ways of which I could make the datagrid differentiate, which is the correct link button to read,
So that both of them do not clash with one another??
protected void RPYGrid_ItemSelect(object sender, DataGridCommandEventArgs e)
{
DataSet dsGenRequestPayment = new DataSet();
dsGenRequestPayment = GenerateRequestPayment();
DataTable dtRequest = new DataTable();
dtRequest = dsGenRequestPayment.Tables[0];
if (((LinkButton)e.CommandSource).CommandName == "ItemSelect")
{
try
{
int iIndex = e.Item.DataSetIndex;
string sId = RPYGrid.DataKeys[iIndex].ToString();
foreach (DataRow drRequest in dtRequest.Rows)
{
string sRequestID = drRequest["RequestNo"].ToString();
if (sId == sRequestID)
{
sRequestNo = drRequest["RequestNo"].ToString();
sAmount = drRequest["RequestAmt"].ToString();
sAttachment = drRequest["FilePath"].ToString();
sReqCompanyID = drRequest["RequestCompanyID"].ToString();
sPayCompanyID = drRequest["PayerCompanyID"].ToString();
sReqCoName = drRequest["RequestCoName"].ToString();
sPayCoName = drRequest["PayerCoName"].ToString();
sRequestDate = drRequest["RequestDt"].ToString();
}
}
dtRequest.Clear();
dsGenRequestPayment.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
protected void RPYGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
RPYGrid.CurrentPageIndex = e.NewPageIndex;
GenerateRequestData();
}
Does that still happen to you by having different command names in your linkbuttons?
I think I didnt understand very well your question
is this what you want to do ?
Select Case (CType(e.CommandSource, LinkButton)).CommandName
Case "paging"
'dopaging
case "redirect"
'doredirect
Case Else
End Select

Categories

Resources