I have created a WebMethod in a WebService that uses stored procedures to find whatever you are searching for.
[WebMethod]
public DataSet getMyData(string search)
{
using (SqlConnection conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("searchingads", conn);
SqlDataAdapter da;
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#search", search);
da = new SqlDataAdapter(cmd);
da.Fill(ds, "MyData");
conn.Close();
conn.Close();
return ds;
}
I don't know how to call this method from an ASP.NET application. I have a button that, when clicked, needs to call this method and populate a GridView.
I have the following code in my ASP.NET web application (on button click):
WebService1 service = new WebService1();
GridView2.DataSource = service.getMyData(TextBox1.Text);
GridView2.DataBind();
Label1.Text = service.HelloWorld();
The label switches to "hello world" when the button is clicked, but it doesn't give me any table when I do a search.
Thank you in advance for your help.
Please use this
GridView2.DataSource = service.getMyData(TextBox1.Text);
instead of
GridView2.DataSource = service.IskanjeOglasov(TextBox1.Text);
If you have tested your search logic and its returning the data then try by assigning the data table as data source not the data set.
GridView2.DataSource = ((DataSet)service.getMyData(TextBox1.Text)).Tables[0];
GridView2.DataBind();
Here I have removed the check part of dataset. This might work.
Related
Sorry if this is a stupid question! I'm very new to coding and only have limited access to the internet whilst at work, I was just wondering if this is possible? and If so do you have a useful link for me to read over?
A quick overview of what I'm trying to do.
I have form1 and form2, form1 has a datagridview box that pulls data from the database, when I click on a cell it opens form2, which has a checklistbox which I want to now put code in so it pulls the data from a specific column and checks a box if the cell that was clicked applies to it.
Go easy on me if this question is stupid ^^, thanks in advance!
Some code I have produced so far! Which pulls the boxes from the database, but I also need to check the boxes too..
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
DataTable dt = ds.Tables[0];
foreach (DataRow datarow in dt.Rows)
{
checkedListBox1.Items.Add(datarow["ReportID"]);
Edited: put current coding in.
You can use below code to bind checkboxlist from database.
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID,NAME from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
checkedListBox1.DataValueField = "ID"
checkedListBox1.DataTextField = "NAME"
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DataBind();
Hello internet so first i am newbie at c# and i am doing project about changing data from a sql table. I passed the result of the query to the datatable and datagridview, but what i want next is i want the modified datagridview table passing to a dataset. this is how i get my table from the sql:
string query = "select Article,Barcode,\"Minimum Stock\",\"Current Stock\" from article where Fid=2 and \"Minimum Stock\" > \"Current Stock\"";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
da.Dispose();
Now the users will modifie the table and valid the information. How can i save it to a dataset?
I tried to bind dataset to the datagridview:
Dataset1.Tables.Add(datagridview1.datasource);
But it threw a error where it says datasource doesn't use here
Can anyone help?
I am using c# and i don't know how to write the data from c# in the HTML (to be available on the browser)
Here is my code :
public void searchbutton_Click(object sender , EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=MAX-PC\\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True");
SqlCommand cmd = new SqlCommand("search_name", conn);
cmd.CommandText = "exec search_name #name";
cmd.Parameters.AddWithValue("#name", search.Text);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
conn.Close();
}
I want to know what to write in the HTML file to take the data from the reader
Thanks
You probably want to use a GridView control to display the data. http://msdn.microsoft.com/en-us/library/aa479342.aspx
Hope this helps
After the execute reader command do the following lines code for binding the data to the grid.
Also add a gridcontrol to the HTML to which the data has to be rendered.
DataSet ds = new DataSet();
DataTable dt = new DataTable("Table1");
ds.Tables.Add(dt);
ds.Load(read, LoadOption.PreserveChanges, ds.Tables[0]);
gridControl1.DataSource = ds.Tables[0];
I want to select all customer information where customerid = the selected customerid stored in the combo box and show the result in datagridview I tried this code but the gridview doesnot show result.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
int id = Convert.ToInt32(comboBox1.SelectedValue);
string cmdstring=string.Format("select *from customers where customer_id={0}",id);
SqlCommand cmd = new SqlCommand(cmdstring,con);
//cmd.Parameters.AddWithValue("#id",id);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//da.Fill(ds, "customers");
//dataGridView1.DataSource = ds.Tables["customers"];
con.Open();
SqlDataReader red = cmd.ExecuteReader();
con.Close();
dataGridView1.DataSource = red;
button = new DataGridViewButtonColumn();
button.HeaderText = "edit";
button.Tag = ds.Tables["customers"].Columns["customer_id"];
dataGridView1.Columns.Add(button);
}
you could always make a DataBase Class and if you need to refactor this Class to pass in Connection String or read Connection string from .Config File you can use this as a template to get started plus it's a lot cleaner
Notice that I am returning a DataTable you can use this if you like just a suggestion
public class ClassDataManagement
{
public DataTable GetData(string sqlcmdString, string connString)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sqlcmdString, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
if you want to use DataSet instead of DataTable replace where i have DataTable with
or change the method to return a DataSet like this below
public DataSet GetData(string sqlcmdString, string connString)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sqlcmdString, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
after returning the ds you will need to bind it like this
dataGridView1.DataSource = ds;
dataGridView1.DataBind();
I'm fairly certain that you're not getting any data because you're closing the connection before binding, and because you're using an incompatible type as your data source:
con.Close();
dataGridView1.DataSource = red;
Set the data source prior to closing the connection, or at least be sure to populate the data (for data readers, the data are populated as you enumerate). Additionally, DataGridView.DataSource indicates that it must use one of four interfaces: IList, IListSource, IBindingList, and IBindingListSource. SqlDataReader does not support these. I recommend reading DataAdapters and DataReaders, as this outlines some of the features that are for populating this kind of control.
I try to bind database data to the gridview in c# and asp.net. But I couldn't see the datas in the gridview.Rows are added to the gridview but they are empty. When I run that query in SQLServer, it gives the correct result.I didn't add or change any code to the asp part.Should I? I couldn't find where is the problem :( please help..
myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
command = new SqlCommand();
connect.Open();
command.Connection = connect;
string komut = "SELECT K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi FROM OduncIslemleri O,Kitap K WHERE O.kullaniciId=" + Session["id"] + " AND O.kitapId = K.id;";
try
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand = connect.CreateCommand();
sqlCommand.CommandText = komut;
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connect);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//Create a DataTable to hold the query results.
DataTable dTable = new DataTable();
//Fill the DataTable.
sda.Fill(dTable);
GridView1.DataSource = dTable;
GridView1.DataBind();
}
catch (SqlException)
{
//Console.WriteLine(e.StackTrace);
}
reader.Close();
connect.Close();
Here is the correct answer :
myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
string sorgu = "select K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi from Kitap K, OduncIslemleri O where O.kitapId = K.id and O.kullaniciId = "+ Session["id"];
SqlDataAdapter sadp = new SqlDataAdapter(sorgu, connect);
DataSet ds = new DataSet();
sadp.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
connect.Close();
I also used template fields in Gridview. Also autogeneratedFields should be true. I hope this helps to the people who have the same problem
watch for another event triggered after the bind that could be clearing the rows
Try creating a DataSet and populate that using Fill instead. I've never seen Fill used on a DataTable - and can't find that particular overload on MSDN. My suspicion is, though, that such an overload would not modify the existing schema of the DataTable (which, since it's only just been created prior to use in your example, would mean that it has no columns).
I think you have to use a BindingSource Control, you set the DataSource of it to the DataTable, and then set the GridView's DataSource to the BindingSource.