user3091919 user3091919 - 1 year ago 53
C# Question

Converting From String

string Audit = cmbToDoList.SelectedItem.ToString();
string _TransactionId = Audit;
cAuditTasksEntity transaction = _TransactionId as cAuditTasksEntity;
string trans = Convert.ToString(transaction);


string result = (from cAuditTasksEntity transactionid in _TransactionId
select transactionid.TransactionId).ToString();

The section i am getting the error is:
_TransactionId as cAuditTasksEntity;

It is saying that
Cannot convert type 'String' to 'DAL.cAuditTasksEntity via a reference conversion, boxing conversion,unboxing conversion,wrapping conversion, or null type conversion


This is my class that you may need to know?

[Table(Name = "Audit_Tasks")]
public class cAuditTasksEntity

private int _TaskId;
private int _AuditUserId;
private string _TransactionId;
private string _TaskTypeId;
private DateTime _Date;
private string _AuditStatus;

[Column(DbType = "INT", IsPrimaryKey = true, IsDbGenerated = true)]
public int TaskId
get { return _TaskId; }
set { _TaskId = value; }

[Column(DbType = "INT")]
public int AuditUserId
get { return _AuditUserId; }
set { _AuditUserId = value; }

[Column(DbType = "CHAR(32)")]
public string TransactionId
get { return _TransactionId; }
set { _TransactionId = value; }

[Column(DbType = "CHAR(32)")]
public string TaskTypeId
get { return _TaskTypeId; }
set { _TaskTypeId = value; }

[Column(DbType = "DATETIME")]
public DateTime Date
get { return _Date; }
set { _Date = value; }

[Column(DbType = "VARCHAR(255)")]
public string AuditStatus
get { return _AuditStatus; }
set { _AuditStatus = value; }

What I'm trying to do is; I have
that shows up in a combobox, when a user selects a task it needs to redirect them to another screen
where the details of that transaction shows up automatically.

There is a
TaskTable' this hold

then when they get redirected I need to search the
this will shows
Date creation
and other columns.

Answer Source

The easiest (probably) way to get cAuditTaskEntity transaction, which was selected by the user, is to create an array (or list, or dictionary, or whatever) to bind combobox displayed value to cAuditTaskEntity (via index).

Here is an example (it may be unusable by you, but should give an idea):

class cAuditTaskEntity
    public int A;
    public string B;

    public cAuditTaksEntity() {}

// data to fill into a combobox
cAuditTaskEntity[] data = new cAuditTaksEntity[] {new cAuditTaskEntity(), new cAuditTaksEntity(), new cAuditTaskEntity() ... }

// fill combobox, we represent data as a string
for(int i = 0; i < data.Length; i++)
    cmbToDoList.Items.Add(string.Format("Item {0}: {1}, {2}", i, data[i].A, data[i].B));

// reading selected data
// we can't convert represented by a string data back, but we can take it by index from created earlier array
cAuditTasksEntity transaction = data[cmbToDoList.SelectedIndex];


You can override ToString() in your cAuditTaskEntity, so then you can add object directly and get them. Here is an example (create new solution, add combobox and button on a form):

public partial class Form1 : Form

    public class A
        public string B;
        public int C;

        public override string ToString()
            return B;

    A[] _a = new A[] { new A() { B = "1", C = 111 }, new A() { B = "2", C = 222 }, new A() { B = "3", C = 333 } };

    public Form1()
        foreach (A item in _a)

    private void button1_Click(object sender, EventArgs e)
        var a = comboBox1.Items[comboBox1.SelectedIndex] as A;
        if (a != null)
            MessageBox.Show(string.Format("{0} {1}", a.B, a.C));

In any case you cannot convert string -> cAuditTaskEntity, but you can utilize combobox SelectedIndex to find in the array of its Items selected one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download