This is my column in db
mobno bigint
9491992426
9491992426
9491992426
9491992426
9491992426
9491992426
This is my SP
SQL
create procedure sp_SearchBy
@mobno bigint
as begin
select * from form where mobno like '%' + @mobno + '%'
endi want to search with mobile number in grid view . when i searched with name i got the result but when i search with mobile no it is showing input string was in invlid format
i have taken bigint for mobno in db i want to search with mobile number pls help me
This is my webform.aspx.cs
C#
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DisplayInfo();
}
if (!this.IsPostBack)
{
this.BindGrid();
}
}
void DisplayInfo()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
string query = "select * from form";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid() (This Is for Searching )
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
SqlCommand cmd = new SqlCommand("sp_SearchByme", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mobno",Convert.ToInt32(TextBox7.Text)); //(I got the error here conversion problem)
//(Input string was not in a correct format)
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 16, 2023, 11:05 PM EST
Hello Jaya,
Try below - Change
cmd.Parameters.AddWithValue("@mobno",Convert.ToInt32(TextBox7.Text)); (I got the error here conversion problem)To
cmd.Parameters.AddWithValue("@mobno",Convert.ToInt64(TextBox7.Text));NOTE: Please check if your string has any white space or not. If it contains space, then will throw the error.
Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 16, 2023, 2:04 PM EST
The error "Input string was not in a correct format" occurs because you are trying to convert a string to an integer using the Convert.ToInt32 method, but the string is not in the correct format.
Since the mobno field in your database is defined as a bigint, you should use the Convert.ToInt64 method to convert the string to a long (which is the equivalent of a bigint in C#). Here is the modified code for the BindGrid method:
private void BindGrid() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); SqlCommand cmd = new SqlCommand("sp_SearchBy", con); cmd.CommandType = CommandType.StoredProcedure; long mobileNo; if (long.TryParse(TextBox7.Text, out mobileNo)) { cmd.Parameters.AddWithValue("@mobno", mobileNo); DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } else { // Handle invalid input } }In the modified code, I have used the long.TryParse method to convert the string to a long value. This method returns a boolean value that indicates whether the conversion was successful or not. If the conversion is successful, the mobileNo variable will contain the converted value, and you can use it as the parameter value for the stored procedure. If the conversion fails, you can handle the invalid input appropriately (for example, by displaying an error message to the user).
Bro hope this helps, please let me know if it works for you.
Amit MohantyRank #112 · 20K reputationPosted Feb 16, 2023, 11:12 PM EST
Your TextBox7 value must be numeric. Please chaek your value first. or you may try Tuhin Paul's solutions.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 16, 2023, 11:21 PM EST
Hello jaya,
What is the value of Texbox6.Text?
NOTE: Please check if your string has any white space or not. If it contains space, then will throw the error.
Thanks
Naimish Makwana
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 17, 2023, 12:34 AM EST
Hello Jaya,
Put debug point on line no 67. You shared screenshot below.
Once debug point reach on 67. Mouse over on Textbox6.Text it will show you the value of the textbox.
Thanks
Naimish Makwana
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 16, 2023, 10:56 PM EST
not working sir still the error same
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 16, 2023, 11:19 PM EST
Same Error Sir Pls Help me out in my database i took bigint for mobno
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 17, 2023, 12:27 AM EST
Hi Naimish Sir,
i about to enter mobile number in textbox6.text. when i debug the it is not showing the aspx page its directly thows an error
input string was not in a correct format