请解决以下错误 [英] plz solve this error given below

查看:140
本文介绍了请解决以下错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {

            String connectionString = "....;user id=sa;password=...;database=student";
            SqlConnection thisConnection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand();


            thisConnection.Open();

            String thisQuery = "INSERT INTO detail (ROLL_NO, S_NAME, AGE) VALUES (''" + txtroll.Text + "'', ''" + txtname.Text + "'', ''" + txtage.Text + "'')";
            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);

            thisCommand.ExecuteNonQuery();
            thisConnection.Close();

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlCommand = sqlConnection.CreateCommand();
                SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                DataTable detail = new DataTable();
                sda.Fill(detail);
                BindingSource bSource = new BindingSource();
                bSource.DataSource = detail;
                dataGridView1.DataSource = bSource;
            }
        }
    }
}

推荐答案

您想从数据库中获取数据,但未定义任何命令.

在下面的行之后
You want to fetch data from database but did not define any commands.

After the below line
sqlCommand = sqlConnection.CreateCommand();


添加另一行,如


Add another line like

sqlCommand.CommandText = "Select ......";


(写选择查询以获取gridview的数据...)

然后修改该行


(Write Select Query to fetch the data for gridview...)

Then modify the line

SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);




to

SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);



添加一行将数据表绑定到gridviw,如



Add the line to bind the datatable to gridviw like

dataGridView1.DataBind();


请还记下您在运行此代码时确切得到的错误

你忘了绑定网格我belevie

please also write down what error u getting exactly when u run this code

u forget the bind the grid i belevie

dataGridView1.DataBind();


插入/编辑/删除操作后,重新绑定网格.
After Insert/Edit/delete operation re-bind your grid.


这篇关于请解决以下错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