c# - Displaying SQL Server database value in Gridview showing error -


When I try to display the database values ​​in Gridview, I get an error:

< P> Exception of the action without a type 'System.Data.SqlClient.SqlException'

Additional information originated in System.Data.dll:. Keywords with the wrong syntax 'and'

and the code

  Private Zero Button 1_Click (Object Sender, EventArgs e) {SqlDataAdapter adap; Dataset ds; SqlConnection cn = New SqlConnection (@ "Data Source = DILIPWIN \ SQLEXPRESS; Initial Catalog = Radb; Integrated Security = True"); Cn.Open (); Var Home = New Home (); adap = Select the new SqlDataAdapter mark ("roll_num, mark with mark where <50 and dept_id =" + home.cbox.dept.SelectedValue + "and sem_id =" + home.cbox.Sem.SelectedValue + "and subject_id =" + home.cboxSubject. SelectedValue + "and batch_id =" + home.cboxBatch.SelectedValue + "and cls_id =" + home.cboxClass.SelectedValue, cn); DS = new system. Dataetset (); Adap.fill (DS, "DataGrid View1"); dataGridView1.DataSource = DTTables [0]; }    

Use the SQL-parameter which probably fixes this problem and the future Issues that prevents SQL-Injection:

  String sql = @ "roll_num, select from the marks marks where marks of <50 and dept_id = @ dept_id and sem_id = @ sem_id And subject_id = @ subject_id and BATCH_ID = @ BATCH_ID and cls_id = @ cls_id, "; Dataset DS = new dataset (); Using (using the new SqlConnection (@ "Data Source = DILIPWIN \ SQLEXPRESS; Initial List = Radb; Integrated Security = True")) (using da = new SqlDataAdapter (SQL, CN)) {da. SelectCommand.Parameters.AddWithValue ("@dept_id", home.cboxDept.SelectedValue); Da.SelectCommand.Parameters.AddWithValue ("@ sem_id", home.cbox.Sem.SelectedValue); Da.SelectCommand.Parameters.AddWithValue ("@ topic_id", home.cbox.Subject.SelectedValue); Da.SelectCommand.Parameters.AddWithValue ("@Batch_ID", home.cboxBatch.SelectedValue); Da.SelectCommand.Parameters.AddWithValue ("@ cls_id", home.cboxClass.SelectedValue); Da.Fill (DS); // You do not need to close / close connection with filling} Datagram View1.DataSource = ds.Tables [0];   

You should also use the right type. Try to guess the type of value, if those integer ( int.Parse (home.cboxdept.SelectedValue) ). you must parse them accordingly

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -