Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the Stored procedure "HSNMasterView" for select the value from table

I have written the FillDataGridView function fill the datagridview and call this function in SearchClick

What I have tried:

SQL
// dbo.HSNMasterView
ALTER PROCEDURE dbo.HSNMasterView
@HSNCode int AS SELECT * FROM inv_hsn WHERE  status = 1

c-sharp
//FillDataGridView
private void FillDataGridView()
        {
            try
            {
                if (sqlcon.State == ConnectionState.Closed)
                    sqlcon.Open();
                SqlDataAdapter sqlda = new SqlDataAdapter("HSNMasterView", sqlcon);
                sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;            
sqlda.SelectCommand.Parameters.AddWithValue("@HSNCode",txt_HSNSearch.Text.Trim());
                DataTable dtbl = new DataTable();
                sqlda.Fill(dtbl);
                dgv_HSNMaster.DataSource = dtbl;
                dgv_HSNMaster.Columns[0].Visible = false;
                sqlcon.Close();
            }
            catch (Exception ex)
            { 
                MessageBox.Show(ex.Message);
            }
        }

//Search Code
private void dgv_HSNMaster_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                FillDataGridView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
Posted
Updated 11-Nov-17 0:49am
v2
Comments
Karthik_Mahalingam 11-Nov-17 10:11am    
are you getting any error message?

1 solution

Everything looks fine except following few things-
1. Your stored procedure expects an INT parameter where as you are passing STRING as parameter.
2. If you haven't called it in the Page_Load or some appropriate event, it will not get loaded.
3. There is no point really to bind the gridview on dgv_HSNMaster_CellClick as it will never be called unless the grid is available to click.
4. I suspect, you want to call the bind grid on a button (search or something) click rather than grid cell click.

Suggestions:
1.
C#
sqlda.SelectCommand.Parameters.AddWithValue("@HSNCode",int.parse(txt_HSNSearch.Text.Trim()));

2. Have a search button(or use if you already have it) and bind the gridview in the button click event.

Please let me know if my assumptions are not correct and your problem is something other than this.

Hope, it helps :)
 
Share this answer
 
Comments
Karthik_Mahalingam 11-Nov-17 10:04am    
1. Your stored procedure expects an INT parameter where as you are passing STRING as parameter.

that doesnt matter, the concern is whether the string value is a number or not ?
validating the string using int.tryparse will do the trick.
Suvendu Shekhar Giri 11-Nov-17 10:20am    
Oh. I see. Not using ADO since few years. Thanks for educating!
Karthik_Mahalingam 11-Nov-17 10:24am    
welcome :)

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