Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string s = "select * from "+U_name.Text+" where Physics_Topic='"+grdTopicname.Text+"'";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.Connection = con;
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        if (dr.HasRows)
        {
            dr.Read();
            string s1 = "update " + U_name.Text + " set TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='"+Result.Text+"'";
            con.Open();
            cmd.ExecuteNonQuery();
            
        }
        else
        {
            string s2 = "insert into" + U_name.Text + "where TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
            con.Open();
            cmd.ExecuteNonQuery();


plz help me sir
Posted
Updated 17-Dec-14 17:17pm
v2

well, for a start, you

a) don't tell us where you get that error - for example, at the dr.read() on line 5?
b) use 'con' on lines 2,3 but don't issue an open until line 11,17
c) you havnt shown us where & how you initialise 'con'

so it might be a bit hard for us to see whats going on
 
Share this answer
 
Comments
iamvinod34 17-Dec-14 23:32pm    
SqlDataReader dr = cmd.ExecuteReader(); get eror
It looks like you try to use the reader before you open the connection.

C#
string s = "select * from "+U_name.Text+" where Physics_Topic='"+grdTopicname.Text+"'";
con.Open();   // Moved this statement here.
SqlCommand cmd = new SqlCommand(s, con);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
    dr.Read();
    string s1 = "update " + U_name.Text + " set TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='"+Result.Text+"'";
    cmd.ExecuteNonQuery();
}
else
{
    string s2 = "insert into" + U_name.Text + "where TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
    cmd.ExecuteNonQuery();
    con.Close();   // Also close the connection


That said, there are a few things you should correct in your code.

1. Use using statements for connections. It will automatically close your connection.
C#
using (SqlConnection con = new SqlConnection(connectionString))
{
    // Put the rest of your code here
}

2. Also use using for readers for the same reason
C#
using (SqlDataReader dr = cmd.ExecuteReader())
{
    // Reading code here
}


3. Use paramterized SQL commands
This makes your life easier when it comes to change the SQL commands and it prevents from injection of malicious code.
SqlCommand.Parameters[^]
 
Share this answer
 
Comments
iamvinod34 17-Dec-14 23:41pm    
The ConnectionString property has not been initialized.
con.open();
George Jonsson 17-Dec-14 23:52pm    
What do you mean?
Haven't you initialized with a connection string?
iamvinod34 17-Dec-14 23:57pm    
yes sir
my code :
{

string s = "select * from " + U_name.Text + " where Physics_Topic='" + grdTopicname.Text + "'";
con.Open();
SqlCommand cmd = new SqlCommand(s, con);
cmd.Connection = con;

using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
if (dr.HasRows)
{
dr.Read();
string s1 = "update " + U_name.Text + " set TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
cmd.ExecuteNonQuery();

}
else
{
string s2 = "insert into" + U_name.Text + "where TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
}
}

}
George Jonsson 18-Dec-14 0:01am    
So where does it fail?
You understand I cannot execute your code, right?
iamvinod34 18-Dec-14 0:08am    
no sir
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string s = "select * from " + U_name.Text + " where Physics_Topic='" + grdTopicname.Text + "'";

SqlCommand cmd = new SqlCommand(s, con);
cmd.Connection = con;

using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
if (dr.HasRows)
{
dr.Read();
string s1 = "update " + U_name.Text + " set TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
cmd.ExecuteNonQuery();

}
else
{
string s2 = "insert into" + U_name.Text + "where TopicID='" + grdID.Text + "',Physics_Topic='" + grdTopicname.Text + "',Physics_score='" + Result.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
}
}

}
The ConnectionString property has not been initialized.
Line 266: using (SqlConnection con = new SqlConnection(connectionString))
Line 267: {
Line 268: con.Open();
Line 269: string s = "select * from " + U_name.Text + " where Physics_Topic='" + grdTopicname.Text + "'";
Line 270:

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