SQL Server - Qry Doubt - Asked By Jahir on 15-Feb-12 04:43 AM


how to qry Make ?

if
(DropDownList1.SelectedItem.Text != "ALL" && DropDownList2.SelectedItem.Text != "ALL" && RadioButtonList1.SelectedIndex != 0 && RadioButtonList2.SelectedIndex != 0)

{

adap =ERR coding this is 


new SqlDataAdapter("select a.fromdate,b.servicename,c.userid,d.type,e.debit,e.total from creditadd a,serve b,memreg c,serviceamount d,creditdeduct e where a.fromdate between '" + from.ToShortDateString() + "' and '" + to.ToShortDateString() + "' and b.id=e.services and c.userid=e.userid and d.type=e.type and e.debit=e.debit and e.total=e.total", con);//how to write qry here for retrieve from another table ?



ds = new DataSet();

adap.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();



}

Web Star replied to Jahir on 15-Feb-12 04:54 AM
Just use one simple thing when you are creating sql query in code just store that query in string variable as follows

string strQuery ="select a.fromdate,b.servicename,c.userid,d.type,e.debit,e.total from creditadd a,serve b,memreg c,serviceamount d,creditdeduct e where a.fromdate between '" + from.ToShortDateString() + "' and '" + to.ToShortDateString() + "' and b.id=e.services and c.userid=e.userid and d.type=e.type and e.debit=e.debit and e.total=e.total" ;

//and than use in code 
SqlDataAdapter  da = new SqlDataAdapter( strQuery  ,con);

And just try to debug your code and get quick view of value of strQuery  and copy it and just try to run in query analyser directly if this query run there than you code also work otherwise you will get actual error in that.


kalpana aparnathi replied to Jahir on 15-Feb-12 05:02 AM
hi,

Do you have any primary key with both table?If yes ,then use join query for your solution

Regards,
Somesh Yadav replied to Jahir on 15-Feb-12 05:06 AM
Hi,

Try this,

if (DropDownList1.SelectedItem.Text != "ALL" && DropDownList2.SelectedItem.Text != "ALL" && RadioButtonList1.SelectedIndex != 0 && RadioButtonList2.SelectedIndex != 0)

{
string strQuery ="select a.fromdate,b.servicename,c.userid,d.type,e.debit,e.total from creditadd a,serve b,memreg c,serviceamount d,creditdeduct e where a.fromdate between '" + from.ToShortDateString() + "' and '" + to.ToShortDateString() + "' and b.id=e.services and c.userid=e.userid and d.type=e.type and e.debit=e.debit and e.total=e.total" ;

SqlDataAdapter  da = new SqlDataAdapter( strQuery  ,con);

 Dataset Ds  = new DataSet();

da.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

Hope it helps you.