Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim s As String = dgv.Rows(row).Cells("Column1").Value
             cmd = New OleDbCommand("SELECT COUNT(Protocol) As num FROM Raw_Protocol WHERE Protocol = '" & s & "'", connection)
        Dr = cmd.ExecuteReader

            While Dr.Read()


                dgv.Rows.Add()
                dgv.Rows(row).Cells("Column2").Value = Dr("num")
                   datagridview1.Rows(row).Cells("Column4").Value = Dr("num")
                row += 1

           End While



This is another question,i want to make it loop to count the data in the mdb database only when data grid view row column 1 got data and stop when the data grid view row column 1 don't have data. Can show me how i changed the code.Thanks
Posted
Comments
[no name] 19-Apr-12 2:53am    
i have a gridview and i want to bind it from the loop in which loop contain data from database and gridview displays only last record. i want to display all records which data is according to query.


my query in for loop like...
for (int k = 0; k < intArray.Length; k++)
{
//select document.title from document,document_rights where document_rights.user_id in('" + intArray[k] + "')";
string xxxx = "select title from document where id in('"+intArray[k]+"')";
ad1 = new SqlDataAdapter(xxxx, c.getcon());
ds1 = new DataSet();
ad1.Fill(ds1);

if (ds.Tables[0].Rows.Count != 0)
{

GridView1.DataSource = ds1;
GridView1.DataBind();
}

}

If I get what your after!

its a list in a datagridview that shows Protocol and Count of Protocol i.e.

Protocol, Count
a, 10
b, 2

and so on. If so you can solve this in a query

SQL
select protocol, count(protocol) from raw_protocol


using the query with a dataadapter as shown in this article, oleDbAdapter[^]

The example uses a dataset but you can just replace it for a datatable if you wish.
 
Share this answer
 
Can you explain properly what you want to achieve?
 
Share this answer
 
Comments
william_nhl 28-Jun-11 12:35pm    
What i want is to let the source code above to run to calculate each data grid view row column 1 data and show it on that row column 2 if the column 1 have data. The below is the source code that will show the distinct data in data grid view column 1.
Dim n As Integer = 0
row = 0
dgv.Rows.Clear()
datagridview1.Rows.Clear()
connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\AnalyzerDb.mdb")
connection.Open()
cmd = New OleDbCommand("SELECT DISTINCT (Protocol) FROM Raw_Protocol", connection)
Dr = cmd.ExecuteReader
Do
While Dr.Read()
dgv.Rows.Add(Dr("Protocol"))
datagridview1.Rows.Add(Dr("Protocol"))
n += 1
End While
Loop While Dr.NextResult
Dr.Close()
connection.Close()
Not an answer, but a strong suggestion: never, ever concatenate your strings to generate an SQL command: it is an open invitation to an accidental or deliberate SQL injection attack which can destroy your database. Use Parameterized queries instead:
cmd = New OleDbCommand("SELECT COUNT(Protocol) As num FROM Raw_Protocol WHERE Protocol = @PCL", connection)
cmd.Parameters.AddWithValue("@PCL", s);
 
Share this answer
 
Comments
william_nhl 28-Jun-11 12:27pm    
Ok,thanks for advice.^^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900