Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a issue with parameterized query.

Here the case is I need to fetch the values from DB and display in gridview, but in all the times the no. of records are zero.

Can you help me?

What I did was:
C#
MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY AND PRD.PRD_IDY=@prdidy and bat_idy=@batchidy ",con);

cmd.Parameters.AddWithValue("@prdidy", prdidy);
cmd.Parameters.AddWithValue("@batchidy", batchidy);

reader = cmd.ExecuteReader();
if(reader.HasRows)
{
    reader.Read();
    MessageBox.Show(reader.GetValue(0).ToString());
}
Posted
Updated 2-Dec-10 23:06pm
v3
Comments
Dalek Dave 3-Dec-10 4:23am    
Edited for Grammar and Syntax.

Can you check in the database and make sure you have records available for the prdidy and batchidy values you are sending into the database.

Your query otherwise looks ok.
 
Share this answer
 
Comments
krishna kishore58 3-Dec-10 3:45am    
thanks 4 replying... ya the records are available n u know the query is getting executed in Mysql query browser
Abhinav S 3-Dec-10 7:37am    
Can you please try
do
{
if (reader.HasRows)
{
reader.Read(); }
} while (reader.NextResult());
C#
MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY AND PRD.PRD_IDY=@prdidy and bat_idy=@batchidy ",con);

cmd.Parameters.AddWithValue("@prdidy", prdidy);
cmd.Parameters.AddWithValue("@batchidy", batchidy);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read)
{
GridView1.Datasource=reader ;
GridView1.DataBind();

reader .Close();
con.Close();
}
 
Share this answer
 
v3
Comments
krishna kishore58 3-Dec-10 3:45am    
thanks 4 replying...Dude cursor is not going in if(reader.Read()) and moreover how can i insert the records separetely in gridview without binding...
Dalek Dave 3-Dec-10 4:24am    
I Code Blocked your answer.
Hello Krishna,

since I see that you're joining tables BRD and PRD on their common field PRD_ID (I'm guessing that BRD.PRD_ID is a foreign key reference to table PRD), are you really sure that the join would return values?
Maybe the join of both tables is already empty.

Please try this:

C#
MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY", con);
//cmd.Parameters.AddWithValue("@prdidy", prdidy);
//cmd.Parameters.AddWithValue("@batchidy", batchidy);
reader = cmd.ExecuteReader();
if(reader.HasRows)
{
    reader.Read();
    MessageBox.Show(reader.GetValue(0).ToString());
}


Does this return results?

Modification:

Ok. You said code I pasted above does also return no records. This means the join of both tables on their common column PRD_ID is empty.
This means in effect that both tables do not have a common value in column PRD_ID.

End Modification

Cheers

Manfred
 
Share this answer
 
v6
Comments
krishna kishore58 3-Dec-10 4:01am    
In the query the u didnot include @prdidy and @batchidy...k dats okay..and here theres no issue of foreign key.As i told the query is getting executed in query browser by the passing the direct values...Every thing is correct but i donno how to work with parameterized query...help me...same issue the control is not reaching if condition...
Manfred Rudolf Bihy 3-Dec-10 4:03am    
Ok, so there you go. If the join is already empty adding parameters isn't going to get you anywhere. There has to be some PRD.PRD_ID value that exists in both tables PRD_ID column. Do you understand what I mean?
krishna kishore58 3-Dec-10 4:11am    
ya prd_idy is available in both the tables...
Manfred Rudolf Bihy 3-Dec-10 4:15am    
Yes the column is availabe in both tables. Thats what I already said. You need to check if the join WITHOUT the parameters would return records. So try what I posted in my answer and if no results are returned then you know that the columns PRD.PRD_ID and BRD.PRD_ID don't have any VALUES in common. Did I make myself clear now?
krishna kishore58 3-Dec-10 4:21am    
ya ur clear...the both the fields have few common values and lemme tel u 1 thing is that the qurey is getting executed in query browser and not in app...think there might be wrong usage of parameterized query...Dont mind i just wanna conform...does the answer which u posted is absolutely correct?
C#
using MySql.Data.MySqlClient;



// Download MySQLConnectorfor VS2010 and add rederence of the DLL named
// MySql.Data.dll

String strcon = "Your connection string";
MySqlConnection con = new MySqlConnection(strcon);
String strQuery = "Your SQL Query";
MySqlCommand cmd = new MySqlCommand(strQuery,con);

MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();

try
{
con.Open();
da.Fill(ds, "TableName");
// OOOORRRR
da.Fill(ds);
}
catch (Exception err)
{
//show error
}
finally
{
con.Close();
}


 
Share this answer
 
The best easy and more modifiable & even more readable n understandable code sample ever : same can be done for select query also.


C#
constr = "Your connection string";
con = new OleDbConnection(constr);
cmd = new OleDbCommand();

cmd.Connection = con;
OleDbParameter IDp, Namep, Semesterp, Collegep;
IDp = new OleDbParameter();
Namep = new OleDbParameter();
Semesterp = new OleDbParameter();
Collegep = new OleDbParameter();

IDp.OleDbType = OleDbType.Integer;
Namep.OleDbType = OleDbType.VarChar;
Semesterp.OleDbType = OleDbType.VarChar;
Collegep.OleDbType = OleDbType.VarChar;

IDp.Value = id;
Namep.Value = name;
Semesterp.Value = sem;
Collegep.Value = college;

cmd.Parameters.Clear();
cmd.Parameters.Add(IDp);
cmd.Parameters.Add(Namep);
cmd.Parameters.Add(Semesterp);
cmd.Parameters.Add(Collegep);

cmd.Connection = con;
cmd.CommandText = "INSERT INTO StudentInfo([ID],[Name],[Semester],[College]) VALUES([@IDp],[@Namep],[@Semesterp],[@Collegep]);";
    cmd.CommandType = CommandType.Text;

try
{
    con.Open();
    cmd.ExecuteNonQuery();
    ResultLabel.Text = "Data Successfully Inserted Into DataBase";
}
catch (Exception err)
{
    ResultLabel.Text = err.Message.ToString();
}
 
Share this answer
 
v3
Comments
fjdiewornncalwe 3-Dec-10 14:45pm    
Just added the pre tags to make the code portion a little easier to read. Cheers.
krishna kishore58 3-Dec-10 23:24pm    
Hey thanks very 4 replying... as u know im working with SELECT command ...i did wat ever u suggested...after executing the command how can i display the selected records in gridview..bcoz wen i try to do this
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "cmd");// its throwing the error as Fatal error exists...Wat the solution 4 this...
sandipapatel 4-Dec-10 3:21am    
using MySql.Data.MySqlClient;

// Download MySQLConnectorfor VS2010 and add rederence of the DLL named
// MySql.Data.dll

String strcon = "Your connection string";
MySqlConnection con = new MySqlConnection(strcon);
String strQuery = "Your SQL Query";
MySqlCommand cmd = new MySqlCommand(strQuery,con);

MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();

try
{
con.Open();
da.Fill(ds, "TableName");
// OOOORRRR
da.Fill(ds);
}
catch (Exception err)
{
//show error
}
finally
{
con.Close();
}

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