asp.net and wf4 correlation exception - c#

Im pretty new to wf4 but have created a simple console app with submit, approve and reject capabilities just fine. Im now trying to create an asp.net app that consumes the service i have created but am getting a fault exception as shown below. This worked fine in my console app
The execution of an InstancePersistenceCommand was interrupted because
the instance key '3a552603-c92f-2424-085c-7b6fc1a0e98e' was not associated to
an instance
Basically ive created 3 simple pages. the first page is a simple form where the user submits a request. the 2nd page just prints a list of the requests. Clicking on one of the requests takes you to the 3rd page that prints a more detailed view of the request with an approve and decline button. Im using a GUID for the correlation which is passed to the 3rd page through the query string. Clicking the approve button fires the approve method of the service passing in the query string value. Its at this point i get the exception. The strange thing is the guid in the error message is not the same as the value im passing in.
Any ideas below is my code is that helps
1st page
protected void Unnamed1_Click(object sender, EventArgs e) {
ServiceReference1.ServiceClient Client = new ServiceReference1.ServiceClient();
ServiceReference1.Request R = new ServiceReference1.Request();
R.Title = TxtRequestTitle.Text;
R.Amount = Convert.ToInt32(TxtAmount.Text);
Guid g = Guid.NewGuid();
Client.SubmitRequest(R, g);
Response.Write("submitted");
}
2nd page
protected void Page_Load(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(#"Data Source=bantai11\sqlexpress;Initial Catalog=RequestMonkey;Integrated Security=True;Asynchronous Processing=True")) {
using (SqlCommand com = new SqlCommand()) {
com.Connection = con;
com.CommandType = System.Data.CommandType.Text;
com.CommandText = "Select InstanceId, Title, state from Requests";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dt);
rp.DataSource = dt;
rp.DataBind();
}
}
}
3rd page
protected void Page_Load(object sender, EventArgs e) {
this._id = Request.QueryString.Get("Id");
using (SqlConnection con = new SqlConnection(#"Data Source=bantai11\sqlexpress;Initial Catalog=RequestMonkey;Integrated Security=True;Asynchronous Processing=True")) {
using (SqlCommand com = new SqlCommand()) {
con.Open();
com.Connection = con;
com.CommandType = System.Data.CommandType.Text;
com.CommandText = "Select InstanceId, Title, state from Requests where instanceid = '" + this._id + "'";
SqlDataReader dr = com.ExecuteReader();
dr.Read();
lblTitle.Text = dr[1].ToString();
lblGuid.Text = dr[0].ToString();
lblAmount.Text = "0";
}
}
}
protected void btnApprove_Click(object sender, EventArgs e) {
ServiceReference1.ServiceClient Client = new ServiceReference1.ServiceClient();
Client.Approve(1, this._id);
}

The exception indicates that the InstanceStore couldn't find a workflow associated with that key. It could be that the workflow has already completed or aborted with an error. You need to get tracking data on the WorkflowService to see what is going wrong. See Troubleshooting Workflow Services with diagnostic logging

Related

C# - Pull SQL data to textboxes/labels from changing combobox selected item

So I'm trying to create sort of an overview utility of sites, with different infos on each site.
I'd like to have a dropdown list / combobox, reading a sqldb and create items according to the db. Then I would like different textboxes to get populated with a value from a column.
Say my db table is called "AvSites" so far (just for the sake of it) i have "projectNr", "siteName", "siteClients" and "siteLicenses" columns I'd like each of these to populate some textbox / label somewhere.
I've tried the following, which kinda works, Ive had the code working most of the time, but the thing that defeats me is having the data change with the combobox item selected.
I hope you can help, and so here is my code so far (I have a login window, before this "main" program starts, just so you're not wondering)
And I'm quite new to C# so if there's something thats done inefficient thats the reason :) Im still learning.
namespace AvOverview{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//btn_LogOut Click Event
this.Hide();
Form1 fl = new Form1();
fl.Show();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FrmMain_Load(object sender, EventArgs e)
{
string cs = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
SqlConnection con = new SqlConnection(cs);
con.Open();
string strCmd = "select * from AvSites";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
cmd.ExecuteNonQuery();
con.Close();
}
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
string cs = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
string strCmd = "select id from AvSites";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(strCmd, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{//this last part is solely for testing if the text changed the way I wanted.
label1.Text = dr.GetValue(1).ToString();
label2.Text = dr.GetValue(2).ToString();
label3.Text = dr.GetValue(0).ToString();
label4.Text = dr.GetValue(3).ToString();
You don't need to call the database again. All the info are in the current selected item.
When you set the DataSource of your combo to a datatable like you do in the click event each element of the combo is a DataRowView and from this element you can get all the info extracted from the database from your initial query
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView rv = Combo1.SelectedItem as DataRowView;
if(rv != null)
{
label1.Text = rv[1].ToString();
label2.Text = rv[2].ToString();
label3.Text = rv[0].ToString();
label4.Text = rv[3].ToString();
}
}
Side note: There are some improvements needed in your code.
First you should store the connection string in the config file and read it back with the ConfigurationManager class. Read about Configuration in NET
Second you shouldn't work with disposable objects like you do now. A disposable object should be disposed as soon as you have finished to use it. In particular the SqlConnection keeps valuable system resources both on your machine and on the server. You should start to use the using statement
string strCmd = "select * from AvSites";
using(SqlConnection con = new SqlConnection(.......))
using(SqlCommand cmd = new SqlCommand(strCmd, con)))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
// ??? not needed ==> cmd.ExecuteNonQuery();
// not needed with using ==> con.Close();
}
// Here the connection is closed and disposed and resources are released

dataGridView1 doesn't display data from a specified table

This code block is supposed to be a sign-up form. Project has several
Forms needed to be attached to one another. When you run the project a
LOGIN form first appears on the screen with its two textBoxes labelled
as (Username, Password) and two buttons (sign-in , sign-up). After the
sign-up process which sends all your information with "insert into"
command into the USERStable in database, you have to type your
"username" and "password" into the displayed textboxes correctly,
they should match the following "GetString(1)" and "GetString(2)", and
then by pressing "sign-up" button bam, you should have "CONTACTS FORM"
popped up on the screen. CONTACTS FORM has a dataGridView which is
supposed to display all the rows that USERStable has. In order to
prevent users from getting in with wrong Username and Password, I used if
statement inside the while loop. It works but when you get in,
dataGridVew doesn't display any information. However without a while loop it does.
I couldn't figure out the reason where the problem lays on. Do you have any
suggestions?
One thing I forgot to point out. I've also created a class named USERS with
fields and properties:
string username;
string password;
On the top you can see I've created an object with reference "us".
public partial class LOGIN : Form
{
public LOGIN()
{
InitializeComponent();
}
USERS us = new USERS();
private void button1_Click(object sender, EventArgs e)
{
CONTACTS contact = new CONTACTS(); //FORM CONTACT
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source = DESKTOP-9I0BNAS\SQLEXPRESS; Initial Catalog = CAVID; Integrated Security = True";
con.Open();
SqlCommand cmd = new SqlCommand("Select * FROM users", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader dr = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
while (dr.Read())
{
us.Username = dr.GetString(1);
us.Password = dr.GetInt32(2).ToString();
if(us.Username == textBox1.Text &&
us.Password == textBox2.Text)
{
contact.Show();
dataTable.Load(dr);
contact.dataGridView1.DataSource = dataTable;
this.Hide();
break;
}
}
}
}

Asp.NET label displays

So i have this .Net website which is very basic and simple since i am creating it to learn .Net and pratice. For the moment i have a form that after some inputs checks in the database and ant displays data.
What i what now to do but im not figuring out yet how to do it is that:
If the system checks and there is no data equal to the input in the database the label posts a msg like: "NO DATA FOUND"
Here is the code i used for checking and displaying data after clickin submit button:
protected void Search_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnect"].ConnectionString);
con.Open();
SqlCommand cnd = new SqlCommand();
cnd.CommandText = "select * FROM [dbconnect] ";
cnd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cnd;
DataSet ds = new DataSet();
da.Fill(ds, " dbconnect ");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables["dbconnect"].NewRow();
drow["userName"] = TextBox3.Text;
drow["rollno"] = TextBox4.Text;
ds.Tables["TableName "].Rows.Add(drow);
da.Update(ds, " dbconnect ");
}
catch(Exception EX)
{
string A = "";
}
}
how can i make my label to post a msg if from the button i will not have a response
If you add a Label control to your web page, say called lblMessage, then at the end of your code above, you could do something like...
lblMessage.Text = "No results found";

