Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code is
C#
Int32 i;
       string  d="";
       for (i = 0; i < DataList1.Items.Count; i++)
       {
           CheckBox k;
           k = (CheckBox)(DataList1.Items[i].FindControl("chk"));
           if (k.Checked)
           {
               d += DataList1.DataKeys[i].ToString()+","
               Label1.Text = d;
           }

       }
             d = d.Substring(0, d.Length - 1);

           String qry;
           qry ="select * from tbook where bookid in (" + d + ")";
           SqlDataAdapter ad = new SqlDataAdapter(qry, ConfigurationManager.ConnectionStrings["key"].ConnectionString);
           DataSet dsp = new DataSet();


           ad.Fill(dsp);// error is at this line


           Repeater1.DataSource = dsp;
           Repeater1.DataBind();






any general solution for this problem??
Posted
Updated 4-Jul-10 21:41pm
v3
Comments
DaveAuld 5-Jul-10 3:01am    
Well, that totally depends on what the rest of the code is!

I suggest you update your question, to also include some of the surrounding code and also exactly what error message you got.
Sandeep Mewara 5-Jul-10 3:02am    
Too short a question to reply back! Add more details like what was the error.

1 solution

First of all about the error:
C#
SqlDataAdapter ad = new SqlDataAdapter(qry, ConfigurationManager.ConnectionStrings["key"].ConnectionString);
DataSet dsp = new DataSet();
ad.Fill(dsp);// error is at this line

This is a WRONG way to use SQLDataAdapter. You have not mentioned the SqlCommand that will be associated adapter to get data. Without that, Fill would not work.

Try something like:
C#
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(dataset);


Look here:
SqlDataAdapter Class[^]


After resolving this error, would just like to point out that there are few places you need to improve your coding. Like:
1.) Instead of... Int32 i; => int i;
2.) Instead of... string d =""; => string d = string.Empty;
3.) Keep a friendly names of variables and controls like, i as itemCount
4.) Keep checks for null at necessary places like accessing connection string. You directly passed that into query. You should first check if it is available or not (not null when retrieved from DB), then create a SqlConnection using it and use the connection object in adapter.
 
Share this answer
 
v2
Comments
Ankur\m/ 5-Jul-10 7:08am    
Are you sure the OP's way of using SQLDataAdapter is a WRONG way of using it?
AFAIK, the syntax OP has used is one of the overload for SQLDataAdapter where selectCommandText is the first parameter and the connection string is the second. It should run perfectly.
Sandeep Mewara 5-Jul-10 7:53am    
Yes Ankur, I am sure. If you feel oterwise, can you share across a link or so where the "connection string" is the second parameter? I believe it should be "SqlConnection object" instead and NOT connection string. I mentioned this in my last point(4) using it that way. Though what i explained was a simpler and obvious way that one can remember and not miss out next time.
Ankur\m/ 5-Jul-10 8:06am    
I confirmed my point using the VS IDE intellisense. But to test it, I just created a test program.
Test results - I can give 'Connection String' as the second parameter (and select query as the first). It works perfectly well. You may test it and then edit your answer. :thumbsup:
[EDIT]
Link: http://msdn.microsoft.com/en-us/library/w2d3kh8d.aspx
[/EDIT]
Sandeep Mewara 5-Jul-10 8:16am    
Thanks. Sure i will do that! Meanwhile let the OP revert back with her findings.
BTW, was your other test steps same as OP has done here? Or something different?
Ankur\m/ 5-Jul-10 8:23am    
I just did a test to fill a DataTable using the OP's way. And I could fill the DataTable with the selected records.

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