wilko100
asked on
C# Execute SQl View with an object
newbie trying to trigger off this statement in a Main form C#...
command.CommandText = "SELECT CustomerName FROM spCustomersandContacts WHERE CustomerCounter = " + form.Counter + ""; //Works ok and returns value
this.txtCustomerName.DataB indings.Ad d("Text", mydatabaseDataSet, "spCustomersandContacts.Cu stomerName "); //nothing happens
then populate txtbox with the result, it does run with no errors and when i put a break point ont the SQL code it does have the value OK but it does not poulate the txtbox, is my syntax wrong on the txtbox line?
note:-Definition of form.counter holds a value [Counter] returned from another form
command.CommandText = "SELECT CustomerName FROM spCustomersandContacts WHERE CustomerCounter = " + form.Counter + ""; //Works ok and returns value
this.txtCustomerName.DataB
then populate txtbox with the result, it does run with no errors and when i put a break point ont the SQL code it does have the value OK but it does not poulate the txtbox, is my syntax wrong on the txtbox line?
note:-Definition of form.counter holds a value [Counter] returned from another form
for more help, have a look at following link,
https://www.experts-exchange.com/questions/22021046/DataBinding-To-A-textBox-Question-Window-Forms-net-c.html
https://www.experts-exchange.com/questions/22021046/DataBinding-To-A-textBox-Question-Window-Forms-net-c.html
ASKER
Below is the code, i have created a datasource (RIS_ClientDataset which is not bound to any control) and used it in the txtbox lline of code but do i need this as i have manually specified it in the SQLconnection string. Is that where im going wrong? Do i need to specify the database name in this line instead of rIS_ClientDataset?
this.txtCustomerName.DataB indings.Ad d("Text", rIS_ClientDataSet, "spCustomersandContacts.Cu stomerName "); //change rIS_ClientDataset to database?
I will have a look at the reference's and VB code for examples you mentioned later today so thanks for that
this.txtCustomerName.DataB
I will have a look at the reference's and VB code for examples you mentioned later today so thanks for that
private void btnSearchCustomer_Click(object sender, EventArgs e)
{
using (frmSearchCustomer form = new frmSearchCustomer())
{
if (form.ShowDialog() == DialogResult.OK)
{
//this.txtCustomerName.Text = form.Counter;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = @"Data Source=<SQLInstance>;Initial Catalog=RIS_Client;Integrated Security=True";
//setup SqlCommand and assign SQL query in command
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
command.CommandType = System.Data.CommandType.Text;
command.Connection = conn;
command.CommandText = "SELECT CustomerName FROM spCustomersandContacts WHERE CustomerCounter = " + form.Counter + "";
//create one data adapter which will execute the command and fill the data into data set
conn.Open();
System.Data.SqlClient.SqlDataAdapter recordAdp = new System.Data.SqlClient.SqlDataAdapter();
SqlDataReader reader = command.ExecuteReader();
reader.Close();
recordAdp.SelectCommand = command;
System.Data.DataSet recordSet = new System.Data.DataSet();
recordAdp.Fill(recordSet);
command.Dispose();
conn.Close();
this.txtCustomerName.DataBindings.Add("Text", rIS_ClientDataSet, "spCustomersandContacts.CustomerName");
}
}
ASKER
It tried that example in the link but still not binding th txt box...
this.txtCustomerName.DataB indings.Ad d("Text",s pCustomers andContact sBindingSo urce, "CustomerName");
this.txtCustomerName.DataB
can you please provide me with your .ASPX code? you have already provided .CS code but I would like to see .ASPX also
ASKER
ASPX?
ASKER
Call me dumb but this is C# not ASP, and i still cant get this txt box to bind, anyone esle have a suggestion?
ASKER
im know getting this...
ERROR Child list for field spCustomersandContacts cannot be created.
after this...
this.txtCustomerName.DataB indings.Ad d("Text", conn, "spCustomersandContacts.Cu stomerName ");
any ideas?
ERROR Child list for field spCustomersandContacts cannot be created.
after this...
this.txtCustomerName.DataB
any ideas?
ASKER
Ok think i sorted it
txtCustomerName.Text = cmd.ExecuteScalar() + "";
gets me back the CustomerName which is initialally what i wanted to do HOWEVER...
What if i want to return a record, i read that executescalar returns a single column (I.E. Customername) but what if i want to fill other txt boxes such as address and telephone number for that customer? Can anyone help?
txtCustomerName.Text = cmd.ExecuteScalar() + "";
gets me back the CustomerName which is initialally what i wanted to do HOWEVER...
What if i want to return a record, i read that executescalar returns a single column (I.E. Customername) but what if i want to fill other txt boxes such as address and telephone number for that customer? Can anyone help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim bindingsource As new System.Windows.Forms.Bindi
Me.bindingsource.DataMembe
Me.bindingsource.DataSourc
textBox1.DataBindings.Add _
(New Binding("Text", bindingsource, "Name"))
this is actually VB.NET code but you can get the concept. if this will not work, let me see your code more.