null value in asp.net page.items

I have an asp.net page with behind C# code.
I add a page Item in main page. then I used some web user control.
In one of them, when I use this Item in page_load. it has correct value, but when I use it in another private function in same web control, it has null value!
then I prefered to use a public string and set the value of that Item in it. but not working too.
public string ReqID0;
string Status0 { get; set; }
string Status = "";
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sqlc4;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReqID0 = Page.Items["ReqID"].ToString();
ReqIDLbl.Text = ReqID0;
...
second function (method) is:
private void Change_Status(string newStatus)
{
//ReqID0=Page.Items["ReqID"].ToString();
sqlc4 = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Request SET Status=#newStatus WHERE ID=#ReqID";
cmd.Parameters.AddWithValue("#newStatus", newStatus);
cmd.Parameters.AddWithValue("#ReqID", ReqID0);
cmd.Connection = sqlc4;
sqlc4.Open();
cmd.ExecuteNonQuery();
sqlc4.Close();
}
Page.Item refreshes with each post back and you have to add it every time your page refreshes ,,and as per your code here in Page_Load event
if (!this.IsPostBack)
{
ReqID0 = Page.Items["ReqID"].ToString();
ReqIDLbl.Text = ReqID0;
--Assigning of value is getting bypassed when your page does a post back and you get Null value in your Change_Status method. Try using Viewstate Instead.
Use the Items property to store objects with the same lifetime as the page request. MSDN
Update: try using ViewState.
if (!this.IsPostBack)
{
ViewState["ReqID"]= ReqIDLbl.Text;
//ReqID0 = Page.Items["ReqID"].ToString();
//ReqIDLbl.Text = ReqID0;
private void Change_Status(string newStatus)
{
//ReqID0=ViewState["ReqID"].ToString();
The global variables are created / intialized between postback in asp.net and they do not retain the values between postback as http is stateless protocol, you need to use ViewState for that.
public string ReqID0;
string Status0 { get; set; }
string Status = "";
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sqlc4;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReqIDLbl.Text = ReqID0;
ReqID0 = ViewState["ReqID0"].ToString();//ViewState
/*In case if the above code doesn't work use
ReqIDLbl.Text = ViewState["ReqID0"].ToString();*/
------
}
//Second Function
private void Change_Status(string newStatus)
{
ReqID0=ViewState["ReqID0"].ToString();
sqlc4 = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Request SET Status=#newStatus WHERE ID=#ReqID";
cmd.Parameters.AddWithValue("#newStatus", newStatus);
cmd.Parameters.AddWithValue("#ReqID", ReqID0);
cmd.Connection = sqlc4;
sqlc4.Open();
cmd.ExecuteNonQuery();
sqlc4.Close();
}
Hope This might give you the answer

Getting Invalid syntax near keyword where

I am on the verge of finishing a web-project similar to nike+ and runkeeper, it is a prototype for a company make running devices, anyhow I have stumbled upon a problem here, and I am getting a error message that says Invalid syntax near keyword WHERE. I cannot for the life of me figure it out.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Email"] == null) //If user is not logged in, send to startpage
{
Response.Redirect("~/UserPages/Default.aspx");
}
else if (!IsPostBack)
{
//User info is selected from DB and put in textboxes
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["jaklin11ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM [Users] WHERE Email = #Email", con1);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#Email", Session["Email"].ToString());
using (con1)
{
con1.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
imgProfileImageProfile.ImageUrl = rdr["ProfileImage"].ToString();
textProfileImageProfile.Text = rdr["ProfileImage"].ToString();
textFirstNameProfile.Text = rdr["FirstName"].ToString();
textLastNameProfile.Text = rdr["LastName"].ToString();
textHeightProfile.Text = rdr["Height"].ToString();
textWeightProfile.Text = rdr["Weight"].ToString();
textPasswordProfile.Text = rdr["Password"].ToString();
textBirthdateProfile.Text = rdr["Birthdate"].ToString();
textAreaCode.Text = rdr["AreaCode"].ToString();
textTown.Text = rdr["Town"].ToString();
ddlGenderProfileEdit.Text = rdr["Gender"].ToString();
}
}
}
}

Categories